Skip to content

Commit

Permalink
chore: Fixed ESLint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Apr 16, 2021
1 parent 34196b3 commit 77aecd6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/example/user/user-date.input.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Field, GraphQLISODateTime, InputType, Int } from '@nestjs/graphql';
import { Field, GraphQLISODateTime, InputType } from '@nestjs/graphql';

@InputType()
export class UserDateInput {
Expand Down
1 change: 1 addition & 0 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export async function generate(
const prismaClientDmmf: DMMF.Document = JSON.parse(
JSON.stringify(
args.prismaClientDmmf ??
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access
(require(prismaClientOutput).dmmf as DMMF.Document),
),
);
Expand Down
8 changes: 5 additions & 3 deletions src/handlers/model-output-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { ImportDeclarationMap } from '../helpers/import-declaration-map';
import { propertyStructure } from '../helpers/property-structure';
import { EventArguments, OutputType } from '../types';

const nestjsGraphql = '@nestjs/graphql';

export function modelOutputType(outputType: OutputType, args: EventArguments) {
const {
getSourceFile,
Expand Down Expand Up @@ -76,8 +78,8 @@ export function modelOutputType(outputType: OutputType, args: EventArguments) {
? [JSON5.stringify({ description: model.documentation })]
: [];

importDeclarations.add('Field', '@nestjs/graphql');
importDeclarations.add('ObjectType', '@nestjs/graphql');
importDeclarations.add('Field', nestjsGraphql);
importDeclarations.add('ObjectType', nestjsGraphql);

for (const field of outputType.fields) {
// Do not generate already defined properties for model
Expand Down Expand Up @@ -156,7 +158,7 @@ export function modelOutputType(outputType: OutputType, args: EventArguments) {
}

if (settings?.hideOutput) {
importDeclarations.add('HideField', '@nestjs/graphql');
importDeclarations.add('HideField', nestjsGraphql);
property.decorators?.push({ name: 'HideField', arguments: [] });
} else {
property.decorators?.push({
Expand Down
8 changes: 5 additions & 3 deletions src/handlers/output-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { ImportDeclarationMap } from '../helpers/import-declaration-map';
import { propertyStructure } from '../helpers/property-structure';
import { EventArguments, OutputType } from '../types';

const nestjsGraphql = '@nestjs/graphql';

export function outputType(outputType: OutputType, args: EventArguments) {
const {
getSourceFile,
Expand Down Expand Up @@ -51,8 +53,8 @@ export function outputType(outputType: OutputType, args: EventArguments) {
properties: [],
};

importDeclarations.add('Field', '@nestjs/graphql');
importDeclarations.add('ObjectType', '@nestjs/graphql');
importDeclarations.add('Field', nestjsGraphql);
importDeclarations.add('ObjectType', nestjsGraphql);

for (const field of outputType.fields) {
const { location, isList, type } = field.outputType;
Expand Down Expand Up @@ -108,7 +110,7 @@ export function outputType(outputType: OutputType, args: EventArguments) {
}

if (settings?.hideOutput) {
importDeclarations.add('HideField', '@nestjs/graphql');
importDeclarations.add('HideField', nestjsGraphql);
property.decorators?.push({ name: 'HideField', arguments: [] });
} else {
// Generate `@Field()` decorator
Expand Down
46 changes: 25 additions & 21 deletions src/helpers/field-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type FieldSetting = {
from: string;
defaultImport?: string | true;
namespaceImport?: string;
namespace?: string;
};

export class FieldSettings extends Array<FieldSetting> {
Expand Down Expand Up @@ -52,34 +53,16 @@ export function createFieldSettings(args: {
input: false,
output: false,
from: '',
defaultImport: undefined,
namespaceImport: undefined,
};
if (name === 'TypeGraphQL.omit' || name === 'HideField') {
Object.assign(decorator, {
name: 'HideField',
arguments: [],
from: '@nestjs/graphql',
defaultImport: false,
namespaceImport: false,
});
if (match.groups?.args) {
if (/output:\s*true/.test(match.groups.args)) {
decorator.output = true;
}
if (/input:\s*true/.test(match.groups.args)) {
decorator.input = true;
}
} else {
decorator.output = true;
}
Object.assign(decorator, hideFieldDecorator(match));
} else if (name === 'FieldType' && match.groups?.args) {
let options = parseArgs(match.groups.args);
if (typeof options === 'string') {
options = { name: options };
}
const namespace = getNamespace(options.name);
if (options.name.includes('.')) {
if ((options as { name: string }).name.includes('.')) {
decorator.namespaceImport = namespace;
}
merge(decorator, config.fields[namespace], options, { isFieldType: true });
Expand All @@ -104,7 +87,28 @@ export function createFieldSettings(args: {
};
}

function parseArgs(string: string) {
function hideFieldDecorator(match: RegExpExecArray) {
const result: Partial<FieldSetting> = {
name: 'HideField',
arguments: [],
from: '@nestjs/graphql',
defaultImport: undefined,
namespaceImport: undefined,
};
if (match.groups?.args) {
if (/output:\s*true/.test(match.groups.args)) {
result.output = true;
}
if (/input:\s*true/.test(match.groups.args)) {
result.input = true;
}
} else {
result.output = true;
}
return result;
}

function parseArgs(string: string): Record<string, unknown> | string {
try {
return JSON5.parse(string);
} catch {
Expand Down

0 comments on commit 77aecd6

Please sign in to comment.