Skip to content

Commit

Permalink
Fix CreateManyAndReturn type
Browse files Browse the repository at this point in the history
  • Loading branch information
f8k8 committed Jul 1, 2024
1 parent c69b64d commit 29f3e65
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
31 changes: 22 additions & 9 deletions packages/generator/src/sdl/GenerateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Client.Prisma.${innerType}Delegate["createManyAndReturn"]>`;
}
return `Client.Prisma.${name}`;
}

capital(name: string) {
return name.charAt(0).toUpperCase() + name.slice(1);
}
Expand All @@ -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<Client.Prisma.${innerType}Delegate["createManyAndReturn"]>`;
}
const type =
options.type.toString() === 'AffectedRowsOutput'
? 'Prisma.BatchPayload'
Expand Down Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,33 @@ export type Post = { [key: string]: Resolver<any, any, any> } & {
export type CreateManyUserAndReturnOutputType = {
[key: string]: Resolver<any, any, any>
} & {
id?: Resolver<Client.Prisma.CreateManyUserAndReturnOutputType, {}, number>
id?: Resolver<
ReturnType<Client.Prisma.UserDelegate['createManyAndReturn']>,
{},
number
>
createdAt?: Resolver<
Client.Prisma.CreateManyUserAndReturnOutputType,
ReturnType<Client.Prisma.UserDelegate['createManyAndReturn']>,
{},
Date
>
email?: Resolver<Client.Prisma.CreateManyUserAndReturnOutputType, {}, string>
email?: Resolver<
ReturnType<Client.Prisma.UserDelegate['createManyAndReturn']>,
{},
string
>
name?: Resolver<
Client.Prisma.CreateManyUserAndReturnOutputType,
ReturnType<Client.Prisma.UserDelegate['createManyAndReturn']>,
{},
string | null
>
password?: Resolver<
Client.Prisma.CreateManyUserAndReturnOutputType,
ReturnType<Client.Prisma.UserDelegate['createManyAndReturn']>,
{},
string
>
permissions?: Resolver<
Client.Prisma.CreateManyUserAndReturnOutputType,
ReturnType<Client.Prisma.UserDelegate['createManyAndReturn']>,
{},
any
>
Expand All @@ -104,30 +112,38 @@ export type CreateManyUserAndReturnOutputType = {
export type CreateManyPostAndReturnOutputType = {
[key: string]: Resolver<any, any, any>
} & {
id?: Resolver<Client.Prisma.CreateManyPostAndReturnOutputType, {}, number>
id?: Resolver<
ReturnType<Client.Prisma.PostDelegate['createManyAndReturn']>,
{},
number
>
published?: Resolver<
Client.Prisma.CreateManyPostAndReturnOutputType,
ReturnType<Client.Prisma.PostDelegate['createManyAndReturn']>,
{},
boolean
>
title?: Resolver<Client.Prisma.CreateManyPostAndReturnOutputType, {}, string>
title?: Resolver<
ReturnType<Client.Prisma.PostDelegate['createManyAndReturn']>,
{},
string
>
authorId?: Resolver<
Client.Prisma.CreateManyPostAndReturnOutputType,
ReturnType<Client.Prisma.PostDelegate['createManyAndReturn']>,
{},
number | null
>
createdAt?: Resolver<
Client.Prisma.CreateManyPostAndReturnOutputType,
ReturnType<Client.Prisma.PostDelegate['createManyAndReturn']>,
{},
Date
>
updatedAt?: Resolver<
Client.Prisma.CreateManyPostAndReturnOutputType,
ReturnType<Client.Prisma.PostDelegate['createManyAndReturn']>,
{},
Date
>
author?: Resolver<
Client.Prisma.CreateManyPostAndReturnOutputType,
ReturnType<Client.Prisma.PostDelegate['createManyAndReturn']>,
CreateManyPostAndReturnOutputTypeAuthorArgs,
Client.User | null
>
Expand Down Expand Up @@ -191,7 +207,7 @@ export type Mutation = { [key: string]: Resolver<any, any, any> } & {
createManyUserAndReturn?: Resolver<
{},
CreateManyUserAndReturnArgs,
Client.Prisma.CreateManyUserAndReturnOutputType[]
ReturnType<Client.Prisma.UserDelegate['createManyAndReturn']>
>
deleteOneUser?: Resolver<{}, DeleteOneUserArgs, Client.User | null>
updateOneUser?: Resolver<{}, UpdateOneUserArgs, Client.User | null>
Expand All @@ -203,7 +219,7 @@ export type Mutation = { [key: string]: Resolver<any, any, any> } & {
createManyPostAndReturn?: Resolver<
{},
CreateManyPostAndReturnArgs,
Client.Prisma.CreateManyPostAndReturnOutputType[]
ReturnType<Client.Prisma.PostDelegate['createManyAndReturn']>
>
deleteOnePost?: Resolver<{}, DeleteOnePostArgs, Client.Post | null>
updateOnePost?: Resolver<{}, UpdateOnePostArgs, Client.Post | null>
Expand Down

0 comments on commit 29f3e65

Please sign in to comment.