Releases: Carrotzpc/egg-graphql
Releases · Carrotzpc/egg-graphql
use ctx of each request
3.2.0-alpha.3 release 3.2.0-alpha.3
move onPreGraphQL onPrePlayground at the top of the middleware
3.2.0-alpha.2 release 3.2.0-alpha.2
fix issues of onPreGraphQL and onPrePlayground.
- add miss
await
foronPreGraphQL
andonPrePlayground
.
refactor (middleware): use apollo-server-koa directly
// app/middleware/graphql.js
const { ApolloServer } = require('apollo-server-koa');
const { get } = require('lodash');
module.exports = (_, app) => {
const options = app.config.graphql;
const graphQLRouter = options.router || '/graphql';
let apolloServer;
return async (ctx, next) => {
// init apollo server
if (!apolloServer) {
const { getApolloServerOptions } = options;
const apolloServerOptions = Object.assign(
{
// log the error stack by default
formatError: error => {
const stacktrace = (get(error, 'extensions.exception.stacktrace') || []).join('\n');
ctx.logger.error('egg-graphql', stacktrace);
return error;
},
},
options.apolloServerOptions,
// pass ctx to getApolloServerOptions
getApolloServerOptions && getApolloServerOptions(ctx),
// pass schema and context to apollo server
{
schema: app.schema,
context: ctx,
}
);
apolloServer = new ApolloServer(apolloServerOptions);
apolloServer.applyMiddleware({ app, path: graphQLRouter });
}
const { onPreGraphQL, onPrePlayground, playground } = options;
if (ctx.path === graphQLRouter) {
if (ctx.request.accepts([ 'json', 'html' ]) === 'html') {
playground && onPrePlayground && onPrePlayground(ctx);
} else {
onPreGraphQL && onPreGraphQL(ctx);
}
}
await next();
};
};