From 622d8074ceb86086b70c5f2b7b6e2136ecd2f0bd Mon Sep 17 00:00:00 2001 From: Thomas Walker Date: Mon, 23 Aug 2021 13:31:21 +1000 Subject: [PATCH 1/2] Allow support for introspection. --- docs/pages/docs/apis/config.mdx | 5 +-- examples-staging/playground/keystone.ts | 29 ++++++++++++---- .../src/lib/server/createApolloServer.ts | 33 +++++++++++++++---- 3 files changed, 52 insertions(+), 15 deletions(-) diff --git a/docs/pages/docs/apis/config.mdx b/docs/pages/docs/apis/config.mdx index 81bb959f6ca..a88be185166 100644 --- a/docs/pages/docs/apis/config.mdx +++ b/docs/pages/docs/apis/config.mdx @@ -285,8 +285,8 @@ Options: See also the per-list `graphql.queryLimits` option in the [Schema API](./schema). - `apolloConfig` (default: `undefined`): Allows you to pass extra options into the `ApolloServer` constructor. - `playground` (default: `process.env.NODE_ENV !== 'production'`): If truthy, will enable the GraphQL Playground for testing queries and mutations in the browser. - To configure behaviour, pass an object of [GraphQL Playground settings](https://github.com/graphql/graphql-playground#settings). - - See the [Apollo docs](https://www.apollographql.com/docs/apollo-server/api/apollo-server/#constructor) for more supported options. + To configure behaviour, pass an object of [GraphQL Playground settings](https://github.com/graphql/graphql-playground#settings). See the [Apollo docs](https://www.apollographql.com/docs/apollo-server/api/apollo-server/#constructor) for more supported options. + - `introspection` (default: `undefined`): Introspection enables you to query a GraphQL server for information about the underlying schema. If the playground is enabled then introspection is automatically enabled, unless specifically disabled. ```typescript export default config({ @@ -295,6 +295,7 @@ export default config({ queryLimits: { maxTotalResults: 100 }, apolloConfig: { playground: process.env.NODE_ENV !== 'production', + introspection: process.env.NODE_ENV !== 'production', /* ... */ }, }, diff --git a/examples-staging/playground/keystone.ts b/examples-staging/playground/keystone.ts index 12f791c3613..9eb0eb1ed8d 100644 --- a/examples-staging/playground/keystone.ts +++ b/examples-staging/playground/keystone.ts @@ -1,7 +1,7 @@ import { config } from '@keystone-next/keystone'; import { lists } from './schema'; -// graphql.apolloConfig.playground === false (playground not accessible in all cases) +// graphql.apolloConfig.playground === false (playground not accessible in all cases, introspection will be disabled by default) // export default config({ // db: { // provider: 'sqlite', @@ -15,7 +15,7 @@ import { lists } from './schema'; // } // }); -// graphql.apolloConfig.playground === true (playground accessible in all cases) +// graphql.apolloConfig.playground === true (playground accessible in all cases, introspection will be enabled by default) // export default config({ // db: { // provider: 'sqlite', @@ -25,11 +25,26 @@ import { lists } from './schema'; // graphql: { // apolloConfig: { // playground: true, -// } -// } +// }, +// }, +// }); + +// graphql.apolloConfig.playground === true && graphql.apolloConfig.introspection === false (playground accessible in all cases, introspection disabled) +// export default config({ +// db: { +// provider: 'sqlite', +// url: process.env.DATABASE_URL || 'file:./keystone-example.db', +// }, +// lists, +// graphql: { +// apolloConfig: { +// playground: true, +// introspection: false, +// }, +// }, // }); -// graphql.apolloConfig.playground === { settings: ... } (playground accessible in all cases with further customisation - https://www.apollographql.com/docs/apollo-server/testing/graphql-playground) +// graphql.apolloConfig.playground === { settings: ... } (playground accessible in all cases with further customisation - https://www.apollographql.com/docs/apollo-server/testing/graphql-playground, introspection will be enabled by default) // export default config({ // db: { // provider: 'sqlite', @@ -47,7 +62,7 @@ import { lists } from './schema'; // } // }); -// process.env.NODE_ENV === 'production' (playground not accessible in production) +// process.env.NODE_ENV === 'production' (playground not accessible in production, introspection will be disabled by default) // process.env.NODE_ENV = 'production'; // export default config({ // db: { @@ -62,7 +77,7 @@ import { lists } from './schema'; // } // }); -// not specified at all (playground uses defaults) +// not specified at all (playground and introspection uses defaults, enabled in development and disabled in production) export default config({ db: { provider: 'sqlite', diff --git a/packages/keystone/src/lib/server/createApolloServer.ts b/packages/keystone/src/lib/server/createApolloServer.ts index 4e944dca0be..33df36ca655 100644 --- a/packages/keystone/src/lib/server/createApolloServer.ts +++ b/packages/keystone/src/lib/server/createApolloServer.ts @@ -63,19 +63,22 @@ const _createApolloServerConfig = ({ }) => { // Playground config, is /api/graphql available? const apolloConfig = graphqlConfig?.apolloConfig; - const pp = apolloConfig?.playground; + const apolloConfigPlayground = apolloConfig?.playground; let playground: Config['playground']; const settings = { 'request.credentials': 'same-origin' }; - if (typeof pp === 'boolean' && !pp) { + if (typeof apolloConfigPlayground === 'boolean' && !apolloConfigPlayground) { // graphql.apolloConfig.playground === false (playground not accessible in all cases) playground = false; - } else if (typeof pp === 'boolean') { + } else if (typeof apolloConfigPlayground === 'boolean') { // graphql.apolloConfig.playground === true (playground accessible in all cases) playground = { settings }; - } else if (pp) { + } else if (apolloConfigPlayground) { // graphql.apolloConfig.playground === { settings: ... } (playground accessible in all cases with further customisation - https://www.apollographql.com/docs/apollo-server/testing/graphql-playground) - playground = { ...pp, settings: { ...settings, ...pp.settings } }; + playground = { + ...apolloConfigPlayground, + settings: { ...settings, ...apolloConfigPlayground.settings }, + }; } else if (process.env.NODE_ENV === 'production') { // process.env.NODE_ENV === 'production' (playground not accessible in production) playground = undefined; @@ -84,6 +87,23 @@ const _createApolloServerConfig = ({ playground = { settings }; } + const apolloConfigIntrospection = apolloConfig?.introspection; + let introspection: Config['introspection']; + + if (typeof apolloConfigIntrospection === 'boolean' && !apolloConfigIntrospection) { + // graphql.apolloConfig.introspection === false (introspection not accessible in all cases) + introspection = false; + } else if (typeof apolloConfigIntrospection === 'boolean') { + // graphql.apolloConfig.introspection === true (introspection accessible in all cases) + introspection = true; + } else if (process.env.NODE_ENV === 'production') { + // process.env.NODE_ENV === 'production' (introspection not accessible in production) + introspection = undefined; + } else if ((typeof playground === 'boolean' && playground) || typeof playground === 'object') { + // not specified at all (introspection enabled if playground is enabled or defined) + introspection = true; + } + return { uploads: false, schema: graphQLSchema, @@ -100,8 +120,9 @@ const _createApolloServerConfig = ({ // }), ...apolloConfig, formatError: formatError(graphqlConfig), - // Carefully inject the playground + // Carefully inject the playground and introspection variables playground, + introspection, // FIXME: Support for file handling configuration // maxFileSize: 200 * 1024 * 1024, // maxFiles: 5, From cd7d37efca59b9d906c40103d576c0841fb6d903 Mon Sep 17 00:00:00 2001 From: Thomas Walker Date: Mon, 23 Aug 2021 13:34:55 +1000 Subject: [PATCH 2/2] Create great-cougars-argue.md --- .changeset/great-cougars-argue.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/great-cougars-argue.md diff --git a/.changeset/great-cougars-argue.md b/.changeset/great-cougars-argue.md new file mode 100644 index 00000000000..7bde39eb080 --- /dev/null +++ b/.changeset/great-cougars-argue.md @@ -0,0 +1,7 @@ +--- +'@keystone-next/website': patch +'@keystone-next/example-playground': patch +'@keystone-next/keystone': patch +--- + +Adds support for `introspection` in the Apollo Server config. Introspection enables you to query a GraphQL server for information about the underlying schema. If the playground is enabled then introspection is automatically enabled - unless specifically disabled.