From c6d8d46e3cb1627f07a33f2989272911283ddc01 Mon Sep 17 00:00:00 2001 From: Roman_Vasilev Date: Tue, 2 Mar 2021 00:47:42 +0400 Subject: [PATCH] fix: Generate correct json graphql type --- src/@generated/dummy/dummy-group-by.output.ts | 2 +- .../dummy-unchecked-update-many.input.ts | 2 +- src/@generated/dummy/dummy.model.ts | 2 +- .../prisma/json-nullable-filter.input.ts | 4 ++-- ...n-nullable-with-aggregates-filter.input.ts | 4 ++-- .../nested-json-nullable-filter.input.ts | 4 ++-- src/compatibility.spec.ts | 11 +++++++++ src/example/app.module.ts | 2 ++ src/example/dummy/dummy.module.ts | 9 +++++++ src/example/dummy/dummy.resolver.ts | 21 ++++++++++++++++ src/generate.spec.ts | 24 +++++++++++++++++++ src/handlers/input-type.ts | 2 ++ src/helpers/create-config.ts | 2 +- src/helpers/get-graphql-type.ts | 3 +++ 14 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 src/example/dummy/dummy.module.ts create mode 100644 src/example/dummy/dummy.resolver.ts diff --git a/src/@generated/dummy/dummy-group-by.output.ts b/src/@generated/dummy/dummy-group-by.output.ts index 5ca8b59a..3de4f496 100644 --- a/src/@generated/dummy/dummy-group-by.output.ts +++ b/src/@generated/dummy/dummy-group-by.output.ts @@ -47,7 +47,7 @@ export class DummyGroupBy { @Field(() => GraphQLJSON, { nullable: true, }) - json?: Record; + json?: any; @Field(() => DummyCountAggregate, { nullable: true, diff --git a/src/@generated/dummy/dummy-unchecked-update-many.input.ts b/src/@generated/dummy/dummy-unchecked-update-many.input.ts index be3af7e6..684e37cc 100644 --- a/src/@generated/dummy/dummy-unchecked-update-many.input.ts +++ b/src/@generated/dummy/dummy-unchecked-update-many.input.ts @@ -46,7 +46,7 @@ export class DummyUncheckedUpdateManyInput { }) bigInt?: NullableBigIntFieldUpdateOperationsInput; - @Field(() => String, { + @Field(() => GraphQLJSON, { nullable: true, }) json?: any; diff --git a/src/@generated/dummy/dummy.model.ts b/src/@generated/dummy/dummy.model.ts index 564fe9b8..2cc2b3fe 100644 --- a/src/@generated/dummy/dummy.model.ts +++ b/src/@generated/dummy/dummy.model.ts @@ -41,5 +41,5 @@ export class Dummy { @Field(() => GraphQLJSON, { nullable: true, }) - json?: Record; + json?: any; } diff --git a/src/@generated/prisma/json-nullable-filter.input.ts b/src/@generated/prisma/json-nullable-filter.input.ts index 3249cb42..3cd53014 100644 --- a/src/@generated/prisma/json-nullable-filter.input.ts +++ b/src/@generated/prisma/json-nullable-filter.input.ts @@ -3,12 +3,12 @@ import { GraphQLJSON } from 'graphql-type-json'; @InputType() export class JsonNullableFilter { - @Field(() => String, { + @Field(() => GraphQLJSON, { nullable: true, }) equals?: any; - @Field(() => String, { + @Field(() => GraphQLJSON, { nullable: true, }) not?: any; diff --git a/src/@generated/prisma/json-nullable-with-aggregates-filter.input.ts b/src/@generated/prisma/json-nullable-with-aggregates-filter.input.ts index 08789537..da1df6be 100644 --- a/src/@generated/prisma/json-nullable-with-aggregates-filter.input.ts +++ b/src/@generated/prisma/json-nullable-with-aggregates-filter.input.ts @@ -6,12 +6,12 @@ import { NestedJsonNullableFilter } from './nested-json-nullable-filter.input'; @InputType() export class JsonNullableWithAggregatesFilter { - @Field(() => String, { + @Field(() => GraphQLJSON, { nullable: true, }) equals?: any; - @Field(() => String, { + @Field(() => GraphQLJSON, { nullable: true, }) not?: any; diff --git a/src/@generated/prisma/nested-json-nullable-filter.input.ts b/src/@generated/prisma/nested-json-nullable-filter.input.ts index ab28d606..fbcea87c 100644 --- a/src/@generated/prisma/nested-json-nullable-filter.input.ts +++ b/src/@generated/prisma/nested-json-nullable-filter.input.ts @@ -3,12 +3,12 @@ import { GraphQLJSON } from 'graphql-type-json'; @InputType() export class NestedJsonNullableFilter { - @Field(() => String, { + @Field(() => GraphQLJSON, { nullable: true, }) equals?: any; - @Field(() => String, { + @Field(() => GraphQLJSON, { nullable: true, }) not?: any; diff --git a/src/compatibility.spec.ts b/src/compatibility.spec.ts index de70cd3d..bd1e2cc1 100644 --- a/src/compatibility.spec.ts +++ b/src/compatibility.spec.ts @@ -1,5 +1,6 @@ import { Prisma, PrismaClient } from '@prisma/client'; +import { DummyCreateInput } from './@generated/dummy/dummy-create.input'; import { DateTimeFilter } from './@generated/prisma/date-time-filter.input'; import { FloatFilter } from './@generated/prisma/float-filter.input'; import { IntFilter } from './@generated/prisma/int-filter.input'; @@ -106,3 +107,13 @@ const $prisma = new PrismaClient(); }; p = x; } +{ + const x: DummyCreateInput = { id: '1', floaty: 1 }; + let p: Prisma.DummyCreateInput = { id: '2', floaty: 2 }; + p = x; +} +{ + const x: DummyCreateInput['json'] = {}; + let p: Prisma.DummyCreateInput['json'] = {}; + p = x; +} diff --git a/src/example/app.module.ts b/src/example/app.module.ts index a279a7ea..b1251580 100644 --- a/src/example/app.module.ts +++ b/src/example/app.module.ts @@ -1,11 +1,13 @@ import { Module } from '@nestjs/common'; import { GraphQLModule } from '@nestjs/graphql'; +import { DummyModule } from './dummy/dummy.module'; import { UserModule } from './user/user.module'; @Module({ imports: [ UserModule, + DummyModule, GraphQLModule.forRoot({ installSubscriptionHandlers: true, autoSchemaFile: '~schema.gql', diff --git a/src/example/dummy/dummy.module.ts b/src/example/dummy/dummy.module.ts new file mode 100644 index 00000000..58d86e6e --- /dev/null +++ b/src/example/dummy/dummy.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; + +import { DummyResolver } from './dummy.resolver'; + +@Module({ + imports: [], + providers: [DummyResolver], +}) +export class DummyModule {} diff --git a/src/example/dummy/dummy.resolver.ts b/src/example/dummy/dummy.resolver.ts new file mode 100644 index 00000000..bea89e8b --- /dev/null +++ b/src/example/dummy/dummy.resolver.ts @@ -0,0 +1,21 @@ +import { Query, Resolver } from '@nestjs/graphql'; + +import { Dummy } from '../../@generated/dummy/dummy.model'; + +/** + * Resolves user object type. + */ +@Resolver(() => Dummy) +export class DummyResolver { + /** + * Query for single user. + */ + @Query(() => [Dummy]) + dummies() { + const dummy = new Dummy(); + dummy.json = { + a: 1, + }; + return [dummy]; + } +} diff --git a/src/generate.spec.ts b/src/generate.spec.ts index 9dc4f1c6..c24b80a1 100644 --- a/src/generate.spec.ts +++ b/src/generate.spec.ts @@ -324,6 +324,7 @@ describe('one model with scalar types', () => { data Json }`, }); + // const filePaths = sourceFiles.map(s => s.getFilePath()); }); describe('user model', () => { @@ -494,6 +495,29 @@ describe('one model with scalar types', () => { const property = getPropertyStructure(sourceFile, 'rating'); expect(property?.type).toEqual('number'); }); + + it('data property (json)', () => { + expect(getFieldType(sourceFile, 'data')).toEqual('() => GraphQLJSON'); + }); + }); + + describe('json filter', () => { + before(() => { + sourceFile = project.getSourceFile(s => + s.getFilePath().endsWith('/json-filter.input.ts'), + )!; + }); + + it('not field should be json filter', () => { + const not = p('not'); + expect(not?.type).toEqual('any'); + }); + + it('field type should be GraphQLJSON', () => { + expect(getFieldType(sourceFile, 'not')).toEqual('() => GraphQLJSON'); + }); + + // it('', () => console.log(sourceFile.getText())); }); }); diff --git a/src/handlers/input-type.ts b/src/handlers/input-type.ts index 8e714bb6..175812cf 100644 --- a/src/handlers/input-type.ts +++ b/src/handlers/input-type.ts @@ -78,6 +78,7 @@ export function inputType( getSourceFile, }); + // if (inputType.name === 'JsonFilter') { // console.log({ // 'inputType.name': inputType.name, // 'field.name': field.name, @@ -88,6 +89,7 @@ export function inputType( // graphqlType, // graphqlImport, // }); + // } if (graphqlImport.name !== inputType.name && graphqlImport.specifier) { generateImport({ diff --git a/src/helpers/create-config.ts b/src/helpers/create-config.ts index 11e24c28..13ccb1df 100644 --- a/src/helpers/create-config.ts +++ b/src/helpers/create-config.ts @@ -23,7 +23,7 @@ export function createConfig(data: Record) { {}, { Json: { - fieldType: 'Record', + fieldType: 'any', graphqlType: 'GraphQLJSON', graphqlModule: 'graphql-type-json', }, diff --git a/src/helpers/get-graphql-type.ts b/src/helpers/get-graphql-type.ts index 7b915315..1d22c7e1 100644 --- a/src/helpers/get-graphql-type.ts +++ b/src/helpers/get-graphql-type.ts @@ -32,6 +32,9 @@ export function getGraphqlType(args: { case 'true': result = 'Boolean'; break; + case 'Json': + result = 'GraphQLJSON'; + break; } return result;