-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
createApolloServer.ts
108 lines (101 loc) · 3.36 KB
/
createApolloServer.ts
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import type { IncomingMessage, ServerResponse } from 'http';
import { GraphQLError, GraphQLSchema } from 'graphql';
import { ApolloServer as ApolloServerMicro } from 'apollo-server-micro';
import { ApolloServer as ApolloServerExpress } from 'apollo-server-express';
import {
ApolloServerPluginLandingPageDisabled,
ApolloServerPluginLandingPageGraphQLPlayground,
} from 'apollo-server-core';
import type { CreateContext, GraphQLConfig, SessionStrategy } from '../../types';
import { createSessionContext } from '../../session';
export const createApolloServerMicro = ({
graphQLSchema,
createContext,
sessionStrategy,
graphqlConfig,
connectionPromise,
}: {
graphQLSchema: GraphQLSchema;
createContext: CreateContext;
sessionStrategy?: SessionStrategy<any>;
graphqlConfig?: GraphQLConfig;
connectionPromise: Promise<any>;
}) => {
const context = async ({ req, res }: { req: IncomingMessage; res: ServerResponse }) => {
await connectionPromise;
return createContext({
sessionContext: sessionStrategy
? await createSessionContext(sessionStrategy, req, res, createContext)
: undefined,
req,
});
};
const serverConfig = _createApolloServerConfig({ graphQLSchema, graphqlConfig });
return new ApolloServerMicro({ ...serverConfig, context });
};
export const createApolloServerExpress = ({
graphQLSchema,
createContext,
sessionStrategy,
graphqlConfig,
}: {
graphQLSchema: GraphQLSchema;
createContext: CreateContext;
sessionStrategy?: SessionStrategy<any>;
graphqlConfig?: GraphQLConfig;
}) => {
const context = async ({ req, res }: { req: IncomingMessage; res: ServerResponse }) =>
createContext({
sessionContext: sessionStrategy
? await createSessionContext(sessionStrategy, req, res, createContext)
: undefined,
req,
});
const serverConfig = _createApolloServerConfig({ graphQLSchema, graphqlConfig });
return new ApolloServerExpress({ ...serverConfig, context });
};
const _createApolloServerConfig = ({
graphQLSchema,
graphqlConfig,
}: {
graphQLSchema: GraphQLSchema;
graphqlConfig?: GraphQLConfig;
}) => {
const apolloConfig = graphqlConfig?.apolloConfig;
const playgroundOption = graphqlConfig?.playground ?? process.env.NODE_ENV !== 'production';
return {
schema: graphQLSchema,
debug: graphqlConfig?.debug, // If undefined, use Apollo default of NODE_ENV !== 'production'
...apolloConfig,
plugins:
playgroundOption === 'apollo'
? apolloConfig?.plugins
: [
playgroundOption
? ApolloServerPluginLandingPageGraphQLPlayground({
settings: { 'request.credentials': 'same-origin' },
})
: ApolloServerPluginLandingPageDisabled(),
...(apolloConfig?.plugins || []),
],
formatError: formatError(graphqlConfig),
};
};
const formatError = (graphqlConfig: GraphQLConfig | undefined) => {
return (err: GraphQLError) => {
let debug = graphqlConfig?.debug;
if (debug === undefined) {
debug = process.env.NODE_ENV !== 'production';
}
if (!debug && err.extensions) {
// Strip out any `debug` extensions
delete err.extensions.debug;
delete err.extensions.exception;
}
if (graphqlConfig?.apolloConfig?.formatError) {
return graphqlConfig.apolloConfig.formatError(err);
} else {
return err;
}
};
};