Skip to content

Commit

Permalink
fix: use correct import for json values
Browse files Browse the repository at this point in the history
  • Loading branch information
rfermann committed Apr 17, 2021
1 parent c060a3e commit 355361b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 33 deletions.
11 changes: 5 additions & 6 deletions src/Handlers/BaseHandler/BaseFileGenerator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,14 @@ export class BaseFileGenerator {
}

// eslint-disable-next-line class-methods-use-this
addJsonImports({ imports, sourceFile }: { imports: string[]; sourceFile: SourceFile }): void {
addJsonImports({ sourceFile }: { sourceFile: SourceFile }): void {
sourceFile.addImportDeclaration({
moduleSpecifier: "graphql-type-json",
namedImports: ["GraphQLJSON"],
moduleSpecifier: "@prisma/client",
namedImports: [this._baseParser.prismaImport],
});

sourceFile.addImportDeclaration({
moduleSpecifier: "@prisma/client",
namedImports: imports.sort(comparePrimitiveValues),
moduleSpecifier: "graphql-type-json",
namedImports: ["GraphQLJSON"],
});
}

Expand Down
10 changes: 8 additions & 2 deletions src/Handlers/BaseHandler/BaseParser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export class BaseParser {
NestJSTypes.Int as string,
]);

readonly prismaImport = "Prisma";

