Skip to content

Commit

Permalink
Move config.extendGraphqlSchema to config.graphql.extendSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Apr 4, 2024
1 parent 340fd8c commit e6196a7
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 73 deletions.
5 changes: 5 additions & 0 deletions .changeset/move-extend-graphql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': major
---

Moves `config.extendGraphqlSchema` to `config.graphql.extendGraphqlSchema`, similar to `db.extendPrismaSchema`
6 changes: 4 additions & 2 deletions examples/extend-graphql-schema-graphql-tools/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export default config({
// WARNING: this is only needed for our monorepo examples, dont do this
...fixPrismaPath,
},
graphql: {
extendGraphqlSchema
},
lists,
extendGraphqlSchema,
})
})
4 changes: 3 additions & 1 deletion examples/extend-graphql-schema-graphql-ts/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export default config({
// WARNING: this is only needed for our monorepo examples, dont do this
...fixPrismaPath,
},
graphql: {
extendGraphqlSchema
},
lists,
extendGraphqlSchema,
})
5 changes: 3 additions & 2 deletions examples/extend-graphql-schema-nexus/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion examples/extend-graphql-subscriptions/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
12 changes: 8 additions & 4 deletions packages/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,18 @@ export function createAuth<ListTypeInfo extends BaseListTypeInfo> ({

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: {
Expand All @@ -296,9 +303,6 @@ export function createAuth<ListTypeInfo extends BaseListTypeInfo> ({
},
},
},
extendGraphqlSchema: schema => {
return extendGraphqlSchema(authExtendGraphqlSchema(schema))
},
}
}

Expand Down
6 changes: 1 addition & 5 deletions packages/core/src/lib/createGraphQLSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 2 additions & 3 deletions packages/core/src/lib/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
},
Expand All @@ -128,4 +127,4 @@ export function resolveDefaults (config: KeystoneConfig) {
} satisfies KeystoneConfig
}

export type ResolvedKeystoneConfig = ReturnType<typeof resolveDefaults>
export type ResolvedKeystoneConfig = ReturnType<typeof resolveDefaults>
8 changes: 6 additions & 2 deletions packages/core/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ export type KeystoneConfig<TypeInfo extends BaseKeystoneTypeInfo = BaseKeystoneT
path?: string
}

// TODO: why isn't this within .graphql?
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 @@ -270,6 +268,12 @@ export type GraphQLConfig<TypeInfo extends BaseKeystoneTypeInfo = BaseKeystoneTy
* @default 'schema.graphql'
*/
schemaPath?: string

/**
* A function that receives the Keystone GraphQL schema for the developer to extend
* @default 'schema.graphql'
*/
extendGraphqlSchema?: (schema: GraphQLSchema) => GraphQLSchema
}

export type FilesConfig = {
Expand Down
48 changes: 25 additions & 23 deletions tests/api-tests/extend-graphql-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}),
},
}
})
}
}),
})

Expand Down
60 changes: 31 additions & 29 deletions tests/api-tests/queries/cache-hints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof runner>) => {
Expand Down
4 changes: 3 additions & 1 deletion tests/test-projects/live-reloading/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export default config({
...fixPrismaPath,
},
lists,
extendGraphqlSchema,
graphql: {
extendGraphqlSchema
},
ui: {
getAdditionalFiles: [
() => [
Expand Down

0 comments on commit e6196a7

Please sign in to comment.