-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.ts
658 lines (568 loc) · 22.7 KB
/
index.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
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,
GraphQLError,
VariableDefinitionNode,
} 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,
CompositionMetadata,
} from './loadServicesFromStorage';
import { serializeQueryPlan, QueryPlan, OperationContext } from './QueryPlan';
import { GraphQLDataSource } from './datasources/types';
import { RemoteGraphQLDataSource } from './datasources/RemoteGraphQLDataSource';
import { HeadersInit } from 'node-fetch';
import { getVariableValues } from 'graphql/execution/values';
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;
// experimental observability callbacks
experimental_didResolveQueryPlan?: Experimental_DidResolveQueryPlanCallback;
experimental_didFailComposition?: Experimental_DidFailCompositionCallback;
experimental_updateServiceDefinitions?: Experimental_UpdateServiceDefinitions;
experimental_didUpdateComposition?: Experimental_DidUpdateCompositionCallback;
experimental_pollInterval?: number;
}
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;
type DataSourceCache = {
[serviceName: string]: { url?: string; dataSource: GraphQLDataSource };
};
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 type Experimental_DidResolveQueryPlanCallback = ({
queryPlan,
serviceMap,
operationContext,
}: {
readonly queryPlan: QueryPlan;
readonly serviceMap: ServiceMap;
readonly operationContext: OperationContext;
}) => void;
export type Experimental_DidFailCompositionCallback = ({
errors,
serviceList,
compositionMetadata,
}: {
readonly errors: GraphQLError[];
readonly serviceList: ServiceDefinition[];
readonly compositionMetadata?: CompositionMetadata;
}) => void;
export interface Experimental_CompositionInfo {
serviceDefinitions: ServiceDefinition[];
schema: GraphQLSchema;
compositionMetadata?: CompositionMetadata;
}
export type Experimental_DidUpdateCompositionCallback = (
currentConfig: Experimental_CompositionInfo,
previousConfig?: Experimental_CompositionInfo,
) => void;
/**
* **Note:** It's possible for a schema to be the same (`isNewSchema: false`) when
* `serviceDefinitions` have changed. For example, during type migration, the
* composed schema may be identical but the `serviceDefinitions` would differ
* since a type has moved from one service to another.
*/
export type Experimental_UpdateServiceDefinitions = (
config: GatewayConfig,
) => Promise<{
serviceDefinitions?: ServiceDefinition[];
compositionMetadata?: CompositionMetadata;
isNewSchema: boolean;
}>;
type Await<T> = T extends Promise<infer U> ? U : T;
type RequestContext<TContext> = WithRequired<
GraphQLRequestContext<TContext>,
'document' | 'queryHash'
>;
export class ApolloGateway implements GraphQLService {
public schema?: GraphQLSchema;
protected serviceMap: DataSourceCache = 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>();
private serviceDefinitions: ServiceDefinition[] = [];
private compositionMetadata?: CompositionMetadata;
private serviceSdlCache = new Map<string, string>();
// Observe query plan, service info, and operation info prior to execution.
// The information made available here will give insight into the resulting
// query plan and the inputs that generated it.
protected experimental_didResolveQueryPlan?: Experimental_DidResolveQueryPlanCallback;
// Observe composition failures and the ServiceList that caused them. This
// enables reporting any issues that occur during composition. Implementors
// will be interested in addressing these immediately.
protected experimental_didFailComposition?: Experimental_DidFailCompositionCallback;
// Used to communicated composition changes, and what definitions caused
// those updates
protected experimental_didUpdateComposition?: Experimental_DidUpdateCompositionCallback;
// Used for overriding the default service list fetcher. This should return
// an array of ServiceDefinition. *This function must be awaited.*
protected updateServiceDefinitions: Experimental_UpdateServiceDefinitions;
// how often service defs should be loaded/updated (in ms)
protected experimental_pollInterval?: number;
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.schema = this.createSchema(this.config.localServiceList);
}
this.initializeQueryPlanStore();
// this will be overwritten if the config provides experimental_updateServiceDefinitions
this.updateServiceDefinitions = this.loadServiceDefinitions;
if (config) {
this.updateServiceDefinitions =
config.experimental_updateServiceDefinitions ||
this.updateServiceDefinitions;
// set up experimental observability callbacks
this.experimental_didResolveQueryPlan =
config.experimental_didResolveQueryPlan;
this.experimental_didFailComposition =
config.experimental_didFailComposition;
this.experimental_didUpdateComposition =
config.experimental_didUpdateComposition;
if (
isManagedConfig(config) &&
config.experimental_pollInterval &&
config.experimental_pollInterval < 10000
) {
this.experimental_pollInterval = 10000;
this.logger.warn(
'Polling Apollo services at a frequency of less than once per 10 seconds (10000) is disallowed. Instead, the minimum allowed pollInterval of 10000 will be used. Please reconfigure your experimental_pollInterval accordingly. If this is problematic for your team, please contact support.',
);
} else {
this.experimental_pollInterval = config.experimental_pollInterval;
}
// Warn against using the pollInterval and a serviceList simulatenously
if (config.experimental_pollInterval && isRemoteConfig(config)) {
console.warn(
'Polling running services is dangerous and not recommended in production. ' +
'Polling should only be used against a registry. ' +
'If you are polling running services, use with caution.',
);
}
}
}
public async load(options?: { engine?: GraphQLServiceEngineConfig }) {
await this.updateComposition(options);
const { graphId, graphVariant } = (options && options.engine) || {};
const mode = isManagedConfig(this.config) ? 'managed' : 'unmanaged';
this.logger.info(
`Gateway successfully loaded schema.\n\t* Mode: ${mode}${graphId ? `\n\t* Service: ${graphId}@${graphVariant || 'current'}`: ''}`,
);
if (this.experimental_pollInterval) {
setInterval(
() => this.updateComposition(options),
this.experimental_pollInterval,
);
}
return {
// we know this will be here since we're awaiting this.updateComposition
// before here which sets this.schema
schema: this.schema!,
executor: this.executor,
};
}
protected async updateComposition(options?: {
engine?: GraphQLServiceEngineConfig;
}): Promise<void> {
// The options argument and internal config update coule be handled by this.load()
// instead of here. We can remove this as a breaking change in the future.
if (options && options.engine) {
if (!options.engine.graphVariant)
console.warn('No graph variant provided. Defaulting to `current`.');
this.engineConfig = options.engine;
}
const previousSchema = this.schema;
const previousServiceDefinitions = this.serviceDefinitions;
const previousCompositionMetadata = this.compositionMetadata;
let result: Await<ReturnType<Experimental_UpdateServiceDefinitions>>;
this.logger.debug('Loading configuration for gateway');
try {
result = await this.updateServiceDefinitions(this.config);
} catch (e) {
this.logger.warn(
'Error checking for schema updates. Falling back to existing schema.',
e,
);
return;
}
if (
!result.serviceDefinitions ||
JSON.stringify(this.serviceDefinitions) ===
JSON.stringify(result.serviceDefinitions)
) {
this.logger.debug('No change in service definitions since last check');
return;
}
if (previousSchema) {
this.logger.info('Gateway config has changed, updating schema');
}
this.compositionMetadata = result.compositionMetadata;
this.serviceDefinitions = result.serviceDefinitions;
if (this.queryPlanStore) this.queryPlanStore.flush();
this.schema = this.createSchema(result.serviceDefinitions);
try {
this.onSchemaChangeListeners.forEach(listener => listener(this.schema!));
} catch (e) {
this.logger.error(
'Error notifying schema change listener of update to schema.',
e,
);
}
if (this.experimental_didUpdateComposition) {
this.experimental_didUpdateComposition(
{
serviceDefinitions: result.serviceDefinitions,
schema: this.schema,
...(this.compositionMetadata && {
compositionMetadata: this.compositionMetadata,
}),
},
previousServiceDefinitions &&
previousSchema && {
serviceDefinitions: previousServiceDefinitions,
schema: previousSchema,
...(previousCompositionMetadata && {
compositionMetadata: previousCompositionMetadata,
}),
},
);
}
}
protected createSchema(serviceList: ServiceDefinition[]) {
this.logger.debug(
`Composing schema from service list: \n${serviceList
.map(({ name, url }) => ` ${url || 'local'}: ${name}`)
.join('\n')}`,
);
const { schema, errors } = composeAndValidate(serviceList);
if (errors && errors.length > 0) {
if (this.experimental_didFailComposition) {
this.experimental_didFailComposition({
errors,
serviceList,
...(this.compositionMetadata && {
compositionMetadata: this.compositionMetadata,
}),
});
}
throw new GraphQLSchemaValidationError(errors);
}
this.createServices(serviceList);
this.logger.debug('Schema loaded and ready for execution');
// 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
return wrapSchemaWithAliasResolver(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(() => {
this.updateComposition();
}, this.experimental_pollInterval || 10000);
// Prevent the Node.js event loop from remaining active (and preventing,
// e.g. process shutdown) by calling `unref` on the `Timeout`. For more
// information, see https://nodejs.org/api/timers.html#timers_timeout_unref.
this.pollingTimer.unref();
}
private createAndCacheDataSource(
serviceDef: ServiceEndpointDefinition,
): GraphQLDataSource {
// If the DataSource has already been created, early return
if (
this.serviceMap[serviceDef.name] &&
serviceDef.url === this.serviceMap[serviceDef.name].url
)
return this.serviceMap[serviceDef.name].dataSource;
if (!serviceDef.url && !isLocalConfig(this.config)) {
this.logger.error(
`Service definition for service ${serviceDef.name} is missing a url`,
);
}
const dataSource = this.config.buildService
? this.config.buildService(serviceDef)
: new RemoteGraphQLDataSource({
url: serviceDef.url,
});
// Cache the created DataSource
this.serviceMap[serviceDef.name] = { url: serviceDef.url, dataSource };
return dataSource;
}
protected createServices(services: ServiceEndpointDefinition[]) {
for (const serviceDef of services) {
this.createAndCacheDataSource(serviceDef);
}
}
protected async loadServiceDefinitions(
config: GatewayConfig,
): ReturnType<Experimental_UpdateServiceDefinitions> {
if (isLocalConfig(config)) {
return { isNewSchema: false };
}
if (isRemoteConfig(config)) {
const serviceList = config.serviceList.map(serviceDefinition => ({
...serviceDefinition,
dataSource: this.createAndCacheDataSource(serviceDefinition),
}));
return getServiceDefinitionsFromRemoteEndpoint({
serviceList,
...(config.introspectionHeaders
? { headers: config.introspectionHeaders }
: {}),
serviceSdlCache: this.serviceSdlCache,
});
}
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,
});
}
// XXX Nothing guarantees that the only errors thrown or returned in
// result.errors are GraphQLErrors, even though other code (eg
// apollo-engine-reporting) assumes that. In fact, errors talking to backends
// are unlikely to show up as GraphQLErrors. Do we need to use
// formatApolloErrors or something?
public executor = async <TContext>(
requestContext: RequestContext<TContext>,
): Promise<GraphQLExecutionResult> => {
const { request, document, queryHash } = requestContext;
const queryPlanStoreKey = queryHash + (request.operationName || '');
const operationContext = buildOperationContext(
this.schema!,
document,
request.operationName,
);
// No need to build a query plan if we know the request is invalid beforehand
// In the future, this should be controlled by the requestPipeline
const validationErrors = this.validateIncomingRequest(
requestContext,
operationContext,
);
if (validationErrors.length > 0) {
return { errors: validationErrors };
}
let queryPlan: QueryPlan | undefined;
if (this.queryPlanStore) {
queryPlan = await this.queryPlanStore.get(queryPlanStoreKey);
}
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(queryPlanStoreKey, queryPlan),
).catch(err => this.logger.warn('Could not store queryPlan', err));
}
}
const serviceMap: ServiceMap = Object.entries(this.serviceMap).reduce(
(serviceDataSources, [serviceName, { dataSource }]) => {
serviceDataSources[serviceName] = dataSource;
return serviceDataSources;
},
Object.create(null) as ServiceMap,
);
if (this.experimental_didResolveQueryPlan) {
this.experimental_didResolveQueryPlan({
queryPlan,
serviceMap,
operationContext,
});
}
const response = await executeQueryPlan<TContext>(
queryPlan,
serviceMap,
requestContext,
operationContext,
);
const shouldShowQueryPlan =
this.config.__exposeQueryPlanExperimental &&
request.http &&
request.http.headers &&
request.http.headers.get('Apollo-Query-Plan-Experimental');
// We only want to serialize the query plan if we're going to use it, which is
// in two cases:
// 1) non-empty query plan and config.debug === true
// 2) non-empty query plan and shouldShowQueryPlan === true
const serializedQueryPlan =
queryPlan.node && (this.config.debug || shouldShowQueryPlan)
? serializeQueryPlan(queryPlan)
: null;
if (this.config.debug && serializedQueryPlan) {
this.logger.debug(serializedQueryPlan);
}
if (shouldShowQueryPlan) {
// 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.
// In the case that `serializedQueryPlan` is null (on introspection), we
// still want to respond to Playground with something truthy since it depends
// on this to decide that query plans are supported by this gateway.
response.extensions = {
__queryPlanExperimental: serializedQueryPlan || true,
};
}
return response;
};
protected validateIncomingRequest<TContext>(
requestContext: RequestContext<TContext>,
operationContext: OperationContext,
) {
// casting out of `readonly`
const variableDefinitions = operationContext.operation
.variableDefinitions as VariableDefinitionNode[] | undefined;
if (!variableDefinitions) return [];
const { errors } = getVariableValues(
operationContext.schema,
variableDefinitions,
requestContext.request.variables!,
);
return errors || [];
}
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';