From e6196a76dc4e41303bc6cc5232d08d6cb48e11d4 Mon Sep 17 00:00:00 2001 From: Daniel Cousens <413395+dcousens@users.noreply.github.com> Date: Thu, 28 Mar 2024 15:40:20 +1100 Subject: [PATCH] Move config.extendGraphqlSchema to config.graphql.extendSchema --- .changeset/move-extend-graphql.md | 5 ++ .../keystone.ts | 6 +- .../keystone.ts | 4 +- .../extend-graphql-schema-nexus/keystone.ts | 5 +- .../extend-graphql-subscriptions/keystone.ts | 4 +- packages/auth/src/index.ts | 12 ++-- packages/core/src/lib/createGraphQLSchema.ts | 6 +- packages/core/src/lib/defaults.ts | 5 +- packages/core/src/types/config/index.ts | 8 ++- tests/api-tests/extend-graphql-schema.test.ts | 48 ++++++++------- tests/api-tests/queries/cache-hints.test.ts | 60 ++++++++++--------- .../test-projects/live-reloading/keystone.ts | 4 +- 12 files changed, 94 insertions(+), 73 deletions(-) create mode 100644 .changeset/move-extend-graphql.md diff --git a/.changeset/move-extend-graphql.md b/.changeset/move-extend-graphql.md new file mode 100644 index 00000000000..43893693331 --- /dev/null +++ b/.changeset/move-extend-graphql.md @@ -0,0 +1,5 @@ +--- +'@keystone-6/core': major +--- + +Moves `config.extendGraphqlSchema` to `config.graphql.extendGraphqlSchema`, similar to `db.extendPrismaSchema` diff --git a/examples/extend-graphql-schema-graphql-tools/keystone.ts b/examples/extend-graphql-schema-graphql-tools/keystone.ts index bf93b6dfe61..3051fe226d2 100644 --- a/examples/extend-graphql-schema-graphql-tools/keystone.ts +++ b/examples/extend-graphql-schema-graphql-tools/keystone.ts @@ -10,6 +10,8 @@ export default config({ // WARNING: this is only needed for our monorepo examples, dont do this ...fixPrismaPath, }, + graphql: { + extendGraphqlSchema + }, lists, - extendGraphqlSchema, -}) +}) \ No newline at end of file diff --git a/examples/extend-graphql-schema-graphql-ts/keystone.ts b/examples/extend-graphql-schema-graphql-ts/keystone.ts index bf93b6dfe61..876de997ec1 100644 --- a/examples/extend-graphql-schema-graphql-ts/keystone.ts +++ b/examples/extend-graphql-schema-graphql-ts/keystone.ts @@ -10,6 +10,8 @@ export default config({ // WARNING: this is only needed for our monorepo examples, dont do this ...fixPrismaPath, }, + graphql: { + extendGraphqlSchema + }, lists, - extendGraphqlSchema, }) diff --git a/examples/extend-graphql-schema-nexus/keystone.ts b/examples/extend-graphql-schema-nexus/keystone.ts index 30eb684df35..063c3ee5720 100644 --- a/examples/extend-graphql-schema-nexus/keystone.ts +++ b/examples/extend-graphql-schema-nexus/keystone.ts @@ -10,9 +10,10 @@ export default config({ // WARNING: this is only needed for our monorepo examples, dont do this ...fixPrismaPath, }, + graphql: { + extendGraphqlSchema + }, lists, - extendGraphqlSchema, - // we use a custom types path for easy integration with nexus types: { path: 'keystone-types.ts', diff --git a/examples/extend-graphql-subscriptions/keystone.ts b/examples/extend-graphql-subscriptions/keystone.ts index 7f3ed1dd57c..d8b83b33745 100644 --- a/examples/extend-graphql-subscriptions/keystone.ts +++ b/examples/extend-graphql-subscriptions/keystone.ts @@ -11,9 +11,11 @@ export default config({ // WARNING: this is only needed for our monorepo examples, dont do this ...fixPrismaPath, }, + graphql: { + extendGraphqlSchema + }, lists, server: { extendHttpServer, }, - extendGraphqlSchema, }) diff --git a/packages/auth/src/index.ts b/packages/auth/src/index.ts index 08b8ff8d3d9..6ce97015ca9 100644 --- a/packages/auth/src/index.ts +++ b/packages/auth/src/index.ts @@ -279,11 +279,18 @@ export function createAuth ({ if (!config.session) throw new TypeError('Missing .session configuration') - const { extendGraphqlSchema = defaultExtendGraphqlSchema } = config + const { graphql } = config + const { extendGraphqlSchema = defaultExtendGraphqlSchema } = graphql ?? {} const authListConfig = config.lists[listKey] return { ...config, + graphql: { + ...config.graphql, + extendGraphqlSchema: (schema) => { + return extendGraphqlSchema(authExtendGraphqlSchema(schema)) + }, + }, ui, session: authSessionStrategy(config.session), lists: { @@ -296,9 +303,6 @@ export function createAuth ({ }, }, }, - extendGraphqlSchema: schema => { - return extendGraphqlSchema(authExtendGraphqlSchema(schema)) - }, } } diff --git a/packages/core/src/lib/createGraphQLSchema.ts b/packages/core/src/lib/createGraphQLSchema.ts index 0c611ab3b19..9631f385217 100644 --- a/packages/core/src/lib/createGraphQLSchema.ts +++ b/packages/core/src/lib/createGraphQLSchema.ts @@ -127,9 +127,5 @@ export function createGraphQLSchema ( ) // merge in the user defined graphQL API - if (config.extendGraphqlSchema) { - return config.extendGraphqlSchema(graphQLSchema) - } - - return graphQLSchema + return config.graphql?.extendGraphqlSchema?.(graphQLSchema) ?? graphQLSchema } diff --git a/packages/core/src/lib/defaults.ts b/packages/core/src/lib/defaults.ts index 78bd5e0d6c3..fdc06c4ae46 100644 --- a/packages/core/src/lib/defaults.ts +++ b/packages/core/src/lib/defaults.ts @@ -102,6 +102,7 @@ export function resolveDefaults (config: KeystoneConfig) { path: '/api/graphql', playground: process.env.NODE_ENV !== 'production', schemaPath: 'schema.graphql', + extendGraphqlSchema: config.graphql?.extendGraphqlSchema ?? ((s) => s), ...config.graphql, }, lists: injectDefaults(config, defaultIdField), @@ -112,8 +113,6 @@ export function resolveDefaults (config: KeystoneConfig) { ...config.server, cors, }, - // TODO: remove in breaking change, move to .graphql.extendSchema - extendGraphqlSchema: config.extendGraphqlSchema ?? ((s) => s), storage: { ...config?.storage }, @@ -128,4 +127,4 @@ export function resolveDefaults (config: KeystoneConfig) { } satisfies KeystoneConfig } -export type ResolvedKeystoneConfig = ReturnType \ No newline at end of file +export type ResolvedKeystoneConfig = ReturnType diff --git a/packages/core/src/types/config/index.ts b/packages/core/src/types/config/index.ts index 9e6c8b10cd6..455c5a9754c 100644 --- a/packages/core/src/types/config/index.ts +++ b/packages/core/src/types/config/index.ts @@ -103,8 +103,6 @@ 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. @@ -270,6 +268,12 @@ export type GraphQLConfig GraphQLSchema } export type FilesConfig = { diff --git a/tests/api-tests/extend-graphql-schema.test.ts b/tests/api-tests/extend-graphql-schema.test.ts index b9f4dadfb45..f1ceeae6dee 100644 --- a/tests/api-tests/extend-graphql-schema.test.ts +++ b/tests/api-tests/extend-graphql-schema.test.ts @@ -32,29 +32,31 @@ const runner = setupTestRunner({ fields: { name: text() }, }), }, - extendGraphqlSchema: graphql.extend(() => { - return { - mutation: { - triple: graphql.field({ - type: graphql.Int, - args: { x: graphql.arg({ type: graphql.nonNull(graphql.Int) }) }, - resolve: withAccessCheck(true, (_, { x }: { x: number }) => 3 * x), - }), - }, - query: { - double: graphql.field({ - type: graphql.Int, - args: { x: graphql.arg({ type: graphql.nonNull(graphql.Int) }) }, - resolve: withAccessCheck(true, (_, { x }: { x: number }) => 2 * x), - }), - quads: graphql.field({ - type: graphql.Int, - args: { x: graphql.arg({ type: graphql.nonNull(graphql.Int) }) }, - resolve: withAccessCheck(falseFn, (_, { x }: { x: number }) => 4 * x), - }), - }, - } - }) + graphql: { + extendGraphqlSchema: graphql.extend(() => { + return { + mutation: { + triple: graphql.field({ + type: graphql.Int, + args: { x: graphql.arg({ type: graphql.nonNull(graphql.Int) }) }, + resolve: withAccessCheck(true, (_, { x }: { x: number }) => 3 * x), + }), + }, + query: { + double: graphql.field({ + type: graphql.Int, + args: { x: graphql.arg({ type: graphql.nonNull(graphql.Int) }) }, + resolve: withAccessCheck(true, (_, { x }: { x: number }) => 2 * x), + }), + quads: graphql.field({ + type: graphql.Int, + args: { x: graphql.arg({ type: graphql.nonNull(graphql.Int) }) }, + resolve: withAccessCheck(falseFn, (_, { x }: { x: number }) => 4 * x), + }), + }, + } + }) + } }), }) diff --git a/tests/api-tests/queries/cache-hints.test.ts b/tests/api-tests/queries/cache-hints.test.ts index 69d3b3a36bc..9ed2aaa658b 100644 --- a/tests/api-tests/queries/cache-hints.test.ts +++ b/tests/api-tests/queries/cache-hints.test.ts @@ -44,35 +44,37 @@ const runner = setupTestRunner({ }, }), }, - extendGraphqlSchema: graphql.extend(() => { - const MyType = graphql.object<{ original: number }>()({ - name: 'MyType', - fields: { - original: graphql.field({ type: graphql.Int }), - double: graphql.field({ type: graphql.Int, resolve: ({ original }) => original * 2 }), - }, - }) - return { - query: { - double: graphql.field({ - type: MyType, - args: { x: graphql.arg({ type: graphql.nonNull(graphql.Int) }) }, - resolve: (_, { x }, context, info) => { - maybeCacheControlFromInfo(info)?.setCacheHint({ maxAge: 100, scope: 'PUBLIC' }) - return { original: x, double: x * 2 } - }, - }), - }, - mutation: { - triple: graphql.field({ - type: graphql.Int, - args: { x: graphql.arg({ type: graphql.nonNull(graphql.Int) }) }, - resolve: (_, { x }) => x * 3, - }), - }, - } - }), - }, + graphql: { + extendGraphqlSchema: graphql.extend(() => { + const MyType = graphql.object<{ original: number }>()({ + name: 'MyType', + fields: { + original: graphql.field({ type: graphql.Int }), + double: graphql.field({ type: graphql.Int, resolve: ({ original }) => original * 2 }), + }, + }) + return { + query: { + double: graphql.field({ + type: MyType, + args: { x: graphql.arg({ type: graphql.nonNull(graphql.Int) }) }, + resolve: (_, { x }, context, info) => { + maybeCacheControlFromInfo(info)?.setCacheHint({ maxAge: 100, scope: 'PUBLIC' }) + return { original: x, double: x * 2 } + }, + }), + }, + mutation: { + triple: graphql.field({ + type: graphql.Int, + args: { x: graphql.arg({ type: graphql.nonNull(graphql.Int) }) }, + resolve: (_, { x }) => x * 3, + }), + }, + } + }), + } + } }) const addFixtures = async (context: ContextFromRunner) => { diff --git a/tests/test-projects/live-reloading/keystone.ts b/tests/test-projects/live-reloading/keystone.ts index d19cef83f2c..62b86624edb 100644 --- a/tests/test-projects/live-reloading/keystone.ts +++ b/tests/test-projects/live-reloading/keystone.ts @@ -11,7 +11,9 @@ export default config({ ...fixPrismaPath, }, lists, - extendGraphqlSchema, + graphql: { + extendGraphqlSchema + }, ui: { getAdditionalFiles: [ () => [