diff --git a/.gitignore b/.gitignore index eef7577..7fa739c 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ run test/fixtures/apps/model-app/run/ .vscode/ yarn.lock +yarn-error.log diff --git a/app/middleware/graphql.js b/app/middleware/graphql.js index f65bc65..56afb8a 100644 --- a/app/middleware/graphql.js +++ b/app/middleware/graphql.js @@ -4,10 +4,13 @@ // directly exposed to the public, now we must still use that for the middle- // ware. const { graphqlKoa } = require('apollo-server-koa/dist/koaApollo'); +const { FormatErrorExtension } = require('apollo-server-core/dist/formatters'); // This has been newly imported, because in v2 of apollo-server, this is removed. const { resolveGraphiQLString } = require('apollo-server-module-graphiql'); +const { koa: voyagerMiddleware } = require('graphql-voyager/middleware'); + /** This function is directly copied from: https://github.com/apollographql/apollo-server/blob/300c0cd12b56be439b206d55131e1b93a9e6dade/packages/apollo-server-koa/src/koaApollo.ts#L51 @@ -36,12 +39,28 @@ function graphiqlKoa(options) { module.exports = (_, app) => { const options = app.config.graphql; const graphQLRouter = options.router; + options.graphiqlRouter = options.graphiqlRouter ? options.graphiqlRouter : graphQLRouter; + const voyagerRouter = options.voyagerRouter; let graphiql = true; if (options.graphiql === false) { graphiql = false; } + let voyager = true; + if (options.voyager === false) { + voyager = false; + } + + const extensions = []; + if (options.formatError && typeof options.formatError === 'function') { + const formatErrorExtension = () => new FormatErrorExtension( + options.formatError, + options.debug, + ); + extensions.push(formatErrorExtension); + } + return async (ctx, next) => { /* istanbul ignore else */ if (ctx.path === graphQLRouter) { @@ -50,7 +69,7 @@ module.exports = (_, app) => { await options.onPreGraphiQL(ctx); } return graphiqlKoa({ - endpointURL: graphQLRouter, + endpointURL: options.graphiqlRouter, })(ctx); } if (options.onPreGraphQL) { @@ -59,7 +78,12 @@ module.exports = (_, app) => { return graphqlKoa({ schema: app.schema, context: ctx, + extensions, })(ctx); + } else if (voyager && ctx.path === voyagerRouter) { + return voyagerMiddleware({ + endpointURL: options.graphiqlRouter, + })(ctx, next); } await next(); }; diff --git a/config/config.default.js b/config/config.default.js index 1dd59cf..4e5aac4 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -2,4 +2,6 @@ exports.graphql = { router: '/graphql', + voyagerRouter: '/graphdoc', + debug: true, }; diff --git a/package.json b/package.json index 3744ae3..8e5adb7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "egg-graphql", - "version": "2.1.0", + "version": "2.0.4", "description": "egg graphql plugin", "eggPlugin": { "name": "graphql" @@ -18,6 +18,7 @@ "graphql": "^0.13.2", "graphql-tag": "^2.9.2", "graphql-tools": "^3.1.1", + "graphql-voyager": "^1.0.0-rc.25", "lodash": "^4.17.10" }, "devDependencies": {