@apollo/gateway 2.0.0-alpha.2 → 2.0.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. package/dist/config.d.ts +43 -15
  2. package/dist/config.d.ts.map +1 -1
  3. package/dist/config.js +28 -18
  4. package/dist/config.js.map +1 -1
  5. package/dist/datasources/LocalGraphQLDataSource.js.map +1 -1
  6. package/dist/datasources/RemoteGraphQLDataSource.d.ts.map +1 -1
  7. package/dist/datasources/RemoteGraphQLDataSource.js +4 -1
  8. package/dist/datasources/RemoteGraphQLDataSource.js.map +1 -1
  9. package/dist/executeQueryPlan.d.ts.map +1 -1
  10. package/dist/executeQueryPlan.js +2 -2
  11. package/dist/executeQueryPlan.js.map +1 -1
  12. package/dist/index.d.ts +37 -22
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +199 -283
  15. package/dist/index.js.map +1 -1
  16. package/dist/schema-helper/addResolversToSchema.d.ts +4 -0
  17. package/dist/schema-helper/addResolversToSchema.d.ts.map +1 -0
  18. package/dist/schema-helper/addResolversToSchema.js +62 -0
  19. package/dist/schema-helper/addResolversToSchema.js.map +1 -0
  20. package/dist/schema-helper/error.d.ts +6 -0
  21. package/dist/schema-helper/error.d.ts.map +1 -0
  22. package/dist/schema-helper/error.js +14 -0
  23. package/dist/schema-helper/error.js.map +1 -0
  24. package/dist/schema-helper/index.d.ts +4 -0
  25. package/dist/schema-helper/index.d.ts.map +1 -0
  26. package/dist/schema-helper/index.js +16 -0
  27. package/dist/schema-helper/index.js.map +1 -0
  28. package/dist/schema-helper/resolverMap.d.ts +16 -0
  29. package/dist/schema-helper/resolverMap.d.ts.map +1 -0
  30. package/dist/schema-helper/resolverMap.js +3 -0
  31. package/dist/schema-helper/resolverMap.js.map +1 -0
  32. package/dist/supergraphManagers/IntrospectAndCompose/index.d.ts +31 -0
  33. package/dist/supergraphManagers/IntrospectAndCompose/index.d.ts.map +1 -0
  34. package/dist/supergraphManagers/IntrospectAndCompose/index.js +112 -0
  35. package/dist/supergraphManagers/IntrospectAndCompose/index.js.map +1 -0
  36. package/dist/supergraphManagers/IntrospectAndCompose/loadServicesFromRemoteEndpoint.d.ts +12 -0
  37. package/dist/supergraphManagers/IntrospectAndCompose/loadServicesFromRemoteEndpoint.d.ts.map +1 -0
  38. package/dist/{loadServicesFromRemoteEndpoint.js → supergraphManagers/IntrospectAndCompose/loadServicesFromRemoteEndpoint.js} +6 -6
  39. package/dist/supergraphManagers/IntrospectAndCompose/loadServicesFromRemoteEndpoint.js.map +1 -0
  40. package/dist/supergraphManagers/LegacyFetcher/index.d.ts +33 -0
  41. package/dist/supergraphManagers/LegacyFetcher/index.d.ts.map +1 -0
  42. package/dist/supergraphManagers/LegacyFetcher/index.js +149 -0
  43. package/dist/supergraphManagers/LegacyFetcher/index.js.map +1 -0
  44. package/dist/supergraphManagers/LocalCompose/index.d.ts +19 -0
  45. package/dist/supergraphManagers/LocalCompose/index.d.ts.map +1 -0
  46. package/dist/supergraphManagers/LocalCompose/index.js +55 -0
  47. package/dist/supergraphManagers/LocalCompose/index.js.map +1 -0
  48. package/dist/supergraphManagers/UplinkFetcher/index.d.ts +33 -0
  49. package/dist/supergraphManagers/UplinkFetcher/index.d.ts.map +1 -0
  50. package/dist/supergraphManagers/UplinkFetcher/index.js +98 -0
  51. package/dist/supergraphManagers/UplinkFetcher/index.js.map +1 -0
  52. package/dist/supergraphManagers/UplinkFetcher/loadSupergraphSdlFromStorage.d.ts +25 -0
  53. package/dist/supergraphManagers/UplinkFetcher/loadSupergraphSdlFromStorage.d.ts.map +1 -0
  54. package/dist/{loadSupergraphSdlFromStorage.js → supergraphManagers/UplinkFetcher/loadSupergraphSdlFromStorage.js} +40 -15
  55. package/dist/supergraphManagers/UplinkFetcher/loadSupergraphSdlFromStorage.js.map +1 -0
  56. package/dist/supergraphManagers/UplinkFetcher/outOfBandReporter.d.ts +13 -0
  57. package/dist/supergraphManagers/UplinkFetcher/outOfBandReporter.d.ts.map +1 -0
  58. package/dist/supergraphManagers/UplinkFetcher/outOfBandReporter.js +85 -0
  59. package/dist/supergraphManagers/UplinkFetcher/outOfBandReporter.js.map +1 -0
  60. package/dist/supergraphManagers/index.d.ts +6 -0
  61. package/dist/supergraphManagers/index.d.ts.map +1 -0
  62. package/dist/supergraphManagers/index.js +14 -0
  63. package/dist/supergraphManagers/index.js.map +1 -0
  64. package/dist/utilities/createHash.d.ts +2 -0
  65. package/dist/utilities/createHash.d.ts.map +1 -0
  66. package/dist/utilities/createHash.js +15 -0
  67. package/dist/utilities/createHash.js.map +1 -0
  68. package/dist/utilities/isNodeLike.d.ts +3 -0
  69. package/dist/utilities/isNodeLike.d.ts.map +1 -0
  70. package/dist/utilities/isNodeLike.js +8 -0
  71. package/dist/utilities/isNodeLike.js.map +1 -0
  72. package/package.json +9 -7
  73. package/src/__mocks__/make-fetch-happen-fetcher.ts +3 -1
  74. package/src/__tests__/build-query-plan.feature +52 -0
  75. package/src/__tests__/executeQueryPlan.test.ts +599 -1
  76. package/src/__tests__/execution-utils.ts +3 -3
  77. package/src/__tests__/gateway/buildService.test.ts +3 -3
  78. package/src/__tests__/gateway/endToEnd.test.ts +1 -1
  79. package/src/__tests__/gateway/executor.test.ts +1 -1
  80. package/src/__tests__/gateway/lifecycle-hooks.test.ts +59 -125
  81. package/src/__tests__/gateway/opentelemetry.test.ts +8 -4
  82. package/src/__tests__/gateway/queryPlanCache.test.ts +25 -12
  83. package/src/__tests__/gateway/reporting.test.ts +42 -13
  84. package/src/__tests__/gateway/supergraphSdl.test.ts +397 -0
  85. package/src/__tests__/integration/aliases.test.ts +9 -4
  86. package/src/__tests__/integration/configuration.test.ts +146 -9
  87. package/src/__tests__/integration/logger.test.ts +1 -1
  88. package/src/__tests__/integration/networkRequests.test.ts +99 -147
  89. package/src/__tests__/integration/nockMocks.ts +30 -16
  90. package/src/__tests__/nockAssertions.ts +20 -0
  91. package/src/config.ts +149 -38
  92. package/src/datasources/LocalGraphQLDataSource.ts +1 -1
  93. package/src/datasources/RemoteGraphQLDataSource.ts +8 -2
  94. package/src/datasources/__tests__/LocalGraphQLDataSource.test.ts +1 -1
  95. package/src/datasources/__tests__/RemoteGraphQLDataSource.test.ts +4 -4
  96. package/src/executeQueryPlan.ts +14 -2
  97. package/src/index.ts +325 -452
  98. package/src/schema-helper/addResolversToSchema.ts +83 -0
  99. package/src/schema-helper/error.ts +11 -0
  100. package/src/schema-helper/index.ts +3 -0
  101. package/src/schema-helper/resolverMap.ts +23 -0
  102. package/src/supergraphManagers/IntrospectAndCompose/__tests__/IntrospectAndCompose.test.ts +370 -0
  103. package/src/{__tests__ → supergraphManagers/IntrospectAndCompose/__tests__}/loadServicesFromRemoteEndpoint.test.ts +5 -5
  104. package/src/supergraphManagers/IntrospectAndCompose/__tests__/tsconfig.json +8 -0
  105. package/src/supergraphManagers/IntrospectAndCompose/index.ts +160 -0
  106. package/src/{loadServicesFromRemoteEndpoint.ts → supergraphManagers/IntrospectAndCompose/loadServicesFromRemoteEndpoint.ts} +6 -6
  107. package/src/supergraphManagers/LegacyFetcher/index.ts +226 -0
  108. package/src/supergraphManagers/LocalCompose/index.ts +79 -0
  109. package/src/supergraphManagers/UplinkFetcher/__tests__/loadSupergraphSdlFromStorage.test.ts +397 -0
  110. package/src/supergraphManagers/UplinkFetcher/__tests__/tsconfig.json +8 -0
  111. package/src/supergraphManagers/UplinkFetcher/index.ts +130 -0
  112. package/src/{loadSupergraphSdlFromStorage.ts → supergraphManagers/UplinkFetcher/loadSupergraphSdlFromStorage.ts} +68 -17
  113. package/src/supergraphManagers/UplinkFetcher/outOfBandReporter.ts +126 -0
  114. package/src/supergraphManagers/index.ts +5 -0
  115. package/src/utilities/__tests__/cleanErrorOfInaccessibleElements.test.ts +12 -9
  116. package/src/utilities/createHash.ts +10 -0
  117. package/src/utilities/isNodeLike.ts +11 -0
  118. package/dist/loadServicesFromRemoteEndpoint.d.ts +0 -13
  119. package/dist/loadServicesFromRemoteEndpoint.d.ts.map +0 -1
  120. package/dist/loadServicesFromRemoteEndpoint.js.map +0 -1
  121. package/dist/loadSupergraphSdlFromStorage.d.ts +0 -13
  122. package/dist/loadSupergraphSdlFromStorage.d.ts.map +0 -1
  123. package/dist/loadSupergraphSdlFromStorage.js.map +0 -1
  124. package/dist/outOfBandReporter.d.ts +0 -15
  125. package/dist/outOfBandReporter.d.ts.map +0 -1
  126. package/dist/outOfBandReporter.js +0 -88
  127. package/dist/outOfBandReporter.js.map +0 -1
  128. package/src/__tests__/gateway/composedSdl.test.ts +0 -44
  129. package/src/__tests__/loadSupergraphSdlFromStorage.test.ts +0 -694
  130. package/src/outOfBandReporter.ts +0 -128
