From f125787946ed41ac5d7d097fa33608f27da14654 Mon Sep 17 00:00:00 2001 From: Daniel Cousens <413395+dcousens@users.noreply.github.com> Date: Thu, 15 Feb 2024 15:53:48 +1100 Subject: [PATCH] Always use `context.graphql.schema` for `createExpressServer` (#9029) --- .changeset/fix-create-express-app.md | 5 +++++ .changeset/less-extend-http-server.md | 2 +- .changeset/less-extend-type.md | 5 +++++ packages/core/src/admin-ui/templates/app.ts | 16 ++++++++-------- packages/core/src/lib/createExpressServer.ts | 7 +++---- packages/core/src/scripts/dev.ts | 13 ++++--------- packages/core/src/scripts/start.ts | 10 +++------- packages/core/src/types/config/index.ts | 5 ++--- .../live-reloading/schemas/second.ts | 6 ++++-- 9 files changed, 35 insertions(+), 34 deletions(-) create mode 100644 .changeset/fix-create-express-app.md create mode 100644 .changeset/less-extend-type.md diff --git a/.changeset/fix-create-express-app.md b/.changeset/fix-create-express-app.md new file mode 100644 index 00000000000..25d9adfbbdf --- /dev/null +++ b/.changeset/fix-create-express-app.md @@ -0,0 +1,5 @@ +---- +'@keystone-6/core': patch +---- + +Fixes `createExpressApp` to use `context.graphql.schema` rather than the GraphQLSchema argument, removing ambiguity in downstream usage diff --git a/.changeset/less-extend-http-server.md b/.changeset/less-extend-http-server.md index b059b08472c..0c798fbf64c 100644 --- a/.changeset/less-extend-http-server.md +++ b/.changeset/less-extend-http-server.md @@ -2,4 +2,4 @@ '@keystone-6/core': patch ---- -Deprecates `extendHttpServer`'s third argument, the graphqlSchema. Use `context.graphql.schema` +Deprecates `extendHttpServer`'s `graphqlSchema` argument; use `context.graphql.schema` now diff --git a/.changeset/less-extend-type.md b/.changeset/less-extend-type.md new file mode 100644 index 00000000000..484a3af5f50 --- /dev/null +++ b/.changeset/less-extend-type.md @@ -0,0 +1,5 @@ +--- +'@keystone-6/core': Patch +--- + +Deprecates `ExtendGraphQLSchema` type, use type `(schema: GraphQLSchema) => GraphQLSchema` instead diff --git a/packages/core/src/admin-ui/templates/app.ts b/packages/core/src/admin-ui/templates/app.ts index 304ceb69bf2..c0e3d15dc98 100644 --- a/packages/core/src/admin-ui/templates/app.ts +++ b/packages/core/src/admin-ui/templates/app.ts @@ -1,27 +1,27 @@ import hashString from '@emotion/hash' import { - executeSync, + type ExecutionResult, + type FragmentDefinitionNode, + type GraphQLSchema, + type SelectionNode, GraphQLNonNull, GraphQLScalarType, - type GraphQLSchema, GraphQLUnionType, - parse, - type FragmentDefinitionNode, - type SelectionNode, - type ExecutionResult, Kind, + executeSync, + parse, } from 'graphql' import { staticAdminMetaQuery, type StaticAdminMetaQuery } from '../admin-meta-graphql' import type { AdminMetaRootVal } from '../../lib/create-admin-meta' type AppTemplateOptions = { configFileExists: boolean } -export const appTemplate = ( +export function appTemplate ( adminMetaRootVal: AdminMetaRootVal, graphQLSchema: GraphQLSchema, { configFileExists }: AppTemplateOptions, apiPath: string -) => { +) { const result = executeSync({ document: staticAdminMetaQuery, schema: graphQLSchema, diff --git a/packages/core/src/lib/createExpressServer.ts b/packages/core/src/lib/createExpressServer.ts index 8f9e3833dc5..2c4f6a5ea36 100644 --- a/packages/core/src/lib/createExpressServer.ts +++ b/packages/core/src/lib/createExpressServer.ts @@ -5,7 +5,6 @@ import { expressMiddleware } from '@apollo/server/express4' import express from 'express' import { type GraphQLFormattedError, - type GraphQLSchema } from 'graphql' import { ApolloServer, type ApolloServerOptions } from '@apollo/server' import { ApolloServerPluginLandingPageDisabled } from '@apollo/server/plugin/disabled' @@ -49,7 +48,7 @@ function formatError (graphqlConfig: GraphQLConfig | undefined) { export async function createExpressServer ( config: Pick, - graphQLSchema: GraphQLSchema, // TODO: redundant, prefer context.graphql.schema, remove in breaking change + _: any, // TODO: uses context.graphql.schema now, remove in breaking change context: KeystoneContext ): Promise<{ expressServer: express.Express @@ -85,7 +84,7 @@ export async function createExpressServer ( } await config.server?.extendExpressApp?.(expressServer, context) - await config.server?.extendHttpServer?.(httpServer, context, graphQLSchema) + await config.server?.extendHttpServer?.(httpServer, context, context.graphql.schema) if (config.storage) { for (const val of Object.values(config.storage)) { @@ -117,7 +116,7 @@ export async function createExpressServer ( includeStacktraceInErrorResponses: config.graphql?.debug, ...apolloConfig, - schema: graphQLSchema, + schema: context.graphql.schema, plugins: playgroundOption === 'apollo' ? apolloConfig?.plugins diff --git a/packages/core/src/scripts/dev.ts b/packages/core/src/scripts/dev.ts index 00460f69887..4a02a998e6e 100644 --- a/packages/core/src/scripts/dev.ts +++ b/packages/core/src/scripts/dev.ts @@ -204,7 +204,7 @@ export async function dev ( } } - const { graphQLSchema, getKeystone, adminMeta } = createSystem(newConfig) + const { getKeystone, graphQLSchema, adminMeta } = createSystem(newConfig) // we're not using generateCommittedArtifacts or any of the similar functions // because we will never need to write a new prisma schema here // and formatting the prisma schema leaves some listeners on the process @@ -222,16 +222,11 @@ export async function dev ( await generateAdminUI(newConfig, graphQLSchema, adminMeta, paths.admin, true) if (prismaClientModule) { if (server && lastApolloServer) { - const keystone = getKeystone({ - PrismaClient: function fakePrismaClientClass () { - return prismaClient - } as unknown as new (args: unknown) => any, - Prisma: prismaClientModule.Prisma, - }) - const servers = await createExpressServer(newConfig, graphQLSchema, keystone.context) + const { context: newContext } = getKeystone(prismaClientModule) + const servers = await createExpressServer(newConfig, null, newContext) if (nextApp) { servers.expressServer.use( - createAdminUIMiddlewareWithNextApp(newConfig, keystone.context, nextApp) + createAdminUIMiddlewareWithNextApp(newConfig, newContext, nextApp) ) } expressServer = servers.expressServer diff --git a/packages/core/src/scripts/start.ts b/packages/core/src/scripts/start.ts index acbe6a20357..40401fba76d 100644 --- a/packages/core/src/scripts/start.ts +++ b/packages/core/src/scripts/start.ts @@ -1,7 +1,7 @@ import fs from 'node:fs/promises' import type { ListenOptions } from 'node:net' import next from 'next' -import { createSystem } from '../lib/createSystem' +import { createSystem } from '../system' import { createExpressServer } from '../lib/createExpressServer' import { createAdminUIMiddlewareWithNextApp } from '../lib/createAdminUIMiddleware' import { @@ -30,7 +30,7 @@ export async function start ( const config = getBuiltKeystoneConfiguration(cwd) const paths = getSystemPaths(cwd, config) - const { getKeystone, graphQLSchema } = createSystem(config) + const { getKeystone } = createSystem(config) const prismaClient = require(paths.prisma) const keystone = getKeystone(prismaClient) @@ -43,11 +43,7 @@ export async function start ( await keystone.connect() console.log('✨ Creating server') - const { expressServer, httpServer } = await createExpressServer( - config, - graphQLSchema, - keystone.context - ) + const { expressServer, httpServer } = await createExpressServer(config, null, keystone.context) console.log(`✅ GraphQL API ready`) if (!config.ui?.isDisabled && ui) { diff --git a/packages/core/src/types/config/index.ts b/packages/core/src/types/config/index.ts index cd108841a0c..2c746b67018 100644 --- a/packages/core/src/types/config/index.ts +++ b/packages/core/src/types/config/index.ts @@ -105,7 +105,7 @@ export type KeystoneConfig GraphQLSchema /** An object containing configuration about keystone's various external storages. * * Each entry should be of either `kind: 'local'` or `kind: 's3'`, and follow the configuration of each. @@ -293,8 +293,7 @@ export type GraphQLConfig GraphQLSchema export type FilesConfig = { diff --git a/tests/test-projects/live-reloading/schemas/second.ts b/tests/test-projects/live-reloading/schemas/second.ts index 2551c90e1a5..0a1d59a6b62 100644 --- a/tests/test-projects/live-reloading/schemas/second.ts +++ b/tests/test-projects/live-reloading/schemas/second.ts @@ -2,6 +2,8 @@ import { graphql, list } from '@keystone-6/core' import { allowAll } from '@keystone-6/core/access' import { text, virtual } from '@keystone-6/core/fields' +import type { Lists } from '.keystone/types' + export const lists = { Something: list({ access: allowAll, @@ -11,13 +13,13 @@ export const lists = { field: graphql.field({ type: graphql.String, resolve (item) { - return (item as { text: string }).text + return item.text }, }), }), }, }), -} +} satisfies Lists export const extendGraphqlSchema = graphql.extend(() => { return {