Skip to content

Commit f125787

Browse files
authored
Always use context.graphql.schema for createExpressServer (#9029)
1 parent 18eed93 commit f125787

File tree

9 files changed

+35
-34
lines changed

9 files changed

+35
-34
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
----
2+
'@keystone-6/core': patch
3+
----
4+
5+
Fixes `createExpressApp` to use `context.graphql.schema` rather than the GraphQLSchema argument, removing ambiguity in downstream usage

.changeset/less-extend-http-server.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'@keystone-6/core': patch
33
----
44

5-
Deprecates `extendHttpServer`'s third argument, the graphqlSchema. Use `context.graphql.schema`
5+
Deprecates `extendHttpServer`'s `graphqlSchema` argument; use `context.graphql.schema` now

.changeset/less-extend-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@keystone-6/core': Patch
3+
---
4+
5+
Deprecates `ExtendGraphQLSchema` type, use type `(schema: GraphQLSchema) => GraphQLSchema` instead

packages/core/src/admin-ui/templates/app.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import hashString from '@emotion/hash'
22
import {
3-
executeSync,
3+
type ExecutionResult,
4+
type FragmentDefinitionNode,
5+
type GraphQLSchema,
6+
type SelectionNode,
47
GraphQLNonNull,
58
GraphQLScalarType,
6-
type GraphQLSchema,
79
GraphQLUnionType,
8-
parse,
9-
type FragmentDefinitionNode,
10-
type SelectionNode,
11-
type ExecutionResult,
1210
Kind,
11+
executeSync,
12+
parse,
1313
} from 'graphql'
1414
import { staticAdminMetaQuery, type StaticAdminMetaQuery } from '../admin-meta-graphql'
1515
import type { AdminMetaRootVal } from '../../lib/create-admin-meta'
1616

1717
type AppTemplateOptions = { configFileExists: boolean }
1818

19-
export const appTemplate = (
19+
export function appTemplate (
2020
adminMetaRootVal: AdminMetaRootVal,
2121
graphQLSchema: GraphQLSchema,
2222
{ configFileExists }: AppTemplateOptions,
2323
apiPath: string
24-
) => {
24+
) {
2525
const result = executeSync({
2626
document: staticAdminMetaQuery,
2727
schema: graphQLSchema,

packages/core/src/lib/createExpressServer.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { expressMiddleware } from '@apollo/server/express4'
55
import express from 'express'
66
import {
77
type GraphQLFormattedError,
8-
type GraphQLSchema
98
} from 'graphql'
109
import { ApolloServer, type ApolloServerOptions } from '@apollo/server'
1110
import { ApolloServerPluginLandingPageDisabled } from '@apollo/server/plugin/disabled'
@@ -49,7 +48,7 @@ function formatError (graphqlConfig: GraphQLConfig | undefined) {
4948

5049
export async function createExpressServer (
5150
config: Pick<KeystoneConfig, 'graphql' | 'server' | 'storage'>,
52-
graphQLSchema: GraphQLSchema, // TODO: redundant, prefer context.graphql.schema, remove in breaking change
51+
_: any, // TODO: uses context.graphql.schema now, remove in breaking change
5352
context: KeystoneContext
5453
): Promise<{
5554
expressServer: express.Express
@@ -85,7 +84,7 @@ export async function createExpressServer (
8584
}
8685

8786
await config.server?.extendExpressApp?.(expressServer, context)
88-
await config.server?.extendHttpServer?.(httpServer, context, graphQLSchema)
87+
await config.server?.extendHttpServer?.(httpServer, context, context.graphql.schema)
8988

9089
if (config.storage) {
9190
for (const val of Object.values(config.storage)) {
@@ -117,7 +116,7 @@ export async function createExpressServer (
117116
includeStacktraceInErrorResponses: config.graphql?.debug,
118117

119118
...apolloConfig,
120-
schema: graphQLSchema,
119+
schema: context.graphql.schema,
121120
plugins:
122121
playgroundOption === 'apollo'
123122
? apolloConfig?.plugins

packages/core/src/scripts/dev.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export async function dev (
204204
}
205205
}
206206

207-
const { graphQLSchema, getKeystone, adminMeta } = createSystem(newConfig)
207+
const { getKeystone, graphQLSchema, adminMeta } = createSystem(newConfig)
208208
// we're not using generateCommittedArtifacts or any of the similar functions
209209
// because we will never need to write a new prisma schema here
210210
// and formatting the prisma schema leaves some listeners on the process
@@ -222,16 +222,11 @@ export async function dev (
222222
await generateAdminUI(newConfig, graphQLSchema, adminMeta, paths.admin, true)
223223
if (prismaClientModule) {
224224
if (server && lastApolloServer) {
225-
const keystone = getKeystone({
226-
PrismaClient: function fakePrismaClientClass () {
227-
return prismaClient
228-
} as unknown as new (args: unknown) => any,
229-
Prisma: prismaClientModule.Prisma,
230-
})
231-
const servers = await createExpressServer(newConfig, graphQLSchema, keystone.context)
225+
const { context: newContext } = getKeystone(prismaClientModule)
226+
const servers = await createExpressServer(newConfig, null, newContext)
232227
if (nextApp) {
233228
servers.expressServer.use(
234-
createAdminUIMiddlewareWithNextApp(newConfig, keystone.context, nextApp)
229+
createAdminUIMiddlewareWithNextApp(newConfig, newContext, nextApp)
235230
)
236231
}
237232
expressServer = servers.expressServer

packages/core/src/scripts/start.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'node:fs/promises'
22
import type { ListenOptions } from 'node:net'
33
import next from 'next'
4-
import { createSystem } from '../lib/createSystem'
4+
import { createSystem } from '../system'
55
import { createExpressServer } from '../lib/createExpressServer'
66
import { createAdminUIMiddlewareWithNextApp } from '../lib/createAdminUIMiddleware'
77
import {
@@ -30,7 +30,7 @@ export async function start (
3030

3131
const config = getBuiltKeystoneConfiguration(cwd)
3232
const paths = getSystemPaths(cwd, config)
33-
const { getKeystone, graphQLSchema } = createSystem(config)
33+
const { getKeystone } = createSystem(config)
3434
const prismaClient = require(paths.prisma)
3535
const keystone = getKeystone(prismaClient)
3636

@@ -43,11 +43,7 @@ export async function start (
4343
await keystone.connect()
4444

4545
console.log('✨ Creating server')
46-
const { expressServer, httpServer } = await createExpressServer(
47-
config,
48-
graphQLSchema,
49-
keystone.context
50-
)
46+
const { expressServer, httpServer } = await createExpressServer(config, null, keystone.context)
5147

5248
console.log(`✅ GraphQL API ready`)
5349
if (!config.ui?.isDisabled && ui) {

packages/core/src/types/config/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export type KeystoneConfig<TypeInfo extends BaseKeystoneTypeInfo = BaseKeystoneT
105105
}
106106

107107
// TODO: why isn't this within .graphql?
108-
extendGraphqlSchema?: ExtendGraphqlSchema
108+
extendGraphqlSchema?: (schema: GraphQLSchema) => GraphQLSchema
109109
/** An object containing configuration about keystone's various external storages.
110110
*
111111
* Each entry should be of either `kind: 'local'` or `kind: 's3'`, and follow the configuration of each.
@@ -293,8 +293,7 @@ export type GraphQLConfig<TypeInfo extends BaseKeystoneTypeInfo = BaseKeystoneTy
293293
schemaPath?: string
294294
}
295295

296-
// config.extendGraphqlSchema
297-
296+
/** @deprecated */
298297
export type ExtendGraphqlSchema = (schema: GraphQLSchema) => GraphQLSchema
299298

300299
export type FilesConfig = {

tests/test-projects/live-reloading/schemas/second.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { graphql, list } from '@keystone-6/core'
22
import { allowAll } from '@keystone-6/core/access'
33
import { text, virtual } from '@keystone-6/core/fields'
44

5+
import type { Lists } from '.keystone/types'
6+
57
export const lists = {
68
Something: list({
79
access: allowAll,
@@ -11,13 +13,13 @@ export const lists = {
1113
field: graphql.field({
1214
type: graphql.String,
1315
resolve (item) {
14-
return (item as { text: string }).text
16+
return item.text
1517
},
1618
}),
1719
}),
1820
},
1921
}),
20-
}
22+
} satisfies Lists
2123

2224
export const extendGraphqlSchema = graphql.extend(() => {
2325
return {

0 commit comments

Comments
 (0)