-
Notifications
You must be signed in to change notification settings - Fork 242
/
config.ts
269 lines (232 loc) 路 8.31 KB
/
config.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
import { GraphQLError, GraphQLSchema } from 'graphql';
import { HeadersInit } from 'node-fetch';
import { fetch } from 'apollo-server-env';
import {
GraphQLRequestContextExecutionDidStart,
Logger,
} from 'apollo-server-types';
import { ServiceDefinition } from '@apollo/federation';
import { GraphQLDataSource } from './datasources/types';
import { QueryPlan } from '@apollo/query-planner';
import { OperationContext } from './operationContext';
import { ServiceMap } from './executeQueryPlan';
export type ServiceEndpointDefinition = Pick<ServiceDefinition, 'name' | 'url'>;
export type Experimental_DidResolveQueryPlanCallback = ({
queryPlan,
serviceMap,
operationContext,
requestContext,
}: {
readonly queryPlan: QueryPlan;
readonly serviceMap: ServiceMap;
readonly operationContext: OperationContext;
readonly requestContext: GraphQLRequestContextExecutionDidStart<
Record<string, any>
>;
}) => void;
interface ImplementingServiceLocation {
name: string;
path: string;
}
export interface CompositionMetadata {
formatVersion: number;
id: string;
implementingServiceLocations: ImplementingServiceLocation[];
schemaHash: string;
}
export type Experimental_DidFailCompositionCallback = ({
errors,
serviceList,
compositionMetadata,
}: {
readonly errors: GraphQLError[];
readonly serviceList: ServiceDefinition[];
readonly compositionMetadata?: CompositionMetadata;
}) => void;
export interface ServiceDefinitionCompositionInfo {
serviceDefinitions: ServiceDefinition[];
schema: GraphQLSchema;
compositionMetadata?: CompositionMetadata;
}
export interface SupergraphSdlCompositionInfo {
schema: GraphQLSchema;
compositionId: string;
supergraphSdl: string;
}
export type CompositionInfo =
| ServiceDefinitionCompositionInfo
| SupergraphSdlCompositionInfo;
export type Experimental_DidUpdateCompositionCallback = (
currentConfig: CompositionInfo,
previousConfig?: CompositionInfo,
) => void;
export type CompositionUpdate = ServiceDefinitionUpdate | SupergraphSdlUpdate;
export interface ServiceDefinitionUpdate {
serviceDefinitions?: ServiceDefinition[];
compositionMetadata?: CompositionMetadata;
isNewSchema: boolean;
}
export interface SupergraphSdlUpdate {
id: string;
supergraphSdl: string;
}
export function isSupergraphSdlUpdate(
update: CompositionUpdate,
): update is SupergraphSdlUpdate {
return 'supergraphSdl' in update;
}
export function isServiceDefinitionUpdate(
update: CompositionUpdate,
): update is ServiceDefinitionUpdate {
return 'isNewSchema' in update;
}
/**
* **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: DynamicGatewayConfig,
) => Promise<ServiceDefinitionUpdate>;
export type Experimental_UpdateSupergraphSdl = (
config: DynamicGatewayConfig,
) => Promise<SupergraphSdlUpdate>;
export type Experimental_UpdateComposition = (
config: DynamicGatewayConfig,
) => Promise<CompositionUpdate>;
interface GatewayConfigBase {
debug?: boolean;
logger?: Logger;
// 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_didUpdateComposition?: Experimental_DidUpdateCompositionCallback;
experimental_pollInterval?: number;
experimental_approximateQueryPlanStoreMiB?: number;
experimental_autoFragmentization?: boolean;
fetcher?: typeof fetch;
serviceHealthCheck?: boolean;
}
// TODO(trevor:removeServiceList)
export interface RemoteGatewayConfig extends GatewayConfigBase {
// @deprecated: use `supergraphSdl` in its function form instead
serviceList: ServiceEndpointDefinition[];
// @deprecated: use `supergraphSdl` in its function form instead
introspectionHeaders?:
| HeadersInit
| ((
service: ServiceEndpointDefinition,
) => Promise<HeadersInit> | HeadersInit);
}
export interface ManagedGatewayConfig extends GatewayConfigBase {
/**
* This configuration option shouldn't be used unless by recommendation from
* Apollo staff.
*/
schemaConfigDeliveryEndpoint?: string;
}
// TODO(trevor:removeServiceList): migrate users to `supergraphSdl` function option
interface ManuallyManagedServiceDefsGatewayConfig extends GatewayConfigBase {
// @deprecated: use `supergraphSdl` in its function form instead
experimental_updateServiceDefinitions: Experimental_UpdateServiceDefinitions;
}
// TODO(trevor:removeServiceList): migrate users to `supergraphSdl` function option
interface ExperimentalManuallyManagedSupergraphSdlGatewayConfig
extends GatewayConfigBase {
// @deprecated: use `supergraphSdl` in its function form instead
experimental_updateSupergraphSdl: Experimental_UpdateSupergraphSdl;
}
export function isManuallyManagedSupergraphSdlGatewayConfig(
config: GatewayConfig,
): config is ManuallyManagedSupergraphSdlGatewayConfig {
return (
'supergraphSdl' in config && typeof config.supergraphSdl === 'function'
);
}
interface ManuallyManagedSupergraphSdlGatewayConfig extends GatewayConfigBase {
supergraphSdl: (
update: (updatedSupergraphSdl: string) => Promise<void>,
) => Promise<{ supergraphSdl: string; cleanup?: () => Promise<void> }>;
}
type ManuallyManagedGatewayConfig =
| ManuallyManagedServiceDefsGatewayConfig
| ExperimentalManuallyManagedSupergraphSdlGatewayConfig
| ManuallyManagedSupergraphSdlGatewayConfig;
// TODO(trevor:removeServiceList)
interface LocalGatewayConfig extends GatewayConfigBase {
// @deprecated: use `supergraphSdl` in its function form instead
localServiceList: ServiceDefinition[];
}
interface StaticSupergraphSdlGatewayConfig extends GatewayConfigBase {
supergraphSdl: string;
}
export type StaticGatewayConfig =
| LocalGatewayConfig
| StaticSupergraphSdlGatewayConfig;
type DynamicGatewayConfig =
| ManagedGatewayConfig
| RemoteGatewayConfig
| ManuallyManagedGatewayConfig;
export type GatewayConfig = StaticGatewayConfig | DynamicGatewayConfig;
// TODO(trevor:removeServiceList)
export function isLocalConfig(
config: GatewayConfig,
): config is LocalGatewayConfig {
return 'localServiceList' in config;
}
// TODO(trevor:removeServiceList)
export function isRemoteConfig(
config: GatewayConfig,
): config is RemoteGatewayConfig {
return 'serviceList' in config;
}
export function isStaticSupergraphSdlConfig(
config: GatewayConfig,
): config is StaticSupergraphSdlGatewayConfig {
return 'supergraphSdl' in config && typeof config.supergraphSdl === 'string';
}
// A manually managed config means the user has provided a function which
// handles providing service definitions to the gateway.
export function isManuallyManagedConfig(
config: GatewayConfig,
): config is ManuallyManagedGatewayConfig {
return (
('supergraphSdl' in config && typeof config.supergraphSdl === 'function') ||
'experimental_updateServiceDefinitions' in config ||
'experimental_updateSupergraphSdl' in config
);
}
// Managed config strictly means managed by Studio
export function isManagedConfig(
config: GatewayConfig,
): config is ManagedGatewayConfig {
return (
'schemaConfigDeliveryEndpoint' in config ||
(!isRemoteConfig(config) &&
!isLocalConfig(config) &&
!isStaticSupergraphSdlConfig(config) &&
!isManuallyManagedConfig(config))
);
}
// A static config is one which loads synchronously on start and never updates
export function isStaticConfig(
config: GatewayConfig,
): config is StaticGatewayConfig {
return isLocalConfig(config) || isStaticSupergraphSdlConfig(config);
}
// A dynamic config is one which loads asynchronously and (can) update via polling
export function isDynamicConfig(
config: GatewayConfig,
): config is DynamicGatewayConfig {
return (
isRemoteConfig(config) ||
isManagedConfig(config) ||
isManuallyManagedConfig(config)
);
}