-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ApolloServer.ts
847 lines (757 loc) · 27.7 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
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
import {
makeExecutableSchema,
addMockFunctionsToSchema,
GraphQLParseOptions,
} from 'graphql-tools';
import { Server as HttpServer } from 'http';
import {
execute,
GraphQLSchema,
subscribe,
ExecutionResult,
GraphQLError,
GraphQLFieldResolver,
ValidationContext,
FieldDefinitionNode,
DocumentNode,
isObjectType,
isScalarType,
isSchema,
} from 'graphql';
import { GraphQLExtension } from 'graphql-extensions';
import {
InMemoryLRUCache,
PrefixingKeyValueCache,
} from 'apollo-server-caching';
import {
ApolloServerPlugin,
GraphQLServiceContext,
} from 'apollo-server-plugin-base';
import runtimeSupportsUploads from './utils/runtimeSupportsUploads';
import {
SubscriptionServer,
ExecutionParams,
} from 'subscriptions-transport-ws';
import { formatApolloErrors } from 'apollo-server-errors';
import {
GraphQLServerOptions,
PersistedQueryOptions,
} from './graphqlOptions';
import {
Config,
Context,
ContextFunction,
SubscriptionServerOptions,
FileUploadOptions,
PluginDefinition,
} from './types';
import { gql } from './index';
import {
createPlaygroundOptions,
PlaygroundRenderPageOptions,
} from './playground';
import { generateSchemaHash } from './utils/schemaHash';
import { isDirectiveDefined } from './utils/isDirectiveDefined';
import createSHA from './utils/createSHA';
import {
processGraphQLRequest,
GraphQLRequestContext,
GraphQLRequest,
APQ_CACHE_PREFIX,
} from './requestPipeline';
import { Headers } from 'apollo-server-env';
import { buildServiceDefinition } from '@apollographql/apollo-tools';
const NoIntrospection = (context: ValidationContext) => ({
Field(node: FieldDefinitionNode) {
if (node.name.value === '__schema' || node.name.value === '__type') {
context.reportError(
new GraphQLError(
'GraphQL introspection is not allowed by Apollo Server, but the query contained __schema or __type. To enable introspection, pass introspection: true to ApolloServer in production',
[node],
),
);
}
},
});
function getEngineApiKey(engine: Config['engine']): string | undefined {
const keyFromEnv = process.env.ENGINE_API_KEY || '';
if (engine === false) {
return;
} else if (typeof engine === 'object' && engine.apiKey) {
return engine.apiKey;
} else if (keyFromEnv) {
return keyFromEnv;
}
return;
}
function getEngineGraphVariant(engine: Config['engine']): string | undefined {
if (engine === false) {
return;
} else if (typeof engine === 'object' && engine.schemaTag) {
return engine.schemaTag;
} else {
return process.env.ENGINE_SCHEMA_TAG;
}
}
function getEngineServiceId(engine: Config['engine']): string | undefined {
const engineApiKey = getEngineApiKey(engine);
if (engineApiKey) {
return engineApiKey.split(':', 2)[1];
}
return;
}
const forbidUploadsForTesting =
process && process.env.NODE_ENV === 'test' && !runtimeSupportsUploads;
function approximateObjectSize<T>(obj: T): number {
return Buffer.byteLength(JSON.stringify(obj), 'utf8');
}
type SchemaDerivedData = {
// A store that, when enabled (default), will store the parsed and validated
// versions of operations in-memory, allowing subsequent parses/validates
// on the same operation to be executed immediately.
documentStore?: InMemoryLRUCache<DocumentNode>;
schema: GraphQLSchema;
schemaHash: string;
extensions: Array<() => GraphQLExtension>;
};
export class ApolloServerBase {
public subscriptionsPath?: string;
public graphqlPath: string = '/graphql';
public requestOptions: Partial<GraphQLServerOptions<any>> = Object.create(null);
private context?: Context | ContextFunction;
private engineReportingAgent?: import('apollo-engine-reporting').EngineReportingAgent;
private engineServiceId?: string;
private engineApiKeyHash?: string;
protected plugins: ApolloServerPlugin[] = [];
protected subscriptionServerOptions?: SubscriptionServerOptions;
protected uploadsConfig?: FileUploadOptions;
// set by installSubscriptionHandlers.
private subscriptionServer?: SubscriptionServer;
// the default version is specified in playground.ts
protected playgroundOptions?: PlaygroundRenderPageOptions;
private parseOptions: GraphQLParseOptions;
private schemaDerivedData: Promise<SchemaDerivedData>;
private config: Config;
/** @deprecated: This is undefined for servers operating as gateways, and will be removed in a future release **/
protected schema?: GraphQLSchema;
private toDispose = new Set<() => void>();
private experimental_approximateDocumentStoreMiB:
Config['experimental_approximateDocumentStoreMiB'];
// The constructor should be universal across all environments. All environment specific behavior should be set by adding or overriding methods
constructor(config: Config) {
if (!config) throw new Error('ApolloServer requires options.');
this.config = config;
const {
context,
resolvers,
schema,
schemaDirectives,
modules,
typeDefs,
parseOptions = {},
introspection,
mocks,
mockEntireSchema,
extensions,
engine,
subscriptions,
uploads,
playground,
plugins,
gateway,
experimental_approximateDocumentStoreMiB,
...requestOptions
} = config;
if (gateway && (modules || schema || typeDefs || resolvers)) {
// TODO: this could be handled by adjusting the typings to keep gateway configs and non-gateway configs seprate.
throw new Error(
'Cannot define both `gateway` and any of: `modules`, `schema`, `typeDefs`, or `resolvers`',
);
}
this.parseOptions = parseOptions;
this.context = context;
// Plugins will be instantiated if they aren't already, and this.plugins
// is populated accordingly.
this.ensurePluginInstantiation(plugins);
// While reading process.env is slow, a server should only be constructed
// once per run, so we place the env check inside the constructor. If env
// should be used outside of the constructor context, place it as a private
// or protected field of the class instead of a global. Keeping the read in
// the contructor enables testing of different environments
const isDev = process.env.NODE_ENV !== 'production';
// if this is local dev, introspection should turned on
// in production, we can manually turn introspection on by passing {
// introspection: true } to the constructor of ApolloServer
if (
(typeof introspection === 'boolean' && !introspection) ||
(introspection === undefined && !isDev)
) {
const noIntro = [NoIntrospection];
requestOptions.validationRules = requestOptions.validationRules
? requestOptions.validationRules.concat(noIntro)
: noIntro;
}
if (requestOptions.cacheControl !== false) {
if (
typeof requestOptions.cacheControl === 'boolean' &&
requestOptions.cacheControl === true
) {
// cacheControl: true means that the user needs the cache-control
// extensions. This means we are running the proxy, so we should not
// strip out the cache control extension and not add cache-control headers
requestOptions.cacheControl = {
stripFormattedExtensions: false,
calculateHttpHeaders: false,
defaultMaxAge: 0,
};
} else {
// Default behavior is to run default header calculation and return
// no cacheControl extensions
requestOptions.cacheControl = {
stripFormattedExtensions: true,
calculateHttpHeaders: true,
defaultMaxAge: 0,
...requestOptions.cacheControl,
};
}
}
if (!requestOptions.cache) {
requestOptions.cache = new InMemoryLRUCache();
}
if (requestOptions.persistedQueries !== false) {
const {
cache: apqCache = requestOptions.cache!,
...apqOtherOptions
} = requestOptions.persistedQueries || Object.create(null);
requestOptions.persistedQueries = {
cache: new PrefixingKeyValueCache(apqCache, APQ_CACHE_PREFIX),
...apqOtherOptions,
};
} else {
// the user does not want to use persisted queries, so we remove the field
delete requestOptions.persistedQueries;
}
this.requestOptions = requestOptions as GraphQLServerOptions;
if (uploads !== false && !forbidUploadsForTesting) {
if (this.supportsUploads()) {
if (!runtimeSupportsUploads) {
printNodeFileUploadsMessage();
throw new Error(
'`graphql-upload` is no longer supported on Node.js < v8.5.0. ' +
'See https://bit.ly/gql-upload-node-6.',
);
}
if (uploads === true || typeof uploads === 'undefined') {
this.uploadsConfig = {};
} else {
this.uploadsConfig = uploads;
}
//This is here to check if uploads is requested without support. By
//default we enable them if supported by the integration
} else if (uploads) {
throw new Error(
'This implementation of ApolloServer does not support file uploads because the environment cannot accept multi-part forms',
);
}
}
// Normalize the legacy option maskErrorDetails.
if (engine && typeof engine === 'object') {
if (engine.maskErrorDetails && engine.rewriteError) {
throw new Error("Can't set both maskErrorDetails and rewriteError!");
} else if (
engine.rewriteError &&
typeof engine.rewriteError !== 'function'
) {
throw new Error('rewriteError must be a function');
} else if (engine.maskErrorDetails) {
engine.rewriteError = () => new GraphQLError('<masked>');
delete engine.maskErrorDetails;
}
}
// In an effort to avoid over-exposing the API key itself, extract the
// service ID from the API key for plugins which only needs service ID.
// The truthyness of this value can also be used in other forks of logic
// related to Engine, as is the case with EngineReportingAgent just below.
this.engineServiceId = getEngineServiceId(engine);
const apiKey = getEngineApiKey(engine);
if (apiKey) {
this.engineApiKeyHash = createSHA('sha512')
.update(apiKey)
.digest('hex');
}
if (this.engineServiceId) {
const { EngineReportingAgent } = require('apollo-engine-reporting');
this.engineReportingAgent = new EngineReportingAgent(
typeof engine === 'object' ? engine : Object.create(null),
);
// Don't add the extension here (we want to add it later in generateSchemaDerivedData).
}
if (gateway && subscriptions !== false) {
// TODO: this could be handled by adjusting the typings to keep gateway configs and non-gateway configs seprate.
throw new Error(
[
'Subscriptions are not yet compatible with the gateway.',
"Set `subscriptions: false` in Apollo Server's constructor to",
'explicitly disable subscriptions (which are on by default)',
'and allow for gateway functionality.',
].join(' '),
);
} else if (subscriptions !== false) {
if (this.supportsSubscriptions()) {
if (subscriptions === true || typeof subscriptions === 'undefined') {
this.subscriptionServerOptions = {
path: this.graphqlPath,
};
} else if (typeof subscriptions === 'string') {
this.subscriptionServerOptions = { path: subscriptions };
} else {
this.subscriptionServerOptions = {
path: this.graphqlPath,
...subscriptions,
};
}
// This is part of the public API.
this.subscriptionsPath = this.subscriptionServerOptions.path;
//This is here to check if subscriptions are requested without support. By
//default we enable them if supported by the integration
} else if (subscriptions) {
throw new Error(
'This implementation of ApolloServer does not support GraphQL subscriptions.',
);
}
}
this.playgroundOptions = createPlaygroundOptions(playground);
// TODO: This is a bit nasty because the subscription server needs this.schema synchronously, for reasons of backwards compatibility.
const _schema = this.initSchema();
if (isSchema(_schema)) {
const derivedData = this.generateSchemaDerivedData(_schema);
this.schema = derivedData.schema;
this.schemaDerivedData = Promise.resolve(derivedData);
} else if (typeof _schema.then === 'function') {
this.schemaDerivedData = _schema.then(schema =>
this.generateSchemaDerivedData(schema),
);
} else {
throw new Error("Unexpected error: Unable to resolve a valid GraphQLSchema. Please file an issue with a reproduction of this error, if possible.");
}
}
// used by integrations to synchronize the path with subscriptions, some
// integrations do not have paths, such as lambda
public setGraphQLPath(path: string) {
this.graphqlPath = path;
}
private initSchema(): GraphQLSchema | Promise<GraphQLSchema> {
const {
gateway,
engine,
schema,
modules,
typeDefs,
resolvers,
schemaDirectives,
parseOptions,
} = this.config;
if (gateway) {
this.toDispose.add(
// Store the unsubscribe handles, which are returned from
// `onSchemaChange`, for later disposal when the server stops
gateway.onSchemaChange(
schema =>
(this.schemaDerivedData = Promise.resolve(
this.generateSchemaDerivedData(schema),
)),
),
);
const graphVariant = getEngineGraphVariant(engine);
const engineConfig =
this.engineApiKeyHash && this.engineServiceId
? {
apiKeyHash: this.engineApiKeyHash,
graphId: this.engineServiceId,
...(graphVariant && { graphVariant }),
}
: undefined;
return gateway.load({ engine: engineConfig }).then(config => {
this.requestOptions.executor = config.executor;
return config.schema;
});
}
let constructedSchema;
if (schema) {
constructedSchema = schema;
} else if (modules) {
const { schema, errors } = buildServiceDefinition(modules);
if (errors && errors.length > 0) {
throw new Error(errors.map(error => error.message).join('\n\n'));
}
constructedSchema = schema!;
} else {
if (!typeDefs) {
throw Error(
'Apollo Server requires either an existing schema, modules or typeDefs',
);
}
const augmentedTypeDefs = Array.isArray(typeDefs) ? typeDefs : [typeDefs];
// We augment the typeDefs with the @cacheControl directive and associated
// scope enum, so makeExecutableSchema won't fail SDL validation
if (!isDirectiveDefined(augmentedTypeDefs, 'cacheControl')) {
augmentedTypeDefs.push(
gql`
enum CacheControlScope {
PUBLIC
PRIVATE
}
directive @cacheControl(
maxAge: Int
scope: CacheControlScope
) on FIELD_DEFINITION | OBJECT | INTERFACE
`,
);
}
if (this.uploadsConfig) {
const { GraphQLUpload } = require('graphql-upload');
if (Array.isArray(resolvers)) {
if (resolvers.every(resolver => !resolver.Upload)) {
resolvers.push({ Upload: GraphQLUpload });
}
} else {
if (resolvers && !resolvers.Upload) {
resolvers.Upload = GraphQLUpload;
}
}
// We augment the typeDefs with the Upload scalar, so typeDefs that
// don't include it won't fail
augmentedTypeDefs.push(
gql`
scalar Upload
`,
);
}
constructedSchema = makeExecutableSchema({
typeDefs: augmentedTypeDefs,
schemaDirectives,
resolvers,
parseOptions,
});
}
return constructedSchema;
}
private generateSchemaDerivedData(schema: GraphQLSchema): SchemaDerivedData {
const schemaHash = generateSchemaHash(schema!);
const { mocks, mockEntireSchema, extensions: _extensions } = this.config;
if (mocks || (typeof mockEntireSchema !== 'undefined' && mocks !== false)) {
addMockFunctionsToSchema({
schema,
mocks:
typeof mocks === 'boolean' || typeof mocks === 'undefined'
? {}
: mocks,
preserveResolvers:
typeof mockEntireSchema === 'undefined' ? false : !mockEntireSchema,
});
}
const extensions = [];
const schemaIsFederated = this.schemaIsFederated(schema);
const { engine } = this.config;
// Keep this extension second so it wraps everything, except error formatting
if (this.engineReportingAgent) {
if (schemaIsFederated) {
// XXX users can configure a federated Apollo Server to send metrics, but the
// Gateway should be responsible for that. It's possible that users are running
// their own gateway or running a federated service on its own. Nonetheless, in
// the likely case it was accidental, we warn users that they should only report
// metrics from the Gateway.
console.warn(
"It looks like you're running a federated schema and you've configured your service " +
'to report metrics to Apollo Graph Manager. You should only configure your Apollo gateway ' +
'to report metrics to Apollo Graph Manager.',
);
}
extensions.push(() =>
this.engineReportingAgent!.newExtension(schemaHash),
);
} else if (engine !== false && schemaIsFederated) {
// We haven't configured this app to use Engine directly. But it looks like
// we are a federated service backend, so we should be capable of including
// our trace in a response extension if we are asked to by the gateway.
const {
EngineFederatedTracingExtension,
} = require('apollo-engine-reporting');
const rewriteError =
engine && typeof engine === 'object' ? engine.rewriteError : undefined;
extensions.push(
() => new EngineFederatedTracingExtension({ rewriteError }),
);
}
// Note: doRunQuery will add its own extensions if you set tracing,
// or cacheControl.
extensions.push(...(_extensions || []));
// Initialize the document store. This cannot currently be disabled.
const documentStore = this.initializeDocumentStore();
return {
schema,
schemaHash,
extensions,
documentStore,
};
}
protected async willStart() {
const { schema, schemaHash } = await this.schemaDerivedData;
const service: GraphQLServiceContext = {
schema: schema,
schemaHash: schemaHash,
engine: {
serviceID: this.engineServiceId,
apiKeyHash: this.engineApiKeyHash,
},
};
// The `persistedQueries` attribute on the GraphQLServiceContext was
// originally used by the operation registry, which shared the cache with
// it. This is no longer the case. However, while we are continuing to
// expand the support of the interface for `persistedQueries`, e.g. with
// additions like https://github.com/apollographql/apollo-server/pull/3623,
// we don't want to continually expand the API surface of what we expose
// to the plugin API. In this particular case, it certainly doesn't need
// to get the `ttl` default value which are intended for APQ only.
if (this.requestOptions.persistedQueries?.cache) {
service.persistedQueries = {
cache: this.requestOptions.persistedQueries.cache,
}
}
persistedQueries: this.requestOptions.persistedQueries,
await Promise.all(
this.plugins.map(
plugin =>
plugin.serverWillStart &&
plugin.serverWillStart(service),
),
);
}
public async stop() {
this.toDispose.forEach(dispose => dispose());
if (this.subscriptionServer) await this.subscriptionServer.close();
if (this.engineReportingAgent) {
this.engineReportingAgent.stop();
await this.engineReportingAgent.sendAllReports();
}
}
public installSubscriptionHandlers(server: HttpServer) {
if (!this.subscriptionServerOptions) {
if (this.config.gateway) {
throw Error(
'Subscriptions are not supported when operating as a gateway',
);
}
if (this.supportsSubscriptions()) {
throw Error(
'Subscriptions are disabled, due to subscriptions set to false in the ApolloServer constructor',
);
} else {
throw Error(
'Subscriptions are not supported, choose an integration, such as apollo-server-express that allows persistent connections',
);
}
}
const { SubscriptionServer } = require('subscriptions-transport-ws');
const {
onDisconnect,
onConnect,
keepAlive,
path,
} = this.subscriptionServerOptions;
// TODO: This shouldn't use this.schema, as it is deprecated in favor of the schemaDerivedData promise.
const schema = this.schema;
if (this.schema === undefined)
throw new Error(
'Schema undefined during creation of subscription server.',
);
this.subscriptionServer = SubscriptionServer.create(
{
schema,
execute,
subscribe,
onConnect: onConnect
? onConnect
: (connectionParams: Object) => ({ ...connectionParams }),
onDisconnect: onDisconnect,
onOperation: async (
message: { payload: any },
connection: ExecutionParams,
) => {
connection.formatResponse = (value: ExecutionResult) => ({
...value,
errors:
value.errors &&
formatApolloErrors([...value.errors], {
formatter: this.requestOptions.formatError,
debug: this.requestOptions.debug,
}),
});
connection.formatError = this.requestOptions.formatError;
let context: Context = this.context ? this.context : { connection };
try {
context =
typeof this.context === 'function'
? await this.context({ connection, payload: message.payload })
: context;
} catch (e) {
throw formatApolloErrors([e], {
formatter: this.requestOptions.formatError,
debug: this.requestOptions.debug,
})[0];
}
return { ...connection, context };
},
keepAlive,
},
{
server,
path,
},
);
}
protected supportsSubscriptions(): boolean {
return false;
}
protected supportsUploads(): boolean {
return false;
}
// Returns true if it appears that the schema was returned from
// @apollo/federation's buildFederatedSchema. This strategy avoids depending
// explicitly on @apollo/federation or relying on something that might not
// survive transformations like monkey-patching a boolean field onto the
// schema.
//
// The only thing this is used for is determining whether traces should be
// added to responses if requested with an HTTP header; if there's a false
// positive, that feature can be disabled by specifying `engine: false`.
private schemaIsFederated(schema: GraphQLSchema): boolean {
const serviceType = schema.getType('_Service');
if (!(serviceType && isObjectType(serviceType))) {
return false;
}
const sdlField = serviceType.getFields().sdl;
if (!sdlField) {
return false;
}
const sdlFieldType = sdlField.type;
if (!isScalarType(sdlFieldType)) {
return false;
}
return sdlFieldType.name == 'String';
}
private ensurePluginInstantiation(plugins?: PluginDefinition[]): void {
if (!plugins || !plugins.length) {
return;
}
this.plugins = plugins.map(plugin => {
if (typeof plugin === 'function') {
return plugin();
}
return plugin;
});
}
private initializeDocumentStore(): InMemoryLRUCache<DocumentNode> {
return new InMemoryLRUCache<DocumentNode>({
// 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) *
(this.experimental_approximateDocumentStoreMiB || 30),
sizeCalculator: approximateObjectSize,
});
}
// This function is used by the integrations to generate the graphQLOptions
// from an object containing the request and other integration specific
// options
protected async graphQLServerOptions(
integrationContextArgument?: Record<string, any>,
): Promise<GraphQLServerOptions> {
const { schema, documentStore, extensions } = await this.schemaDerivedData;
let context: Context = this.context ? this.context : {};
try {
context =
typeof this.context === 'function'
? await this.context(integrationContextArgument || {})
: context;
} catch (error) {
// Defer context error resolution to inside of runQuery
context = () => {
throw error;
};
}
return {
schema,
plugins: this.plugins,
documentStore,
extensions,
context,
// Allow overrides from options. Be explicit about a couple of them to
// avoid a bad side effect of the otherwise useful noUnusedLocals option
// (https://github.com/Microsoft/TypeScript/issues/21673).
persistedQueries: this.requestOptions
.persistedQueries as PersistedQueryOptions,
fieldResolver: this.requestOptions.fieldResolver as GraphQLFieldResolver<
any,
any
>,
parseOptions: this.parseOptions,
reporting: !!this.engineReportingAgent,
...this.requestOptions,
};
}
public async executeOperation(request: GraphQLRequest) {
let options;
try {
options = await this.graphQLServerOptions();
} catch (e) {
e.message = `Invalid options provided to ApolloServer: ${e.message}`;
throw new Error(e);
}
if (typeof options.context === 'function') {
options.context = (options.context as () => never)();
}
const requestCtx: GraphQLRequestContext = {
request,
context: options.context || Object.create(null),
cache: options.cache!,
response: {
http: {
headers: new Headers(),
},
},
};
return processGraphQLRequest(options, requestCtx);
}
}
function printNodeFileUploadsMessage() {
console.error(
[
'*****************************************************************',
'* *',
'* ERROR! Manual intervention is necessary for Node.js < v8.5.0! *',
'* *',
'*****************************************************************',
'',
'The third-party `graphql-upload` package, which is used to implement',
'file uploads in Apollo Server 2.x, no longer supports Node.js LTS',
'versions prior to Node.js v8.5.0.',
'',
'Deployments which NEED file upload capabilities should update to',
'Node.js >= v8.5.0 to continue using uploads.',
'',
'If this server DOES NOT NEED file uploads and wishes to continue',
'using this version of Node.js, uploads can be disabled by adding:',
'',
' uploads: false,',
'',
'...to the options for Apollo Server and re-deploying the server.',
'',
'For more information, see https://bit.ly/gql-upload-node-6.',
'',
].join('\n'),
);
}