-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathindex.ts
More file actions
371 lines (321 loc) · 12.4 KB
/
Copy pathindex.ts
File metadata and controls
371 lines (321 loc) · 12.4 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import {
GraphQLService,
SchemaChangeCallback,
Unsubscriber,
GraphQLServiceEngineConfig,
} from 'apollo-server-core';
import {
GraphQLExecutionResult,
GraphQLRequestContext,
WithRequired,
} from 'apollo-server-types';
import { InMemoryLRUCache } from 'apollo-server-caching';
import { isObjectType, isIntrospectionType, GraphQLSchema } from 'graphql';
import { GraphQLSchemaValidationError } from 'apollo-graphql';
import { composeAndValidate, ServiceDefinition } from '@apollo/federation';
import loglevel, { Logger } from 'loglevel';
import loglevelDebug from 'loglevel-debug';
import { buildQueryPlan, buildOperationContext } from './buildQueryPlan';
import {
executeQueryPlan,
ServiceMap,
defaultFieldResolverWithAliasSupport,
} from './executeQueryPlan';
import { getServiceDefinitionsFromRemoteEndpoint } from './loadServicesFromRemoteEndpoint';
import { getServiceDefinitionsFromStorage } from './loadServicesFromStorage';
import { serializeQueryPlan, QueryPlan } from './QueryPlan';
import { GraphQLDataSource } from './datasources/types';
import { RemoteGraphQLDataSource } from './datasources/RemoteGraphQLDatasource';
import { HeadersInit } from 'node-fetch';
export type ServiceEndpointDefinition = Pick<ServiceDefinition, 'name' | 'url'>;
interface GatewayConfigBase {
debug?: boolean;
// TODO: expose the query plan in a more flexible JSON format in the future
// and remove this config option in favor of `exposeQueryPlan`. Playground
// should cutover to use the new option when it's built.
__exposeQueryPlanExperimental?: boolean;
buildService?: (definition: ServiceEndpointDefinition) => GraphQLDataSource;
}
interface RemoteGatewayConfig extends GatewayConfigBase {
serviceList: ServiceEndpointDefinition[];
introspectionHeaders?: HeadersInit;
}
interface ManagedGatewayConfig extends GatewayConfigBase {
federationVersion?: number;
}
interface LocalGatewayConfig extends GatewayConfigBase {
localServiceList: ServiceDefinition[];
}
export type GatewayConfig =
| RemoteGatewayConfig
| LocalGatewayConfig
| ManagedGatewayConfig;
function isLocalConfig(config: GatewayConfig): config is LocalGatewayConfig {
return 'localServiceList' in config;
}
function isRemoteConfig(config: GatewayConfig): config is RemoteGatewayConfig {
return 'serviceList' in config;
}
function isManagedConfig(
config: GatewayConfig,
): config is ManagedGatewayConfig {
return !isRemoteConfig(config) && !isLocalConfig(config);
}
export class ApolloGateway implements GraphQLService {
public schema?: GraphQLSchema;
protected serviceMap: ServiceMap = Object.create(null);
protected config: GatewayConfig;
protected logger: Logger;
protected queryPlanStore?: InMemoryLRUCache<QueryPlan>;
private engineConfig: GraphQLServiceEngineConfig | undefined;
private pollingTimer?: NodeJS.Timer;
private onSchemaChangeListeners = new Set<SchemaChangeCallback>();
constructor(config?: GatewayConfig) {
this.config = {
// TODO: expose the query plan in a more flexible JSON format in the future
// and remove this config option in favor of `exposeQueryPlan`. Playground
// should cutover to use the new option when it's built.
__exposeQueryPlanExperimental: process.env.NODE_ENV !== 'production',
...config,
};
// Setup logging facilities, scoped under the appropriate name.
this.logger = loglevel.getLogger(`apollo-gateway:`);
// Support DEBUG environment variable, à la https://npm.im/debug/.
loglevelDebug(this.logger);
// And also support the `debug` option, if it's truthy.
if (this.config.debug === true) {
this.logger.enableAll();
}
if (isLocalConfig(this.config)) {
this.createSchema(this.config.localServiceList);
}
this.initializeQueryPlanStore();
}
public async load(options?: { engine?: GraphQLServiceEngineConfig }) {
if (options && options.engine) {
if (!options.engine.graphVariant)
console.warn('No graph variant provided. Defaulting to `current`.');
this.engineConfig = options.engine;
}
if (this.schema) {
return { schema: this.schema, executor: this.executor };
}
this.logger.debug('Loading configuration for Gateway');
const [services] = await this.loadServiceDefinitions(this.config);
this.logger.debug('Configuration loaded for Gateway');
this.schema = this.createSchema(services);
return { schema: this.schema, executor: this.executor };
}
protected createSchema(services: ServiceDefinition[]) {
this.logger.debug(
`Composing schema from service list: \n${services
.map(({ name, url }) => ` ${url || 'local'}: ${name}`)
.join('\n')}`,
);
const { schema, errors } = composeAndValidate(services);
if (errors && errors.length > 0) {
throw new GraphQLSchemaValidationError(errors);
}
// this is a temporary workaround for GraphQLFieldExtensions automatic
// wrapping of all fields when using ApolloServer. Here we wrap all fields
// with support for resolving aliases as part of the root value which
// happens because alises are resolved by sub services and the shape
// of the rootvalue already contains the aliased fields as responseNames
this.schema = wrapSchemaWithAliasResolver(schema);
this.createServices(services);
this.logger.debug('Schema loaded and ready for execution');
return schema;
}
public onSchemaChange(callback: SchemaChangeCallback): Unsubscriber {
if (!isManagedConfig(this.config)) {
return () => {};
}
this.onSchemaChangeListeners.add(callback);
if (!this.pollingTimer) this.startPollingServices();
return () => {
this.onSchemaChangeListeners.delete(callback);
if (this.onSchemaChangeListeners.size === 0 && this.pollingTimer) {
clearInterval(this.pollingTimer!);
this.pollingTimer = undefined;
}
};
}
private startPollingServices() {
if (this.pollingTimer) clearInterval(this.pollingTimer);
this.pollingTimer = setInterval(async () => {
let services, isNewSchema;
try {
[services, isNewSchema] = await this.loadServiceDefinitions(
this.config,
);
} catch (e) {
this.logger.debug(
'Error checking for schema updates. Falling back to existing schema.',
e,
);
return;
}
if (!isNewSchema) {
this.logger.debug('No changes to gateway config');
return;
}
if (this.queryPlanStore) this.queryPlanStore.flush();
this.logger.debug('Gateway config has changed, updating schema');
this.createSchema(services);
try {
this.onSchemaChangeListeners.forEach(listener =>
listener(this.schema!),
);
} catch (e) {
this.logger.debug(
'Error notifying schema change listener of update to schema.',
e,
);
}
}, 10 * 1000);
}
protected createServices(services: ServiceEndpointDefinition[]) {
for (const serviceDef of services) {
if (!serviceDef.url && !isLocalConfig(this.config)) {
throw new Error(
`Service definition for service ${serviceDef.name} is missing a url`,
);
}
this.serviceMap[serviceDef.name] = this.config.buildService
? this.config.buildService(serviceDef)
: new RemoteGraphQLDataSource({
url: serviceDef.url,
});
}
}
protected async loadServiceDefinitions(
config: GatewayConfig,
): Promise<[ServiceDefinition[], boolean]> {
if (isLocalConfig(config)) {
return [config.localServiceList, false];
}
if (isRemoteConfig(config)) {
return getServiceDefinitionsFromRemoteEndpoint({
serviceList: config.serviceList,
...(config.introspectionHeaders
? { headers: config.introspectionHeaders }
: {}),
});
}
if (!this.engineConfig) {
throw new Error(
'When `serviceList` is not set, an Apollo Engine configuration must be provided. See https://www.apollographql.com/docs/apollo-server/federation/managed-federation/ for more information.',
);
}
return getServiceDefinitionsFromStorage({
graphId: this.engineConfig.graphId,
apiKeyHash: this.engineConfig.apiKeyHash,
graphVariant: this.engineConfig.graphVariant,
federationVersion: config.federationVersion || 1,
});
}
public executor = async <TContext>(
requestContext: WithRequired<
GraphQLRequestContext<TContext>,
'document' | 'operation' | 'queryHash'
>,
): Promise<GraphQLExecutionResult> => {
const { request, document, queryHash } = requestContext;
const operationContext = buildOperationContext(
this.schema!,
document,
request.operationName,
);
let queryPlan;
if (this.queryPlanStore) {
queryPlan = await this.queryPlanStore.get(queryHash);
}
if (!queryPlan) {
queryPlan = buildQueryPlan(operationContext);
if (this.queryPlanStore) {
// The underlying cache store behind the `documentStore` returns a
// `Promise` which is resolved (or rejected), eventually, based on the
// success or failure (respectively) of the cache save attempt. While
// it's certainly possible to `await` this `Promise`, we don't care about
// whether or not it's successful at this point. We'll instead proceed
// to serve the rest of the request and just hope that this works out.
// If it doesn't work, the next request will have another opportunity to
// try again. Errors will surface as warnings, as appropriate.
//
// While it shouldn't normally be necessary to wrap this `Promise` in a
// `Promise.resolve` invocation, it seems that the underlying cache store
// is returning a non-native `Promise` (e.g. Bluebird, etc.).
Promise.resolve(this.queryPlanStore.set(queryHash, queryPlan)).catch(
err => this.logger.warn('Could not store queryPlan', err),
);
}
}
const response = await executeQueryPlan<TContext>(
queryPlan,
this.serviceMap,
requestContext,
operationContext,
);
const shouldShowQueryPlan =
this.config.__exposeQueryPlanExperimental &&
request.http &&
request.http.headers &&
request.http.headers.get('Apollo-Query-Plan-Experimental');
if (shouldShowQueryPlan) {
const serializedQueryPlan = serializeQueryPlan(queryPlan);
this.logger.debug(serializedQueryPlan);
// TODO: expose the query plan in a more flexible JSON format in the future
// and rename this to `queryPlan`. Playground should cutover to use the new
// option once we've built a way to print that representation.
response.extensions = { __queryPlanExperimental: serializedQueryPlan };
}
return response;
};
private initializeQueryPlanStore(): void {
this.queryPlanStore = new InMemoryLRUCache<QueryPlan>({
// Create ~about~ a 30MiB InMemoryLRUCache. This is less than precise
// since the technique to calculate the size of a DocumentNode is
// only using JSON.stringify on the DocumentNode (and thus doesn't account
// for unicode characters, etc.), but it should do a reasonable job at
// providing a caching document store for most operations.
maxSize: Math.pow(2, 20) * 30,
sizeCalculator: approximateObjectSize,
});
}
public async stop() {
if (this.pollingTimer) {
clearInterval(this.pollingTimer);
this.pollingTimer = undefined;
}
}
}
function approximateObjectSize<T>(obj: T): number {
return Buffer.byteLength(JSON.stringify(obj), 'utf8');
}
// We can't use transformSchema here because the extension data for query
// planning would be lost. Instead we set a resolver for each field
// in order to counteract GraphQLExtensions preventing a defaultFieldResolver
// from doing the same job
function wrapSchemaWithAliasResolver(schema: GraphQLSchema): GraphQLSchema {
const typeMap = schema.getTypeMap();
Object.keys(typeMap).forEach(typeName => {
const type = typeMap[typeName];
if (isObjectType(type) && !isIntrospectionType(type)) {
const fields = type.getFields();
Object.keys(fields).forEach(fieldName => {
const field = fields[fieldName];
field.resolve = defaultFieldResolverWithAliasSupport;
});
}
});
return schema;
}
export {
buildQueryPlan,
executeQueryPlan,
serializeQueryPlan,
buildOperationContext,
QueryPlan,
ServiceMap,
};
export * from './datasources';