diff --git a/History.md b/History.md index 87a0220..d352b24 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,7 @@ +2.3.1 / 2018-12-28 +================== + + * feat:pass other apollo server options. (#20) 2.3.0 / 2018-10-21 ================== diff --git a/README.md b/README.md index a45bf94..a942d50 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,24 @@ exports.graphql = { onPreGraphQL: function* (ctx) {}, // 开发工具 graphiQL 路由前的拦截器,建议用于做权限操作(如只提供开发者使用) onPreGraphiQL: function* (ctx) {}, + // apollo server的透传参数 + apolloServerOptions: { + rootValue, + formatError, + formatResponse, + mocks, + schemaDirectives, + introspection, + playground, + debug, + validationRules, + tracing, + cacheControl, + subscriptions, + engine, + persistedQueries, + cors, + } }; // 添加中间件拦截请求 diff --git a/app/middleware/graphql.js b/app/middleware/graphql.js index f65bc65..3354feb 100644 --- a/app/middleware/graphql.js +++ b/app/middleware/graphql.js @@ -44,22 +44,33 @@ module.exports = (_, app) => { return async (ctx, next) => { /* istanbul ignore else */ + const { + onPreGraphiQL, + onPreGraphQL, + apolloServerOptions + } = options; + if (ctx.path === graphQLRouter) { if (ctx.request.accepts([ 'json', 'html' ]) === 'html' && graphiql) { - if (options.onPreGraphiQL) { - await options.onPreGraphiQL(ctx); + if (onPreGraphiQL) { + await onPreGraphiQL(ctx); } return graphiqlKoa({ endpointURL: graphQLRouter, })(ctx); } - if (options.onPreGraphQL) { - await options.onPreGraphQL(ctx); + if (onPreGraphQL) { + await onPreGraphQL(ctx); } - return graphqlKoa({ - schema: app.schema, - context: ctx, - })(ctx); + const serverOptions = Object.assign( + {}, + apolloServerOptions, + { + schema: app.schema, + context: ctx, + } + ); + return graphqlKoa(serverOptions)(ctx); } await next(); };