constructor(dmmf: DMMF.Document) {
this.dmmf = dmmf;

Expand Down Expand Up @@ -146,11 +148,11 @@ export class BaseParser {
stringType = type.name;
}

if (location === "scalar" && this.jsonImports.has(stringType)) {
if (location === "scalar" && this.jsonImports.has(stringType.replace(`${this.prismaImport}.`, ""))) {
jsonImports.add(stringType);
}

if (this.jsonImports.has(tsType.split(" | ")[0])) {
if (this.jsonImports.has(tsType.replace(`${this.prismaImport}.`, "").split(" | ")[0])) {
jsonImports.add(tsType.split(" | ")[0]);
}

Expand Down Expand Up @@ -305,6 +307,10 @@ export class BaseParser {
throw new Error(`Unsupported field location: ${location}`);
}

if (this.jsonImports.has(fieldType)) {
fieldType = `${this.prismaImport}.${fieldType}`;
}

if (isList) {
if (fieldType.includes(" ")) {
fieldType = `Array<${fieldType}>`;
Expand Down
42 changes: 21 additions & 21 deletions src/Handlers/InputTypeHandler/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1041,8 +1041,8 @@ export class IntWithAggregatesFilter {

exports[`InputTypeHandler should parse input types and create correct shared files from datamodel 25`] = `
"import { Field, InputType } from \\"@nestjs/graphql\\";
import { Prisma } from \\"@prisma/client\\";
import { GraphQLJSON } from \\"graphql-type-json\\";
import { JsonValue } from \\"@prisma/client\\";
@InputType({
isAbstract: true,
Expand All @@ -1051,20 +1051,20 @@ export class JsonFilter {
@Field(() => GraphQLJSON, {
nullable: true
})
equals?: JsonValue | undefined;
equals?: Prisma.JsonValue | undefined;
@Field(() => GraphQLJSON, {
nullable: true
})
not?: JsonValue | undefined;
not?: Prisma.JsonValue | undefined;
}
"
`;

exports[`InputTypeHandler should parse input types and create correct shared files from datamodel 26`] = `
"import { Field, InputType } from \\"@nestjs/graphql\\";
import { Prisma } from \\"@prisma/client\\";
import { GraphQLJSON } from \\"graphql-type-json\\";
import { JsonValue } from \\"@prisma/client\\";
import { NestedIntFilter } from \\"./NestedIntFilter\\";
import { NestedJsonFilter } from \\"./NestedJsonFilter\\";
Expand All @@ -1075,12 +1075,12 @@ export class JsonWithAggregatesFilter {
@Field(() => GraphQLJSON, {
nullable: true
})
equals?: JsonValue | undefined;
equals?: Prisma.JsonValue | undefined;
@Field(() => GraphQLJSON, {
nullable: true
})
not?: JsonValue | undefined;
not?: Prisma.JsonValue | undefined;
@Field(() => NestedIntFilter, {
nullable: true
Expand Down Expand Up @@ -1888,8 +1888,8 @@ export class NestedIntWithAggregatesFilter {

exports[`InputTypeHandler should parse input types and create correct shared files from datamodel 44`] = `
"import { Field, InputType } from \\"@nestjs/graphql\\";
import { Prisma } from \\"@prisma/client\\";
import { GraphQLJSON } from \\"graphql-type-json\\";
import { JsonValue } from \\"@prisma/client\\";
@InputType({
isAbstract: true,
Expand All @@ -1898,12 +1898,12 @@ export class NestedJsonFilter {
@Field(() => GraphQLJSON, {
nullable: true
})
equals?: JsonValue | undefined;
equals?: Prisma.JsonValue | undefined;
@Field(() => GraphQLJSON, {
nullable: true
})
not?: JsonValue | undefined;
not?: Prisma.JsonValue | undefined;
}
"
`;
Expand Down Expand Up @@ -2595,8 +2595,8 @@ Array [

exports[`InputTypeHandler should parse input types and create correct user input files from datamodel 1`] = `
"import { Field, Float, InputType } from \\"@nestjs/graphql\\";
import { Prisma } from \\"@prisma/client\\";
import { GraphQLJSON } from \\"graphql-type-json\\";
import { JsonValue } from \\"@prisma/client\\";
import { UserTypeEnum } from \\"../../enums\\";
import { SessionCreateNestedManyWithoutUserInput } from \\"../../Session/inputTypes/SessionCreateNestedManyWithoutUserInput\\";
Expand Down Expand Up @@ -2632,7 +2632,7 @@ export class UserCreateInput {
@Field(() => GraphQLJSON, {
nullable: false
})
rawData!: JsonValue;
rawData!: Prisma.JsonValue;
@Field(() => SessionCreateNestedManyWithoutUserInput, {
nullable: true
Expand All @@ -2644,8 +2644,8 @@ export class UserCreateInput {

exports[`InputTypeHandler should parse input types and create correct user input files from datamodel 2`] = `
"import { Field, Float, InputType, Int } from \\"@nestjs/graphql\\";
import { Prisma } from \\"@prisma/client\\";
import { GraphQLJSON } from \\"graphql-type-json\\";
import { JsonValue } from \\"@prisma/client\\";
import { UserTypeEnum } from \\"../../enums\\";
@InputType({
Expand Down Expand Up @@ -2685,7 +2685,7 @@ export class UserCreateManyInput {
@Field(() => GraphQLJSON, {
nullable: false
})
rawData!: JsonValue;
rawData!: Prisma.JsonValue;
}
"
`;
Expand Down Expand Up @@ -2742,8 +2742,8 @@ export class UserCreateOrConnectWithoutSessionInput {

exports[`InputTypeHandler should parse input types and create correct user input files from datamodel 5`] = `
"import { Field, Float, InputType } from \\"@nestjs/graphql\\";
import { Prisma } from \\"@prisma/client\\";
import { GraphQLJSON } from \\"graphql-type-json\\";
import { JsonValue } from \\"@prisma/client\\";
import { UserTypeEnum } from \\"../../enums\\";
@InputType({
Expand Down Expand Up @@ -2778,7 +2778,7 @@ export class UserCreateWithoutSessionInput {
@Field(() => GraphQLJSON, {
nullable: false
})
rawData!: JsonValue;
rawData!: Prisma.JsonValue;
}
"
`;
Expand Down Expand Up @@ -2904,8 +2904,8 @@ export class UserScalarWhereWithAggregatesInput {

exports[`InputTypeHandler should parse input types and create correct user input files from datamodel 9`] = `
"import { Field, InputType } from \\"@nestjs/graphql\\";
import { Prisma } from \\"@prisma/client\\";
import { GraphQLJSON } from \\"graphql-type-json\\";
import { JsonValue } from \\"@prisma/client\\";
import { StringFieldUpdateOperationsInput } from \\"../../shared/inputTypes/StringFieldUpdateOperationsInput\\";
import { NullableStringFieldUpdateOperationsInput } from \\"../../shared/inputTypes/NullableStringFieldUpdateOperationsInput\\";
import { EnumUserTypeFieldUpdateOperationsInput } from \\"../../shared/inputTypes/EnumUserTypeFieldUpdateOperationsInput\\";
Expand Down Expand Up @@ -2945,7 +2945,7 @@ export class UserUpdateInput {
@Field(() => GraphQLJSON, {
nullable: true
})
rawData?: JsonValue | undefined;
rawData?: Prisma.JsonValue | undefined;
@Field(() => SessionUpdateManyWithoutUserInput, {
nullable: true
Expand All @@ -2957,8 +2957,8 @@ export class UserUpdateInput {

exports[`InputTypeHandler should parse input types and create correct user input files from datamodel 10`] = `
"import { Field, InputType } from \\"@nestjs/graphql\\";
import { Prisma } from \\"@prisma/client\\";
import { GraphQLJSON } from \\"graphql-type-json\\";
import { JsonValue } from \\"@prisma/client\\";
import { StringFieldUpdateOperationsInput } from \\"../../shared/inputTypes/StringFieldUpdateOperationsInput\\";
import { NullableStringFieldUpdateOperationsInput } from \\"../../shared/inputTypes/NullableStringFieldUpdateOperationsInput\\";
import { EnumUserTypeFieldUpdateOperationsInput } from \\"../../shared/inputTypes/EnumUserTypeFieldUpdateOperationsInput\\";
Expand Down Expand Up @@ -2997,7 +2997,7 @@ export class UserUpdateManyMutationInput {
@Field(() => GraphQLJSON, {
nullable: true
})
rawData?: JsonValue | undefined;
rawData?: Prisma.JsonValue | undefined;
}
"
`;
Expand Down Expand Up @@ -3044,8 +3044,8 @@ export class UserUpdateOneRequiredWithoutSessionInput {

exports[`InputTypeHandler should parse input types and create correct user input files from datamodel 12`] = `
"import { Field, InputType } from \\"@nestjs/graphql\\";
import { Prisma } from \\"@prisma/client\\";
import { GraphQLJSON } from \\"graphql-type-json\\";
import { JsonValue } from \\"@prisma/client\\";
import { StringFieldUpdateOperationsInput } from \\"../../shared/inputTypes/StringFieldUpdateOperationsInput\\";
import { NullableStringFieldUpdateOperationsInput } from \\"../../shared/inputTypes/NullableStringFieldUpdateOperationsInput\\";
import { EnumUserTypeFieldUpdateOperationsInput } from \\"../../shared/inputTypes/EnumUserTypeFieldUpdateOperationsInput\\";
Expand Down Expand Up @@ -3084,7 +3084,7 @@ export class UserUpdateWithoutSessionInput {
@Field(() => GraphQLJSON, {
nullable: true
})
rawData?: JsonValue | undefined;
rawData?: Prisma.JsonValue | undefined;
}
"
`;
Expand Down
2 changes: 1 addition & 1 deletion src/Handlers/InputTypeHandler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class InputTypeHandler extends BaseHandler {
}

if (jsonImports && jsonImports.length > 0) {
this.baseFileGenerator.addJsonImports({ imports: jsonImports, sourceFile });
this.baseFileGenerator.addJsonImports({ sourceFile });
}

if (enumImports && enumImports.length > 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/Handlers/ModelHandler/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

exports[`ModelHandler should parse models and create correct files from datamodel 1`] = `
"import { Field, Float, Int, ObjectType } from \\"@nestjs/graphql\\";
import { Prisma } from \\"@prisma/client\\";
import { GraphQLJSON } from \\"graphql-type-json\\";
import { JsonValue } from \\"@prisma/client\\";
import { UserTypeEnum } from \\"../enums\\";
import { Session } from \\"../Session/model\\";
Expand Down Expand Up @@ -48,7 +48,7 @@ export class User {
nullable: false
description: \\"user field comment\\"
})
rawData!: JsonValue;
rawData!: Prisma.JsonValue;
@Field(() => [Session], {
nullable: true
Expand Down
2 changes: 1 addition & 1 deletion src/Handlers/ModelHandler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class ModelHandler extends BaseHandler {
}

if (jsonImports && jsonImports.length > 0) {
this.baseFileGenerator.addJsonImports({ imports: jsonImports, sourceFile });
this.baseFileGenerator.addJsonImports({ sourceFile });
}

if (enumImports && enumImports.length > 0) {
Expand Down

0 comments on commit 355361b

Please sign in to comment.