Skip to content

Commit

Permalink
fix: Generate correct json graphql type
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Mar 1, 2021
1 parent 95d4629 commit c6d8d46
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/@generated/dummy/dummy-group-by.output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class DummyGroupBy {
@Field(() => GraphQLJSON, {
nullable: true,
})
json?: Record<string, any>;
json?: any;

@Field(() => DummyCountAggregate, {
nullable: true,
Expand Down
2 changes: 1 addition & 1 deletion src/@generated/dummy/dummy-unchecked-update-many.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class DummyUncheckedUpdateManyInput {
})
bigInt?: NullableBigIntFieldUpdateOperationsInput;

@Field(() => String, {
@Field(() => GraphQLJSON, {
nullable: true,
})
json?: any;
Expand Down
2 changes: 1 addition & 1 deletion src/@generated/dummy/dummy.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ export class Dummy {
@Field(() => GraphQLJSON, {
nullable: true,
})
json?: Record<string, any>;
json?: any;
}
4 changes: 2 additions & 2 deletions src/@generated/prisma/json-nullable-filter.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/@generated/prisma/nested-json-nullable-filter.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions src/compatibility.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/example/app.module.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
9 changes: 9 additions & 0 deletions src/example/dummy/dummy.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';

import { DummyResolver } from './dummy.resolver';

@Module({
imports: [],
providers: [DummyResolver],
})
export class DummyModule {}
21 changes: 21 additions & 0 deletions src/example/dummy/dummy.resolver.ts
Original file line number Diff line number Diff line change
@@ -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];
}
}
24 changes: 24 additions & 0 deletions src/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ describe('one model with scalar types', () => {
data Json
}`,
});
// const filePaths = sourceFiles.map(s => s.getFilePath());
});

describe('user model', () => {
Expand Down Expand Up @@ -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()));
});
});

Expand Down
2 changes: 2 additions & 0 deletions src/handlers/input-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export function inputType(
getSourceFile,
});

// if (inputType.name === 'JsonFilter') {
// console.log({
// 'inputType.name': inputType.name,
// 'field.name': field.name,
Expand All @@ -88,6 +89,7 @@ export function inputType(
// graphqlType,
// graphqlImport,
// });
// }

if (graphqlImport.name !== inputType.name && graphqlImport.specifier) {
generateImport({
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function createConfig(data: Record<string, string | undefined>) {
{},
{
Json: {
fieldType: 'Record<string, any>',
fieldType: 'any',
graphqlType: 'GraphQLJSON',
graphqlModule: 'graphql-type-json',
},
Expand Down
3 changes: 3 additions & 0 deletions src/helpers/get-graphql-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export function getGraphqlType(args: {
case 'true':
result = 'Boolean';
break;
case 'Json':
result = 'GraphQLJSON';
break;
}

return result;
Expand Down

0 comments on commit c6d8d46

Please sign in to comment.