diff --git a/packages/generator/src/sdl/GenerateTypes.ts b/packages/generator/src/sdl/GenerateTypes.ts index bbb0b660..28bc50c8 100644 --- a/packages/generator/src/sdl/GenerateTypes.ts +++ b/packages/generator/src/sdl/GenerateTypes.ts @@ -43,6 +43,23 @@ export class GenerateTypes { ); } + getParentType(name: string) { + if (['Query', 'Mutation'].includes(name)) { + return '{}'; + } + if (name === 'AffectedRowsOutput') { + return 'Client.Prisma.BatchPayload'; + } + if (this.isModel(name)) { + return `Client.${name}`; + } + if (name.startsWith('CreateMany') && name.endsWith('AndReturnOutputType')) { + const innerType = name.replace(/^CreateMany|AndReturnOutputType$/g, ''); + return `ReturnType`; + } + return `Client.Prisma.${name}`; + } + capital(name: string) { return name.charAt(0).toUpperCase() + name.slice(1); } @@ -57,6 +74,10 @@ export class GenerateTypes { options.type }Args>`; } + if (options.type.startsWith('CreateMany') && options.type.endsWith('AndReturnOutputType')) { + const innerType = options.type.replace(/^CreateMany|AndReturnOutputType$/g, ''); + return `ReturnType`; + } const type = options.type.toString() === 'AffectedRowsOutput' ? 'Prisma.BatchPayload' @@ -105,15 +126,7 @@ export class GenerateTypes { // generate fields type.fields.forEach((field) => { - const parentType = ['Query', 'Mutation'].includes(type.name) - ? '{}' - : `Client.${ - type.name === 'AffectedRowsOutput' - ? 'Prisma.BatchPayload' - : !this.isModel(type.name) - ? 'Prisma.' + type.name - : type.name - }`; + const parentType = this.getParentType(type.name); const argsType = field.args.length > 0 ? `${['Query', 'Mutation'].includes(type.name) ? '' : type.name}${this.capital(field.name)}Args` diff --git a/packages/generator/tests/SDL/__snapshots__/generateSDLTypescript.test.ts.snap b/packages/generator/tests/SDL/__snapshots__/generateSDLTypescript.test.ts.snap index 4b634532..e0a9b50a 100644 --- a/packages/generator/tests/SDL/__snapshots__/generateSDLTypescript.test.ts.snap +++ b/packages/generator/tests/SDL/__snapshots__/generateSDLTypescript.test.ts.snap @@ -77,25 +77,33 @@ export type Post = { [key: string]: Resolver } & { export type CreateManyUserAndReturnOutputType = { [key: string]: Resolver } & { - id?: Resolver + id?: Resolver< + ReturnType, + {}, + number + > createdAt?: Resolver< - Client.Prisma.CreateManyUserAndReturnOutputType, + ReturnType, {}, Date > - email?: Resolver + email?: Resolver< + ReturnType, + {}, + string + > name?: Resolver< - Client.Prisma.CreateManyUserAndReturnOutputType, + ReturnType, {}, string | null > password?: Resolver< - Client.Prisma.CreateManyUserAndReturnOutputType, + ReturnType, {}, string > permissions?: Resolver< - Client.Prisma.CreateManyUserAndReturnOutputType, + ReturnType, {}, any > @@ -104,30 +112,38 @@ export type CreateManyUserAndReturnOutputType = { export type CreateManyPostAndReturnOutputType = { [key: string]: Resolver } & { - id?: Resolver + id?: Resolver< + ReturnType, + {}, + number + > published?: Resolver< - Client.Prisma.CreateManyPostAndReturnOutputType, + ReturnType, {}, boolean > - title?: Resolver + title?: Resolver< + ReturnType, + {}, + string + > authorId?: Resolver< - Client.Prisma.CreateManyPostAndReturnOutputType, + ReturnType, {}, number | null > createdAt?: Resolver< - Client.Prisma.CreateManyPostAndReturnOutputType, + ReturnType, {}, Date > updatedAt?: Resolver< - Client.Prisma.CreateManyPostAndReturnOutputType, + ReturnType, {}, Date > author?: Resolver< - Client.Prisma.CreateManyPostAndReturnOutputType, + ReturnType, CreateManyPostAndReturnOutputTypeAuthorArgs, Client.User | null > @@ -191,7 +207,7 @@ export type Mutation = { [key: string]: Resolver } & { createManyUserAndReturn?: Resolver< {}, CreateManyUserAndReturnArgs, - Client.Prisma.CreateManyUserAndReturnOutputType[] + ReturnType > deleteOneUser?: Resolver<{}, DeleteOneUserArgs, Client.User | null> updateOneUser?: Resolver<{}, UpdateOneUserArgs, Client.User | null> @@ -203,7 +219,7 @@ export type Mutation = { [key: string]: Resolver } & { createManyPostAndReturn?: Resolver< {}, CreateManyPostAndReturnArgs, - Client.Prisma.CreateManyPostAndReturnOutputType[] + ReturnType > deleteOnePost?: Resolver<{}, DeleteOnePostArgs, Client.Post | null> updateOnePost?: Resolver<{}, UpdateOnePostArgs, Client.Post | null>