11const { ApolloServer, gql } = require ( 'apollo-server-express' ) ;
2+ const { ApolloServerPluginCacheControl } = require ( 'apollo-server-core' ) ;
23const flattenDeep = require ( 'lodash.flattendeep' ) ;
34const memoize = require ( 'micro-memoize' ) ;
45const falsey = require ( 'falsey' ) ;
56const createCorsMiddleware = require ( 'cors' ) ;
67const { execute, print } = require ( 'graphql' ) ;
8+ const { makeExecutableSchema } = require ( '@graphql-tools/schema' ) ;
79const GraphQLUpload = require ( 'graphql-upload/GraphQLUpload.js' ) ;
810const { arrayToObject, objMerge, flatten, unique, filterValues } = require ( '@open-keystone/utils' ) ;
911const {
@@ -226,7 +228,8 @@ module.exports = class Keystone {
226228 query = gql ( query ) ;
227229 }
228230
229- return execute ( schema , query , null , context , variables ) ;
231+ // return execute(schema, query, null, context, variables);
232+ return execute ( { schema, document : query , contextValue : context , variableValues : variables } ) ;
230233 }
231234
232235 createAuthStrategy ( options ) {
@@ -443,11 +446,20 @@ module.exports = class Keystone {
443446 }
444447 }
445448
446- createApolloServer ( { apolloConfig = { } , schemaName, dev } ) {
449+ async createApolloServer ( { apolloConfig = { } , schemaName } ) {
447450 // add the Admin GraphQL API
451+
452+ const typeDefs = this . getTypeDefs ( { schemaName } ) ;
453+ const resolvers = this . getResolvers ( { schemaName } ) ;
454+ const { parseOptions } = apolloConfig ;
455+ const schema = makeExecutableSchema ( {
456+ typeDefs,
457+ resolvers,
458+ parseOptions,
459+ } ) ;
460+
448461 const server = new ApolloServer ( {
449- typeDefs : this . getTypeDefs ( { schemaName } ) ,
450- resolvers : this . getResolvers ( { schemaName } ) ,
462+ schema,
451463 context : ( { req } ) => ( {
452464 ...this . createContext ( {
453465 schemaName,
@@ -457,22 +469,20 @@ module.exports = class Keystone {
457469 ...this . _sessionManager . getContext ( req ) ,
458470 req,
459471 } ) ,
460- ...( process . env . ENGINE_API_KEY || process . env . APOLLO_KEY
461- ? {
462- tracing : true ,
463- }
464- : {
465- engine : false ,
466- // Only enable tracing in dev mode so we can get local debug info, but
467- // don't bother returning that info on prod when the `engine` is
468- // disabled.
469- tracing : dev ,
470- } ) ,
471472 formatError,
472473 ...apolloConfig ,
473- uploads : false , // User cannot override this as it would clash with the upload middleware
474+ plugins : [
475+ ...( apolloConfig . plugins || [ ] ) ,
476+ ApolloServerPluginCacheControl ( {
477+ calculateHttpHeaders : true ,
478+ defaultMaxAge : 0 ,
479+ } ) ,
480+ ] ,
474481 } ) ;
475- this . _schemas [ schemaName ] = server . schema ;
482+
483+ await server . start ( ) ;
484+
485+ this . _schemas [ schemaName ] = schema ;
476486
477487 return server ;
478488 }
@@ -607,7 +617,7 @@ module.exports = class Keystone {
607617 pinoOptions,
608618 cors = { origin : true , credentials : true } ,
609619 } = { } ) {
610- this . createApolloServer ( { schemaName : 'internal' } ) ;
620+ await this . createApolloServer ( { schemaName : 'internal' } ) ;
611621 const middlewares = await this . _prepareMiddlewares ( { dev, apps, distDir, pinoOptions, cors } ) ;
612622 // These function can't be called after prepare(), so make them throw an error from now on.
613623 [ 'extendGraphQLSchema' , 'createList' , 'createAuthStrategy' ] . forEach ( f => {
0 commit comments