diff --git a/src/handlers/input-type.ts b/src/handlers/input-type.ts index ae692685..bc1c320a 100644 --- a/src/handlers/input-type.ts +++ b/src/handlers/input-type.ts @@ -4,7 +4,6 @@ import { ClassDeclarationStructure, StructureKind } from 'ts-morph'; import { getGraphqlImport } from '../helpers/get-graphql-import'; import { getGraphqlInputType } from '../helpers/get-graphql-input-type'; -import { getGraphqlType } from '../helpers/get-graphql-type'; import { getPropertyType } from '../helpers/get-property-type'; import { ImportDeclarationMap } from '../helpers/import-declaration-map'; import { propertyStructure } from '../helpers/property-structure'; @@ -90,19 +89,16 @@ export function inputType( graphqlType = fieldType.name; importDeclarations.create({ ...fieldType }); } else { - graphqlType = getGraphqlType({ - location, - type: typeName, - }); - const graphqlImport = getGraphqlImport({ sourceFile, location, - name: graphqlType, + typeName, customType, getSourceFile, }); + graphqlType = graphqlImport.name; + if ( graphqlImport.name !== inputType.name && graphqlImport.specifier && diff --git a/src/handlers/model-output-type.ts b/src/handlers/model-output-type.ts index a5abdb89..71a24e02 100644 --- a/src/handlers/model-output-type.ts +++ b/src/handlers/model-output-type.ts @@ -13,7 +13,6 @@ import { } from 'ts-morph'; import { getGraphqlImport } from '../helpers/get-graphql-import'; -import { getGraphqlType } from '../helpers/get-graphql-type'; import { getOutputTypeName } from '../helpers/get-output-type-name'; import { getPropertyType } from '../helpers/get-property-type'; import { ImportDeclarationMap } from '../helpers/import-declaration-map'; @@ -112,24 +111,18 @@ export function modelOutputType(outputType: OutputType, args: EventArguments) { graphqlType = fieldType.name; importDeclarations.create({ ...fieldType }); } else { - graphqlType = - customType?.graphqlType ?? - getGraphqlType({ - location, - type: outputTypeName, - isId: modelField?.isId, - }); - const graphqlImport = getGraphqlImport({ sourceFile, fileType, location, isId: modelField?.isId, - name: graphqlType, + typeName: outputTypeName, customType, getSourceFile, }); + graphqlType = graphqlImport.name; + if (graphqlImport.name !== outputType.name && graphqlImport.specifier) { importDeclarations.add(graphqlImport.name, graphqlImport.specifier); } diff --git a/src/handlers/output-type.ts b/src/handlers/output-type.ts index 5ce99e70..41f5eb4e 100644 --- a/src/handlers/output-type.ts +++ b/src/handlers/output-type.ts @@ -2,7 +2,6 @@ import JSON5 from 'json5'; import { ClassDeclarationStructure, StructureKind } from 'ts-morph'; import { getGraphqlImport } from '../helpers/get-graphql-import'; -import { getGraphqlType } from '../helpers/get-graphql-type'; import { getOutputTypeName } from '../helpers/get-output-type-name'; import { getPropertyType } from '../helpers/get-property-type'; import { ImportDeclarationMap } from '../helpers/import-declaration-map'; @@ -87,24 +86,18 @@ export function outputType(outputType: OutputType, args: EventArguments) { classStructure.properties?.push(property); - const graphqlType = - customType?.graphqlType ?? - getGraphqlType({ - location, - type: outputTypeName, - isId: false, - }); - const graphqlImport = getGraphqlImport({ sourceFile, fileType, location, isId: false, - name: graphqlType, + typeName: outputTypeName, customType, getSourceFile, }); + const graphqlType = graphqlImport.name; + if (graphqlImport.name !== outputType.name && graphqlImport.specifier) { importDeclarations.add(graphqlImport.name, graphqlImport.specifier); } diff --git a/src/helpers/get-graphql-import.ts b/src/helpers/get-graphql-import.ts index b1bcaeb8..5ced666a 100644 --- a/src/helpers/get-graphql-import.ts +++ b/src/helpers/get-graphql-import.ts @@ -6,7 +6,7 @@ import { relativePath } from './relative-path'; export function getGraphqlImport(args: { sourceFile: SourceFile; - name: string; + typeName: string; location: FieldLocation; isId?: boolean; fileType?: string; @@ -16,8 +16,8 @@ export function getGraphqlImport(args: { const { fileType, location, + typeName, isId, - name, customType, sourceFile, getSourceFile, @@ -34,12 +34,21 @@ export function getGraphqlImport(args: { if (isId) { return { name: 'ID', specifier: '@nestjs/graphql' }; } - if (['Int', 'Float'].includes(name)) { - return { name, specifier: '@nestjs/graphql' }; - } - if (['true', 'Boolean'].includes(name)) { - return { name: 'Boolean', specifier: undefined }; + + switch (typeName) { + case 'Float': + case 'Int': + case 'String': + return { name: typeName, specifier: '@nestjs/graphql' }; + case 'DateTime': + return { name: 'Date', specifier: undefined }; + case 'true': + case 'Boolean': + return { name: 'Boolean', specifier: undefined }; + case 'Json': + return { name: 'GraphQLJSON', specifier: 'graphql-type-json' }; } + return { name: 'String', specifier: undefined }; } @@ -52,9 +61,9 @@ export function getGraphqlImport(args: { sourceFile.getFilePath(), getSourceFile({ type: sourceFileType, - name, + name: typeName, }).getFilePath(), ); - return { name, specifier }; + return { name: typeName, specifier }; } diff --git a/src/helpers/get-graphql-type.ts b/src/helpers/get-graphql-type.ts deleted file mode 100644 index 1d22c7e1..00000000 --- a/src/helpers/get-graphql-type.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { FieldLocation } from '../types'; - -/** - * Return type for `Field` decorator - */ -export function getGraphqlType(args: { - type: string; - location: FieldLocation; - isId?: boolean; -}) { - const { type, location, isId } = args; - - if (['inputObjectTypes', 'outputObjectTypes', 'enumTypes'].includes(location)) { - return type; - } - - if (isId) { - return 'ID'; - } - - let result = 'String'; - switch (type) { - case 'Float': - case 'Int': - case 'Boolean': - case 'String': - result = type; - break; - case 'DateTime': - result = 'Date'; - break; - case 'true': - result = 'Boolean'; - break; - case 'Json': - result = 'GraphQLJSON'; - break; - } - - return result; -}