From 722a6b573dfd6c72a2eaaf6849e5ef5436c1c616 Mon Sep 17 00:00:00 2001 From: Daniel Cousens <413395+dcousens@users.noreply.github.com> Date: Thu, 4 Apr 2024 14:43:34 +1100 Subject: [PATCH] inline listTypeInfoName and other loops --- examples/custom-output-paths/my-types.ts | 1 - .../keystone-types.ts | 3 - .../core/src/lib/typescript-schema-printer.ts | 70 +++++++++---------- 3 files changed, 33 insertions(+), 41 deletions(-) diff --git a/examples/custom-output-paths/my-types.ts b/examples/custom-output-paths/my-types.ts index 27b84cde948..e32cd663078 100644 --- a/examples/custom-output-paths/my-types.ts +++ b/examples/custom-output-paths/my-types.ts @@ -138,7 +138,6 @@ type ResolvedPostCreateInput = { content?: import('./node_modules/.myprisma/client').Prisma.PostCreateInput['content'] publishDate?: import('./node_modules/.myprisma/client').Prisma.PostCreateInput['publishDate'] } - type ResolvedPostUpdateInput = { id?: undefined title?: import('./node_modules/.myprisma/client').Prisma.PostUpdateInput['title'] diff --git a/examples/extend-graphql-schema-nexus/keystone-types.ts b/examples/extend-graphql-schema-nexus/keystone-types.ts index d17d3971b2a..3c342137433 100644 --- a/examples/extend-graphql-schema-nexus/keystone-types.ts +++ b/examples/extend-graphql-schema-nexus/keystone-types.ts @@ -220,7 +220,6 @@ type ResolvedPostCreateInput = { publishDate?: import('./node_modules/.myprisma/client').Prisma.PostCreateInput['publishDate'] author?: import('./node_modules/.myprisma/client').Prisma.PostCreateInput['author'] } - type ResolvedPostUpdateInput = { id?: undefined title?: import('./node_modules/.myprisma/client').Prisma.PostUpdateInput['title'] @@ -229,13 +228,11 @@ type ResolvedPostUpdateInput = { publishDate?: import('./node_modules/.myprisma/client').Prisma.PostUpdateInput['publishDate'] author?: import('./node_modules/.myprisma/client').Prisma.PostUpdateInput['author'] } - type ResolvedAuthorCreateInput = { id?: import('./node_modules/.myprisma/client').Prisma.AuthorCreateInput['id'] name?: import('./node_modules/.myprisma/client').Prisma.AuthorCreateInput['name'] posts?: import('./node_modules/.myprisma/client').Prisma.AuthorCreateInput['posts'] } - type ResolvedAuthorUpdateInput = { id?: undefined name?: import('./node_modules/.myprisma/client').Prisma.AuthorUpdateInput['name'] diff --git a/packages/core/src/lib/typescript-schema-printer.ts b/packages/core/src/lib/typescript-schema-printer.ts index 65c5e7ff89f..4efa2959b97 100644 --- a/packages/core/src/lib/typescript-schema-printer.ts +++ b/packages/core/src/lib/typescript-schema-printer.ts @@ -186,42 +186,8 @@ export function printGeneratedTypes ( graphQLSchema: GraphQLSchema, lists: Record ) { - const interimCreateUpdateTypes = [] - const listsTypeInfo = [] - const listsNamespaces = [] prismaClientPath = stringify(prismaClientPath).replace(/'/g, `\\'`) - for (const [listKey, list] of Object.entries(lists)) { - const listTypeInfoName = `Lists.${listKey}.TypeInfo` - - if (list.graphql.isEnabled.create) { - interimCreateUpdateTypes.push( - printInterimType( - prismaClientPath, - list, - listKey, - list.graphql.names.createInputName, - 'Create' - ) - ) - } - - if (list.graphql.isEnabled.update) { - interimCreateUpdateTypes.push( - printInterimType( - prismaClientPath, - list, - listKey, - list.graphql.names.updateInputName, - 'Update' - ) - ) - } - - listsTypeInfo.push(` readonly ${listKey}: ${listTypeInfoName}`) - listsNamespaces.push(printListTypeInfo(prismaClientPath, listKey, list)) - } - return [ '/* eslint-disable */', '', @@ -235,17 +201,47 @@ export function printGeneratedTypes ( Decimal: `import('@keystone-6/core/types').Decimal | string`, }), '', - interimCreateUpdateTypes.join('\n\n'), + ...(function* () { + for (const [listKey, list] of Object.entries(lists)) { + if (list.graphql.isEnabled.create) { + yield printInterimType( + prismaClientPath, + list, + listKey, + list.graphql.names.createInputName, + 'Create' + ) + } + + if (list.graphql.isEnabled.update) { + yield printInterimType( + prismaClientPath, + list, + listKey, + list.graphql.names.updateInputName, + 'Update' + ) + } + } + }()), '', 'export declare namespace Lists {', - ...listsNamespaces, + ...(function* () { + for (const [listKey, list] of Object.entries(lists)) { + yield printListTypeInfo(prismaClientPath, listKey, list) + } + })(), '}', `export type Context = import('@keystone-6/core/types').KeystoneContext>`, `export type Config = import('@keystone-6/core/types').KeystoneConfig>`, '', 'export type TypeInfo = {', ` lists: {`, - ...listsTypeInfo, + ...(function* () { + for (const listKey in lists) { + yield ` readonly ${listKey}: Lists.${listKey}.TypeInfo` + } + })(), ` }`, ` prisma: import('${prismaClientPath}').PrismaClient`, ` session: Session`,