UNPKG

975 BJavaScriptView Raw
1import { ApolloServer } from 'apollo-server-express';
2import cookieParser from 'cookie-parser';
3import express from 'express';
4import hopsConfig from 'hops-config';
5import schema from 'hops-graphql/schema';
6
7const apolloAppPromise = Promise.resolve(
8 typeof schema === 'function' ? schema() : schema
9).then(resolvedSchema => {
10 const app = express();
11 app.use(cookieParser());
12
13 const server = new ApolloServer({
14 schema: resolvedSchema,
15 playground: {
16 settings: {
17 'request.credentials': 'same-origin',
18 },
19 },
20 context: context => ({ ...context, config: hopsConfig }),
21 });
22
23 server.applyMiddleware({
24 app,
25 path: hopsConfig.graphqlMockServerPath,
26 cors: {
27 credentials: true,
28 origin: true,
29 methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
30 allowedHeaders: 'content-type',
31 },
32 });
33
34 return app;
35});
36
37export default (req, res, next) => {
38 apolloAppPromise.then(app => app.handle(req, res, next), next);
39};