Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD]graphiqlRouter option in config #17

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ run
test/fixtures/apps/model-app/run/
.vscode/
yarn.lock
yarn-error.log
26 changes: 25 additions & 1 deletion app/middleware/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -50,7 +69,7 @@ module.exports = (_, app) => {
await options.onPreGraphiQL(ctx);
}
return graphiqlKoa({
endpointURL: graphQLRouter,
endpointURL: options.graphiqlRouter,
})(ctx);
}
if (options.onPreGraphQL) {
Expand All @@ -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();
};
Expand Down
2 changes: 2 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

exports.graphql = {
router: '/graphql',
voyagerRouter: '/graphdoc',
debug: true,
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "egg-graphql",
"version": "2.1.0",
"version": "2.0.4",
"description": "egg graphql plugin",
"eggPlugin": {
"name": "graphql"
Expand All @@ -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": {
Expand Down