/
constructor.ts
143 lines (129 loc) 路 5.6 KB
/
constructor.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
/**
* This file consists of types used to define the arguments to the ApolloServer
* constructor. The top-level and likely most interesting type exported from
* here is `ApolloServerOptions` which is used to define the input object to the
* `ApolloServer` constructor.
*/
import type { Logger } from '@apollo/utils.logger';
import type { IExecutableSchemaDefinition } from '@graphql-tools/schema';
import type {
DocumentNode,
GraphQLFieldResolver,
GraphQLFormattedError,
GraphQLSchema,
ParseOptions,
ValidationRule,
} from 'graphql';
import type { KeyValueCache } from '@apollo/utils.keyvaluecache';
import type { GatewayInterface } from '@apollo/server-gateway-interface';
import type { ApolloServerPlugin } from './plugins.js';
import type { BaseContext } from './index.js';
import type { GraphQLExperimentalIncrementalExecutionResults } from '../incrementalDeliveryPolyfill.js';
export type DocumentStore = KeyValueCache<DocumentNode>;
// Configuration for how Apollo Server talks to the Apollo registry, as passed
// to the ApolloServer constructor. Each field can also be provided as an
// environment variable.
export interface ApolloConfigInput {
// Your Apollo API key. Environment variable: APOLLO_KEY.
key?: string;
// The graph ref for your graph, eg `my-graph@my-variant` or `my-graph` to use
// your graph's default variant. Environment variable: APOLLO_GRAPH_REF. For
// backwards compatibility, may alternatively specify the ref as graphId and
// graphVariant separately.
graphRef?: string;
// The graph ID of your graph, eg `my-graph`. Environment variable:
// APOLLO_GRAPH_ID.
graphId?: string;
// Your graph's variant name, eg `my-variant`. Environment variable:
// APOLLO_GRAPH_VARIANT.
graphVariant?: string;
}
// some defaults filled in from the ApolloConfigInput passed to the constructor.
export interface ApolloConfig {
key?: string;
keyHash?: string;
graphRef?: string;
}
export interface PersistedQueryOptions {
cache?: KeyValueCache<string>;
/**
* Specified in **seconds**, this time-to-live (TTL) value limits the lifespan
* of how long the persisted query should be cached. To specify a desired
* lifespan of "infinite", set this to `null`, in which case the eviction will
* be determined by the cache's eviction policy, but the record will never
* simply expire.
*/
ttl?: number | null;
}
export interface CSRFPreventionOptions {
// CSRF prevention works by only processing operations from requests whose
// structure indicates that if they were sent by a web browser, then the
// browser would have had to send a preflight OPTIONS request already. We do
// this by specifying some headers that a browser will never automatically set
// and which will trigger the browser to preflight. Apollo Server will reject
// any operation that does not set at least one of these headers *and* does
// not set a content-type (to a header whose parsed type is not
// application/x-www-form-urlencoded, multipart/form-data, or text/plain). If
// CSRF prevention is enabled (eg, with `csrfPrevention: true`) this list
// defaults to ['x-apollo-operation-name', 'apollo-require-preflight']. This
// will allow POST operations from any client and GET operations from Apollo
// Client Web, Apollo iOS, and Apollo Kotlin.
requestHeaders?: string[];
}
interface ApolloServerOptionsBase<TContext extends BaseContext> {
formatError?: (
formattedError: GraphQLFormattedError,
error: unknown,
) => GraphQLFormattedError;
rootValue?: ((parsedQuery: DocumentNode) => unknown) | unknown;
validationRules?: Array<ValidationRule>;
fieldResolver?: GraphQLFieldResolver<any, TContext>;
cache?: KeyValueCache<string> | 'bounded';
includeStacktraceInErrorResponses?: boolean;
logger?: Logger;
allowBatchedHttpRequests?: boolean;
introspection?: boolean;
plugins?: ApolloServerPlugin<TContext>[];
persistedQueries?: PersistedQueryOptions | false;
stopOnTerminationSignals?: boolean;
apollo?: ApolloConfigInput;
nodeEnv?: string;
documentStore?: DocumentStore | null;
csrfPrevention?: CSRFPreventionOptions | boolean;
// Used for parsing operations; unlike in AS3, this is not also used for
// parsing the schema.
parseOptions?: ParseOptions;
// For testing only.
__testing_incrementalExecutionResults?: GraphQLExperimentalIncrementalExecutionResults;
}
export interface ApolloServerOptionsWithGateway<TContext extends BaseContext>
extends ApolloServerOptionsBase<TContext> {
gateway: GatewayInterface;
schema?: undefined;
typeDefs?: undefined;
resolvers?: undefined;
}
export interface ApolloServerOptionsWithSchema<TContext extends BaseContext>
extends ApolloServerOptionsBase<TContext> {
schema: GraphQLSchema;
gateway?: undefined;
typeDefs?: undefined;
resolvers?: undefined;
}
export interface ApolloServerOptionsWithTypeDefs<TContext extends BaseContext>
extends ApolloServerOptionsBase<TContext> {
// These two options are always only passed directly through to
// makeExecutableSchema. (If you don't want to use makeExecutableSchema, pass
// `schema` instead.)
typeDefs: IExecutableSchemaDefinition<TContext>['typeDefs'];
resolvers?: IExecutableSchemaDefinition<TContext>['resolvers'];
gateway?: undefined;
schema?: undefined;
}
// Used internally in ApolloServer.ts but not publicly exported.
export type ApolloServerOptionsWithStaticSchema<TContext extends BaseContext> =
| ApolloServerOptionsWithSchema<TContext>
| ApolloServerOptionsWithTypeDefs<TContext>;
export type ApolloServerOptions<TContext extends BaseContext> =
| ApolloServerOptionsWithGateway<TContext>
| ApolloServerOptionsWithStaticSchema<TContext>;