Skip to content

Commit

Permalink
Always use context.graphql.schema for createExpressServer (keysto…
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens authored Feb 15, 2024
1 parent 18eed93 commit f125787
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/fix-create-express-app.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion .changeset/less-extend-http-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions .changeset/less-extend-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': Patch
---

Deprecates `ExtendGraphQLSchema` type, use type `(schema: GraphQLSchema) => GraphQLSchema` instead
16 changes: 8 additions & 8 deletions packages/core/src/admin-ui/templates/app.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/lib/createExpressServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -49,7 +48,7 @@ function formatError (graphqlConfig: GraphQLConfig | undefined) {

export async function createExpressServer (
config: Pick<KeystoneConfig, 'graphql' | 'server' | 'storage'>,
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
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -117,7 +116,7 @@ export async function createExpressServer (
includeStacktraceInErrorResponses: config.graphql?.debug,

...apolloConfig,
schema: graphQLSchema,
schema: context.graphql.schema,
plugins:
playgroundOption === 'apollo'
? apolloConfig?.plugins
Expand Down
13 changes: 4 additions & 9 deletions packages/core/src/scripts/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 3 additions & 7 deletions packages/core/src/scripts/start.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)

Expand All @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export type KeystoneConfig<TypeInfo extends BaseKeystoneTypeInfo = BaseKeystoneT
}

// TODO: why isn't this within .graphql?
extendGraphqlSchema?: ExtendGraphqlSchema
extendGraphqlSchema?: (schema: GraphQLSchema) => 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.
Expand Down Expand Up @@ -293,8 +293,7 @@ export type GraphQLConfig<TypeInfo extends BaseKeystoneTypeInfo = BaseKeystoneTy
schemaPath?: string
}

// config.extendGraphqlSchema

/** @deprecated */
export type ExtendGraphqlSchema = (schema: GraphQLSchema) => GraphQLSchema

export type FilesConfig = {
Expand Down
6 changes: 4 additions & 2 deletions tests/test-projects/live-reloading/schemas/second.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit f125787

Please sign in to comment.