Skip to content

Commit

Permalink
refactor: Merged get graphql type
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Apr 16, 2021
1 parent be70dea commit 82d944c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 77 deletions.
10 changes: 3 additions & 7 deletions src/handlers/input-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 &&
Expand Down
13 changes: 3 additions & 10 deletions src/handlers/model-output-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
Expand Down
13 changes: 3 additions & 10 deletions src/handlers/output-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
Expand Down
27 changes: 18 additions & 9 deletions src/helpers/get-graphql-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,8 +16,8 @@ export function getGraphqlImport(args: {
const {
fileType,
location,
typeName,
isId,
name,
customType,
sourceFile,
getSourceFile,
Expand All @@ -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 };
}

Expand All @@ -52,9 +61,9 @@ export function getGraphqlImport(args: {
sourceFile.getFilePath(),
getSourceFile({
type: sourceFileType,
name,
name: typeName,
}).getFilePath(),
);

return { name, specifier };
return { name: typeName, specifier };
}
41 changes: 0 additions & 41 deletions src/helpers/get-graphql-type.ts

This file was deleted.

0 comments on commit 82d944c

Please sign in to comment.