This repository has been archived by the owner on May 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
server.js
59 lines (53 loc) · 1.56 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// @flow
/** Copyright (c) 2018 Uber Technologies, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import {
createPlugin,
type Context,
type FusionPlugin,
type Middleware,
} from 'fusion-core';
import {graphqlKoa} from 'apollo-server-koa';
import {GraphQLSchemaToken, ApolloContextToken} from 'fusion-apollo';
import {ApolloServerEndpointToken} from './tokens';
type ApolloServerDepsType = {
endpoint: typeof ApolloServerEndpointToken.optional,
schema: typeof GraphQLSchemaToken,
apolloContext: typeof ApolloContextToken.optional,
};
type ApolloServerType = Context => Promise<void>;
type PluginType = FusionPlugin<ApolloServerDepsType, ApolloServerType>;
const pluginFactory: () => PluginType = () =>
createPlugin({
deps: {
endpoint: ApolloServerEndpointToken.optional,
schema: GraphQLSchemaToken,
apolloContext: ApolloContextToken.optional,
},
provides: ({schema, apolloContext = ctx => ctx}) => {
return graphqlKoa(ctx => ({
schema,
tracing: true,
cacheControl: true,
context:
typeof apolloContext === 'function'
? // $FlowFixMe
apolloContext(ctx)
: apolloContext,
}));
},
middleware: ({endpoint = '/graphql'}, handler): Middleware => async (
ctx,
next
) => {
await next();
if (ctx.path === endpoint) {
return handler(ctx);
}
},
});
export default ((__NODE__ && pluginFactory(): any): PluginType);