-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathApolloServer.ts
165 lines (149 loc) · 5.24 KB
/
ApolloServer.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import {
HttpContext,
FunctionRequest,
FunctionResponse,
} from './azureFunctions';
import { ApolloServerBase } from 'apollo-server-core';
import { GraphQLOptions, Config } from 'apollo-server-core';
import {
renderPlaygroundPage,
RenderPageOptions as PlaygroundRenderPageOptions,
} from '@apollographql/graphql-playground-html';
import { graphqlAzureFunction } from './azureFunctionApollo';
export interface CreateHandlerOptions {
cors?: {
origin?: boolean | string | string[];
methods?: string | string[];
allowedHeaders?: string | string[];
exposedHeaders?: string | string[];
credentials?: boolean;
maxAge?: number;
};
}
export class ApolloServer extends ApolloServerBase {
// If you feel tempted to add an option to this constructor. Please consider
// another place, since the documentation becomes much more complicated when
// the constructor is not longer shared between all integration
constructor(options: Config) {
if (process.env.ENGINE_API_KEY || options.engine) {
options.engine = {
sendReportsImmediately: true,
...(typeof options.engine !== 'boolean' ? options.engine : {}),
};
}
super(options);
}
// This translates the arguments from the middleware into graphQL options It
// provides typings for the integration specific behavior, ideally this would
// be propagated with a generic to the super class
createGraphQLServerOptions(
request: FunctionRequest,
context: HttpContext,
): Promise<GraphQLOptions> {
return super.graphQLServerOptions({ request, context });
}
public createHandler({ cors }: CreateHandlerOptions = { cors: undefined }) {
// We will kick off the `willStart` event once for the server, and then
// await it before processing any requests by incorporating its `await` into
// the GraphQLServerOptions function which is called before each request.
const promiseWillStart = this.willStart();
const corsHeaders: FunctionResponse['headers'] = {};
if (cors) {
if (cors.methods) {
if (typeof cors.methods === 'string') {
corsHeaders['Access-Control-Allow-Methods'] = cors.methods;
} else if (Array.isArray(cors.methods)) {
corsHeaders['Access-Control-Allow-Methods'] = cors.methods.join(',');
}
}
if (cors.allowedHeaders) {
if (typeof cors.allowedHeaders === 'string') {
corsHeaders['Access-Control-Allow-Headers'] = cors.allowedHeaders;
} else if (Array.isArray(cors.allowedHeaders)) {
corsHeaders[
'Access-Control-Allow-Headers'
] = cors.allowedHeaders.join(',');
}
}
if (cors.exposedHeaders) {
if (typeof cors.exposedHeaders === 'string') {
corsHeaders['Access-Control-Expose-Headers'] = cors.exposedHeaders;
} else if (Array.isArray(cors.exposedHeaders)) {
corsHeaders[
'Access-Control-Expose-Headers'
] = cors.exposedHeaders.join(',');
}
}
if (cors.credentials) {
corsHeaders['Access-Control-Allow-Credentials'] = 'true';
}
if (cors.maxAge) {
corsHeaders['Access-Control-Max-Age'] = cors.maxAge;
}
}
return (context: HttpContext, req: FunctionRequest) => {
if (cors && cors.origin) {
if (typeof cors.origin === 'string') {
corsHeaders['Access-Control-Allow-Origin'] = cors.origin;
} else if (
typeof cors.origin === 'boolean' ||
(Array.isArray(cors.origin) &&
cors.origin.includes(
req.headers['Origin'] || req.headers['origin'],
))
) {
corsHeaders['Access-Control-Allow-Origin'] =
req.headers['Origin'] || req.headers['origin'];
}
if (!cors.allowedHeaders) {
corsHeaders['Access-Control-Allow-Headers'] =
req.headers['Access-Control-Request-Headers'];
}
}
if (req.method === 'OPTIONS') {
context.done(null, {
body: '',
status: 204,
headers: corsHeaders,
});
return;
}
if (this.playgroundOptions && req.method === 'GET') {
const acceptHeader = req.headers['Accept'] || req.headers['accept'];
if (acceptHeader && acceptHeader.includes('text/html')) {
const path = req.originalUrl || '/';
const playgroundRenderPageOptions: PlaygroundRenderPageOptions = {
endpoint: path,
...this.playgroundOptions,
};
const body = renderPlaygroundPage(playgroundRenderPageOptions);
context.done(null, {
body: body,
status: 200,
headers: {
'Content-Type': 'text/html',
...corsHeaders,
},
});
return;
}
}
const callbackFilter = (error?: any, output?: FunctionResponse) => {
context.done(
error,
output && {
...output,
headers: {
...output.headers,
...corsHeaders,
},
},
);
};
graphqlAzureFunction(async () => {
await promiseWillStart;
return this.createGraphQLServerOptions(req, context);
})(context, req, callbackFilter);
};
}
}