@@ -0,0 +1,397 @@
1
+ import {
2
+ ApolloGateway,
3
+ RemoteGraphQLDataSource,
4
+ SubgraphHealthCheckFunction,
5
+ SupergraphSdlUpdateFunction,
6
+ } from '@apollo/gateway';
7
+ import { fixturesWithUpdate } from 'apollo-federation-integration-testsuite';
8
+ import { createHash } from '../../utilities/createHash';
9
+ import { ApolloServer } from 'apollo-server';
10
+ import { Logger } from 'apollo-server-types';
11
+ import { fetch } from '../../__mocks__/make-fetch-happen-fetcher';
12
+ import { getTestingSupergraphSdl } from '../execution-utils';
13
+ import { mockAllServicesHealthCheckSuccess } from '../integration/nockMocks';
14
+ import resolvable from '@josephg/resolvable';
15
+ import { nockAfterEach, nockBeforeEach } from '../nockAssertions';
16
+
17
+ async function getSupergraphSdlGatewayServer() {
18
+ const server = new ApolloServer({
19
+ gateway: new ApolloGateway({
20
+ supergraphSdl: getTestingSupergraphSdl(),
21
+ buildService({ url }) {
22
+ return new RemoteGraphQLDataSource({
23
+ url,
24
+ fetcher: fetch,
25
+ });
26
+ },
27
+ }),
28
+ });
29
+
30
+ await server.listen({ port: 0 });
31
+ return server;
32
+ }
33
+
34
+ let logger: Logger;
35
+ let gateway: ApolloGateway | null;
36
+ beforeEach(() => {
37
+ nockBeforeEach();
38
+
39
+ logger = {
40
+ debug: jest.fn(),
41
+ info: jest.fn(),
42
+ warn: jest.fn(),
43
+ error: jest.fn(),
44
+ };
45
+ });
46
+
47
+ afterEach(async () => {
48
+ nockAfterEach();
49
+
50
+ if (gateway) {
51
+ await gateway.stop();
52
+ gateway = null;
53
+ }
54
+ });
55
+
56
+ describe('Using supergraphSdl static configuration', () => {
57
+ it('successfully starts and serves requests to the proper services', async () => {
58
+ const server = await getSupergraphSdlGatewayServer();
59
+
60
+ fetch.mockJSONResponseOnce({
61
+ data: { me: { username: '@jbaxleyiii' } },
62
+ });
63
+
64
+ const result = await server.executeOperation({
65
+ query: '{ me { username } }',
66
+ });
67
+
68
+ expect(result.data).toMatchInlineSnapshot(`
69
+ Object {
70
+ "me": Object {
71
+ "username": "@jbaxleyiii",
72
+ },
73
+ }
74
+ `);
75
+
76
+ const [url, request] = fetch.mock.calls[0];
77
+ expect(url).toEqual('https://accounts.api.com.invalid');
78
+ expect(request?.body).toEqual(
79
+ JSON.stringify({ query: '{me{username}}', variables: {} }),
80
+ );
81
+ await server.stop();
82
+ });
83
+ });
84
+
85
+ describe('Using supergraphSdl dynamic configuration', () => {
86
+ it('calls the user provided function after `gateway.load()` is called', async () => {
87
+ const callbackSpy = jest.fn(async () => ({
88
+ supergraphSdl: getTestingSupergraphSdl(),
89
+ }));
90
+
91
+ gateway = new ApolloGateway({
92
+ supergraphSdl: callbackSpy,
93
+ });
94
+
95
+ expect(callbackSpy).not.toHaveBeenCalled();
96
+ await gateway.load();
97
+ expect(callbackSpy).toHaveBeenCalled();
98
+ });
99
+
100
+ it('starts and remains in `initialized` state until `supergraphSdl` Promise resolves', async () => {
101
+ const promiseGuaranteeingWeAreInTheCallback = resolvable();
102
+ const promiseGuaranteeingWeStayInTheCallback = resolvable();
103
+
104
+ gateway = new ApolloGateway({
105
+ async supergraphSdl() {
106
+ promiseGuaranteeingWeAreInTheCallback.resolve();
107
+ await promiseGuaranteeingWeStayInTheCallback;
108
+ return {
109
+ supergraphSdl: getTestingSupergraphSdl(),
110
+ };
111
+ },
112
+ });
113
+
114
+ expect(gateway.__testing().state.phase).toEqual('initialized');
115
+
116
+ const gatewayLoaded = gateway.load();
117
+ await promiseGuaranteeingWeAreInTheCallback;
118
+ expect(gateway.__testing().state.phase).toEqual('initialized');
119
+
120
+ promiseGuaranteeingWeStayInTheCallback.resolve();
121
+ await gatewayLoaded;
122
+ expect(gateway.__testing().state.phase).toEqual('loaded');
123
+ });
124
+
125
+ it('moves from `initialized` to `loaded` state after calling `load()` and after user Promise resolves', async () => {
126
+ const userPromise = resolvable<{ supergraphSdl: string }>();
127
+
128
+ gateway = new ApolloGateway({
129
+ async supergraphSdl() {
130
+ return userPromise;
131
+ },
132
+ });
133
+
134
+ const loadPromise = gateway.load();
135
+ expect(gateway.__testing().state.phase).toEqual('initialized');
136
+
137
+ const supergraphSdl = getTestingSupergraphSdl();
138
+ const expectedCompositionId = createHash('sha256')
139
+ .update(supergraphSdl)
140
+ .digest('hex');
141
+ userPromise.resolve({ supergraphSdl });
142
+
143
+ await loadPromise;
144
+ const { state, compositionId } = gateway.__testing();
145
+ expect(state.phase).toEqual('loaded');
146
+ expect(compositionId).toEqual(expectedCompositionId);
147
+ });
148
+
149
+ it('updates its supergraph after user calls update function', async () => {
150
+ const userPromise = resolvable<{ supergraphSdl: string }>();
151
+
152
+ let userUpdateFn: SupergraphSdlUpdateFunction;
153
+ gateway = new ApolloGateway({
154
+ async supergraphSdl({ update }) {
155
+ userUpdateFn = update;
156
+ return userPromise;
157
+ },
158
+ });
159
+
160
+ const supergraphSdl = getTestingSupergraphSdl();
161
+ const expectedId = createHash('sha256').update(supergraphSdl).digest('hex');
162
+ userPromise.resolve({ supergraphSdl: getTestingSupergraphSdl() });
163
+ await gateway.load();
164
+ expect(gateway.__testing().compositionId).toEqual(expectedId);
165
+
166
+ const updatedSupergraphSdl = getTestingSupergraphSdl(fixturesWithUpdate);
167
+ const expectedUpdatedId = createHash('sha256')
168
+ .update(updatedSupergraphSdl)
169
+ .digest('hex');
170
+
171
+ userUpdateFn!(updatedSupergraphSdl);
172
+ expect(gateway.__testing().compositionId).toEqual(expectedUpdatedId);
173
+ });
174
+
175
+ it('calls user-provided `cleanup` function when stopped', async () => {
176
+ const cleanup = jest.fn(() => Promise.resolve());
177
+ gateway = new ApolloGateway({
178
+ async supergraphSdl() {
179
+ return {
180
+ supergraphSdl: getTestingSupergraphSdl(),
181
+ cleanup,
182
+ };
183
+ },
184
+ });
185
+
186
+ await gateway.load();
187
+ const { state, compositionId } = gateway.__testing();
188
+ expect(state.phase).toEqual('loaded');
189
+ expect(compositionId).toEqual(
190
+ 'd20cfb7a9c51179aa494ed9e98153f0042892bd225437af064bf1c1aa68eab86',
191
+ );
192
+
193
+ await gateway.stop();
194
+ expect(cleanup).toHaveBeenCalledTimes(1);
195
+ });
196
+
197
+ it('performs a successful health check on subgraphs', async () => {
198
+ mockAllServicesHealthCheckSuccess();
199
+
200
+ let healthCheckCallback: SubgraphHealthCheckFunction;
201
+ const supergraphSdl = getTestingSupergraphSdl();
202
+ gateway = new ApolloGateway({
203
+ async supergraphSdl({ healthCheck }) {
204
+ healthCheckCallback = healthCheck;
205
+ return {
206
+ supergraphSdl,
207
+ };
208
+ },
209
+ });
210
+
211
+ await gateway.load();
212
+ const { state, compositionId } = gateway.__testing();
213
+ expect(state.phase).toEqual('loaded');
214
+ expect(compositionId).toEqual(
215
+ 'd20cfb7a9c51179aa494ed9e98153f0042892bd225437af064bf1c1aa68eab86',
216
+ );
217
+
218
+ await expect(healthCheckCallback!(supergraphSdl)).resolves.toBeUndefined();
219
+ });
220
+
221
+ it('calls `initialize` on an object provided to `supergraphSdl`', async () => {
222
+ const MockSdlUpdatingClass = {
223
+ initialize() {
224
+ return Promise.resolve({
225
+ supergraphSdl: getTestingSupergraphSdl(),
226
+ });
227
+ },
228
+ };
229
+ const initializeSpy = jest.spyOn(MockSdlUpdatingClass, 'initialize');
230
+
231
+ gateway = new ApolloGateway({
232
+ supergraphSdl: MockSdlUpdatingClass,
233
+ });
234
+
235
+ expect(initializeSpy).not.toHaveBeenCalled();
236
+ await gateway.load();
237
+ expect(initializeSpy).toHaveBeenCalled();
238
+ });
239
+
240
+ describe('errors', () => {
241
+ it('fails to load if `SupergraphManager` throws on initialization', async () => {
242
+ const failureMessage = 'Error from supergraphSdl function';
243
+ gateway = new ApolloGateway({
244
+ async supergraphSdl() {
245
+ throw new Error(failureMessage);
246
+ },
247
+ logger,
248
+ });
249
+
250
+ await expect(gateway.load()).rejects.toThrowError(failureMessage);
251
+
252
+ expect(gateway.__testing().state.phase).toEqual('failed to load');
253
+ // we don't want the `afterEach` to call `gateway.stop()` in this case
254
+ // since it would throw an error due to the gateway's failed to load state
255
+ gateway = null;
256
+ });
257
+
258
+ it('gracefully handles Promise rejections from user `cleanup` function', async () => {
259
+ const rejectionMessage = 'thrown from cleanup function';
260
+ const cleanup = jest.fn(() => Promise.reject(rejectionMessage));
261
+ gateway = new ApolloGateway({
262
+ async supergraphSdl() {
263
+ return {
264
+ supergraphSdl: getTestingSupergraphSdl(),
265
+ cleanup,
266
+ };
267
+ },
268
+ logger,
269
+ });
270
+
271
+ await gateway.load();
272
+ await expect(gateway.stop()).resolves.toBeUndefined();
273
+ expect(cleanup).toHaveBeenCalledTimes(1);
274
+ expect(logger.error).toHaveBeenCalledWith(
275
+ 'Error occured while calling user provided `cleanup` function: ' +
276
+ rejectionMessage,
277
+ );
278
+ });
279
+
280
+ it('throws an error when `healthCheck` rejects', async () => {
281
+ // no mocks, so nock will reject
282
+ let healthCheckCallback: SubgraphHealthCheckFunction;
283
+ const supergraphSdl = getTestingSupergraphSdl();
284
+ gateway = new ApolloGateway({
285
+ async supergraphSdl({ healthCheck }) {
286
+ healthCheckCallback = healthCheck;
287
+ return {
288
+ supergraphSdl,
289
+ };
290
+ },
291
+ });
292
+
293
+ await gateway.load();
294
+ const { state, compositionId } = gateway.__testing();
295
+ expect(state.phase).toEqual('loaded');
296
+ expect(compositionId).toEqual(
297
+ 'd20cfb7a9c51179aa494ed9e98153f0042892bd225437af064bf1c1aa68eab86',
298
+ );
299
+
300
+ await expect(healthCheckCallback!(supergraphSdl)).rejects.toThrowError(
301
+ /The gateway subgraphs health check failed\. Updating to the provided `supergraphSdl` will likely result in future request failures to subgraphs\. The following error occurred during the health check/,
302
+ );
303
+ });
304
+
305
+ it('throws an error when `update` is called after gateway fails to load', async () => {
306
+ let updateCallback: SupergraphSdlUpdateFunction;
307
+ const supergraphSdl = getTestingSupergraphSdl();
308
+ gateway = new ApolloGateway({
309
+ async supergraphSdl({ update }) {
310
+ updateCallback = update;
311
+ return {
312
+ supergraphSdl: 'invalid SDL',
313
+ };
314
+ },
315
+ });
316
+
317
+ try {
318
+ await gateway.load();
319
+ } catch {}
320
+
321
+ expect(() =>
322
+ updateCallback!(supergraphSdl),
323
+ ).toThrowErrorMatchingInlineSnapshot(
324
+ `"Can't call \`update\` callback after gateway failed to load."`,
325
+ );
326
+
327
+ // gateway failed to load, so we don't want the `afterEach` to call `gateway.stop()`
328
+ gateway = null;
329
+ });
330
+
331
+ it('throws an error when `update` is called while an update is in progress', async () => {
332
+ let updateCallback: SupergraphSdlUpdateFunction;
333
+ const supergraphSdl = getTestingSupergraphSdl();
334
+ gateway = new ApolloGateway({
335
+ async supergraphSdl({ update }) {
336
+ updateCallback = update;
337
+ return {
338
+ supergraphSdl,
339
+ };
340
+ },
341
+ experimental_didUpdateSupergraph() {
342
+ updateCallback(getTestingSupergraphSdl(fixturesWithUpdate));
343
+ },
344
+ });
345
+
346
+ await expect(gateway.load()).rejects.toThrowErrorMatchingInlineSnapshot(
347
+ `"Can't call \`update\` callback while supergraph update is in progress."`,
348
+ );
349
+
350
+ // gateway failed to load, so we don't want the `afterEach` to call `gateway.stop()`
351
+ gateway = null;
352
+ });
353
+
354
+ it('throws an error when `update` is called after gateway is stopped', async () => {
355
+ let updateCallback: SupergraphSdlUpdateFunction;
356
+ const supergraphSdl = getTestingSupergraphSdl();
357
+ gateway = new ApolloGateway({
358
+ async supergraphSdl({ update }) {
359
+ updateCallback = update;
360
+ return {
361
+ supergraphSdl,
362
+ };
363
+ },
364
+ });
365
+
366
+ await gateway.load();
367
+ await gateway.stop();
368
+
369
+ expect(() =>
370
+ updateCallback!(getTestingSupergraphSdl(fixturesWithUpdate)),
371
+ ).toThrowErrorMatchingInlineSnapshot(
372
+ `"Can't call \`update\` callback after gateway has been stopped."`,
373
+ );
374
+ });
375
+
376
+ it('throws an error when `update` is called with an invalid supergraph', async () => {
377
+ let updateCallback: SupergraphSdlUpdateFunction;
378
+ const supergraphSdl = getTestingSupergraphSdl();
379
+ gateway = new ApolloGateway({
380
+ async supergraphSdl({ update }) {
381
+ updateCallback = update;
382
+ return {
383
+ supergraphSdl,
384
+ };
385
+ },
386
+ });
387
+
388
+ await gateway.load();
389
+
390
+ expect(() =>
391
+ updateCallback!('invalid SDL'),
392
+ ).toThrowErrorMatchingInlineSnapshot(
393
+ `"Syntax Error: Unexpected Name \\"invalid\\"."`,
394
+ );
395
+ });
396
+ });
397
+ });
@@ -141,12 +141,17 @@ it('supports aliases of nested fields on subservices', async () => {
141
141
 
142
142
  // TODO after we remove GraphQLExtensions from ApolloServer, this can go away
143
143
  it('supports aliases when using ApolloServer', async () => {
144
+ const localDataSources = Object.fromEntries(
145
+ fixtures.map((f) => [
146
+ f.name,
147
+ new LocalGraphQLDataSource(buildSubgraphSchema(f)),
148
+ ]),
149
+ );
150
+
144
151
  const gateway = new ApolloGateway({
145
152
  localServiceList: fixtures,
146
- buildService: service => {
147
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
148
- // @ts-ignore
149
- return new LocalGraphQLDataSource(buildSubgraphSchema([service]));
153
+ buildService(service) {
154
+ return localDataSources[service.name];
150
155
  },
151
156
  });
152
157
 
@@ -7,14 +7,16 @@ import {
7
7
  mockSdlQuerySuccess,
8
8
  mockSupergraphSdlRequestSuccess,
9
9
  mockApolloConfig,
10
- mockCloudConfigUrl,
10
+ mockCloudConfigUrl1,
11
+ mockCloudConfigUrl2,
12
+ mockCloudConfigUrl3,
11
13
  } from './nockMocks';
12
14
  import { getTestingSupergraphSdl } from '../execution-utils';
13
- import { MockService } from './networkRequests.test';
15
+ import { fixtures, Fixture } from 'apollo-federation-integration-testsuite';
14
16
 
15
17
  let logger: Logger;
16
18
 
17
- const service: MockService = {
19
+ const service: Fixture = {
18
20
  name: 'accounts',
19
21
  url: 'http://localhost:4001',
20
22
  typeDefs: gql`
@@ -111,7 +113,26 @@ describe('gateway configuration warnings', () => {
111
113
 
112
114
  gateway = new ApolloGateway({
113
115
  logger,
114
- schemaConfigDeliveryEndpoint: mockCloudConfigUrl,
116
+ uplinkEndpoints: [mockCloudConfigUrl1],
117
+ });
118
+
119
+ await gateway.load(mockApolloConfig);
120
+
121
+ await gateway.stop();
122
+
123
+ expect(logger.warn).not.toHaveBeenCalledWith(
124
+ expect.stringMatching(
125
+ /A local gateway configuration is overriding a managed federation configuration/,
126
+ ),
127
+ );
128
+ });
129
+
130
+ it('deprecated conflicting configurations are not warned about when absent', async () => {
131
+ mockSupergraphSdlRequestSuccess();
132
+
133
+ gateway = new ApolloGateway({
134
+ logger,
135
+ schemaConfigDeliveryEndpoint: mockCloudConfigUrl1,
115
136
  });
116
137
 
117
138
  await gateway.load(mockApolloConfig);
@@ -306,16 +327,132 @@ describe('gateway config / env behavior', () => {
306
327
  APOLLO_SCHEMA_CONFIG_DELIVERY_ENDPOINT: 'env-config',
307
328
  });
308
329
 
309
- gateway = new ApolloGateway({
330
+ const config = {
310
331
  logger,
311
- schemaConfigDeliveryEndpoint: 'code-config',
332
+ uplinkEndpoints: [mockCloudConfigUrl1, mockCloudConfigUrl2, mockCloudConfigUrl3],
333
+ };
334
+ gateway = new ApolloGateway(config);
335
+
336
+ expect(gateway['getUplinkEndpoints'](config)).toEqual([
337
+ mockCloudConfigUrl1,
338
+ mockCloudConfigUrl2,
339
+ mockCloudConfigUrl3,
340
+ ]);
341
+
342
+ gateway = null;
343
+ });
344
+ });
345
+
346
+ describe('deprecated schema config delivery endpoint configuration', () => {
347
+ it('A code config overrides the env variable', async () => {
348
+ cleanUp = mockedEnv({
349
+ APOLLO_SCHEMA_CONFIG_DELIVERY_ENDPOINT: 'env-config',
312
350
  });
313
351
 
314
- expect(gateway['schemaConfigDeliveryEndpoint']).toEqual(
315
- 'code-config',
316
- );
352
+ const config = {
353
+ logger,
354
+ schemaConfigDeliveryEndpoint: 'code-config',
355
+ };
356
+ gateway = new ApolloGateway(config);
357
+
358
+ expect(gateway['getUplinkEndpoints'](config)).toEqual(['code-config']);
317
359
 
318
360
  gateway = null;
319
361
  });
320
362
  });
321
363
  });
364
+
365
+ describe('deprecation warnings', () => {
366
+ it('warns with `experimental_updateSupergraphSdl` option set', async () => {
367
+ const gateway = new ApolloGateway({
368
+ async experimental_updateSupergraphSdl() {
369
+ return {
370
+ id: 'supergraph',
371
+ supergraphSdl: getTestingSupergraphSdl(),
372
+ };
373
+ },
374
+ logger,
375
+ });
376
+
377
+ await gateway.load();
378
+
379
+ expect(logger.warn).toHaveBeenCalledWith(
380
+ 'The `experimental_updateSupergraphSdl` option is deprecated and will be removed in a future version of `@apollo/gateway`. Please migrate to the function form of the `supergraphSdl` configuration option.',
381
+ );
382
+
383
+ await gateway.stop();
384
+ });
385
+
386
+ it('warns with `experimental_updateServiceDefinitions` option set', async () => {
387
+ const gateway = new ApolloGateway({
388
+ async experimental_updateServiceDefinitions() {
389
+ return {
390
+ isNewSchema: false,
391
+ };
392
+ },
393
+ logger,
394
+ });
395
+
396
+ try {
397
+ await gateway.load();
398
+ // gateway will throw since we're not providing an actual service list, disregard
399
+ } catch {}
400
+
401
+ expect(logger.warn).toHaveBeenCalledWith(
402
+ 'The `experimental_updateServiceDefinitions` option is deprecated and will be removed in a future version of `@apollo/gateway`. Please migrate to the function form of the `supergraphSdl` configuration option.',
403
+ );
404
+ });
405
+
406
+ it('warns with `serviceList` option set', async () => {
407
+ const gateway = new ApolloGateway({
408
+ serviceList: [{ name: 'accounts', url: 'http://localhost:4001' }],
409
+ logger,
410
+ });
411
+
412
+ try {
413
+ await gateway.load();
414
+ // gateway will throw since we haven't mocked these requests, unimportant for this test
415
+ } catch {}
416
+
417
+ expect(logger.warn).toHaveBeenCalledWith(
418
+ 'The `serviceList` option is deprecated and will be removed in a future version of `@apollo/gateway`. Please migrate to its replacement `IntrospectAndCompose`. More information on `IntrospectAndCompose` can be found in the documentation.',
419
+ );
420
+ });
421
+
422
+ it('warns with `localServiceList` option set', async () => {
423
+ const gateway = new ApolloGateway({
424
+ localServiceList: fixtures,
425
+ logger,
426
+ });
427
+
428
+ await gateway.load();
429
+
430
+ expect(logger.warn).toHaveBeenCalledWith(
431
+ 'The `localServiceList` option is deprecated and will be removed in a future version of `@apollo/gateway`. Please migrate to the `LocalCompose` supergraph manager exported by `@apollo/gateway`.',
432
+ );
433
+
434
+ await gateway.stop();
435
+ });
436
+
437
+ it('warns with `schemaConfigDeliveryEndpoint` option set', async () => {
438
+ new ApolloGateway({
439
+ schemaConfigDeliveryEndpoint: 'test',
440
+ logger,
441
+ });
442
+
443
+ expect(logger.warn).toHaveBeenCalledWith(
444
+ 'The `schemaConfigDeliveryEndpoint` option is deprecated and will be removed in a future version of `@apollo/gateway`. Please migrate to the equivalent (array form) `uplinkEndpoints` configuration option.',
445
+ );
446
+ });
447
+
448
+ it('warns with `experimental_pollInterval` option set', async () => {
449
+ new ApolloGateway({
450
+ experimental_pollInterval: 10000,
451
+ logger,
452
+ });
453
+
454
+ expect(logger.warn).toHaveBeenCalledWith(
455
+ 'The `experimental_pollInterval` option is deprecated and will be removed in a future version of `@apollo/gateway`. Please migrate to the equivalent `pollIntervalInMs` configuration option.',
456
+ );
457
+ });
458
+ });
@@ -10,7 +10,7 @@ import * as log4js from "log4js";
10
10
 
11
11
  const LOWEST_LOG_LEVEL = "debug";
12
12
 
13
- const KNOWN_DEBUG_MESSAGE = "Checking for composition updates...";
13
+ const KNOWN_DEBUG_MESSAGE = "Gateway successfully initialized (but not yet loaded)";
14
14
 
15
15
  async function triggerKnownDebugMessage(logger: Logger) {
16
16
  // Trigger a known error.