@apollo/gateway 2.0.0-alpha.0 → 2.0.0-alpha.4

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 (154) hide show
  1. package/README.md +1 -1
  2. package/dist/__generated__/graphqlTypes.d.ts +13 -11
  3. package/dist/__generated__/graphqlTypes.d.ts.map +1 -1
  4. package/dist/__generated__/graphqlTypes.js.map +1 -1
  5. package/dist/config.d.ts +45 -24
  6. package/dist/config.d.ts.map +1 -1
  7. package/dist/config.js +30 -31
  8. package/dist/config.js.map +1 -1
  9. package/dist/datasources/LocalGraphQLDataSource.js.map +1 -1
  10. package/dist/datasources/RemoteGraphQLDataSource.d.ts.map +1 -1
  11. package/dist/datasources/RemoteGraphQLDataSource.js +4 -1
  12. package/dist/datasources/RemoteGraphQLDataSource.js.map +1 -1
  13. package/dist/datasources/types.d.ts +1 -1
  14. package/dist/datasources/types.d.ts.map +1 -1
  15. package/dist/executeQueryPlan.d.ts.map +1 -1
  16. package/dist/executeQueryPlan.js +6 -6
  17. package/dist/executeQueryPlan.js.map +1 -1
  18. package/dist/index.d.ts +36 -23
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +197 -297
  21. package/dist/index.js.map +1 -1
  22. package/dist/operationContext.js +0 -1
  23. package/dist/operationContext.js.map +1 -1
  24. package/dist/schema-helper/addResolversToSchema.d.ts +4 -0
  25. package/dist/schema-helper/addResolversToSchema.d.ts.map +1 -0
  26. package/dist/schema-helper/addResolversToSchema.js +62 -0
  27. package/dist/schema-helper/addResolversToSchema.js.map +1 -0
  28. package/dist/schema-helper/error.d.ts +6 -0
  29. package/dist/schema-helper/error.d.ts.map +1 -0
  30. package/dist/schema-helper/error.js +14 -0
  31. package/dist/schema-helper/error.js.map +1 -0
  32. package/dist/schema-helper/index.d.ts +4 -0
  33. package/dist/schema-helper/index.d.ts.map +1 -0
  34. package/dist/schema-helper/index.js +16 -0
  35. package/dist/schema-helper/index.js.map +1 -0
  36. package/dist/schema-helper/resolverMap.d.ts +16 -0
  37. package/dist/schema-helper/resolverMap.d.ts.map +1 -0
  38. package/dist/schema-helper/resolverMap.js +3 -0
  39. package/dist/schema-helper/resolverMap.js.map +1 -0
  40. package/dist/supergraphManagers/IntrospectAndCompose/index.d.ts +31 -0
  41. package/dist/supergraphManagers/IntrospectAndCompose/index.d.ts.map +1 -0
  42. package/dist/supergraphManagers/IntrospectAndCompose/index.js +112 -0
  43. package/dist/supergraphManagers/IntrospectAndCompose/index.js.map +1 -0
  44. package/dist/supergraphManagers/IntrospectAndCompose/loadServicesFromRemoteEndpoint.d.ts +12 -0
  45. package/dist/supergraphManagers/IntrospectAndCompose/loadServicesFromRemoteEndpoint.d.ts.map +1 -0
  46. package/dist/{loadServicesFromRemoteEndpoint.js → supergraphManagers/IntrospectAndCompose/loadServicesFromRemoteEndpoint.js} +6 -6
  47. package/dist/supergraphManagers/IntrospectAndCompose/loadServicesFromRemoteEndpoint.js.map +1 -0
  48. package/dist/supergraphManagers/LegacyFetcher/index.d.ts +33 -0
  49. package/dist/supergraphManagers/LegacyFetcher/index.d.ts.map +1 -0
  50. package/dist/supergraphManagers/LegacyFetcher/index.js +149 -0
  51. package/dist/supergraphManagers/LegacyFetcher/index.js.map +1 -0
  52. package/dist/supergraphManagers/LocalCompose/index.d.ts +19 -0
  53. package/dist/supergraphManagers/LocalCompose/index.d.ts.map +1 -0
  54. package/dist/supergraphManagers/LocalCompose/index.js +55 -0
  55. package/dist/supergraphManagers/LocalCompose/index.js.map +1 -0
  56. package/dist/supergraphManagers/UplinkFetcher/index.d.ts +32 -0
  57. package/dist/supergraphManagers/UplinkFetcher/index.d.ts.map +1 -0
  58. package/dist/supergraphManagers/UplinkFetcher/index.js +96 -0
  59. package/dist/supergraphManagers/UplinkFetcher/index.js.map +1 -0
  60. package/dist/supergraphManagers/UplinkFetcher/loadSupergraphSdlFromStorage.d.ts +21 -0
  61. package/dist/supergraphManagers/UplinkFetcher/loadSupergraphSdlFromStorage.d.ts.map +1 -0
  62. package/dist/{loadSupergraphSdlFromStorage.js → supergraphManagers/UplinkFetcher/loadSupergraphSdlFromStorage.js} +41 -10
  63. package/dist/supergraphManagers/UplinkFetcher/loadSupergraphSdlFromStorage.js.map +1 -0
  64. package/dist/supergraphManagers/UplinkFetcher/outOfBandReporter.d.ts +13 -0
  65. package/dist/supergraphManagers/UplinkFetcher/outOfBandReporter.d.ts.map +1 -0
  66. package/dist/supergraphManagers/UplinkFetcher/outOfBandReporter.js +85 -0
  67. package/dist/supergraphManagers/UplinkFetcher/outOfBandReporter.js.map +1 -0
  68. package/dist/supergraphManagers/index.d.ts +5 -0
  69. package/dist/supergraphManagers/index.d.ts.map +1 -0
  70. package/dist/supergraphManagers/index.js +12 -0
  71. package/dist/supergraphManagers/index.js.map +1 -0
  72. package/dist/utilities/array.js +1 -1
  73. package/dist/utilities/array.js.map +1 -1
  74. package/dist/utilities/createHash.d.ts +2 -0
  75. package/dist/utilities/createHash.d.ts.map +1 -0
  76. package/dist/utilities/createHash.js +15 -0
  77. package/dist/utilities/createHash.js.map +1 -0
  78. package/dist/utilities/isNodeLike.d.ts +3 -0
  79. package/dist/utilities/isNodeLike.d.ts.map +1 -0
  80. package/dist/utilities/isNodeLike.js +8 -0
  81. package/dist/utilities/isNodeLike.js.map +1 -0
  82. package/package.json +9 -9
  83. package/src/__generated__/graphqlTypes.ts +13 -11
  84. package/src/__mocks__/make-fetch-happen-fetcher.ts +3 -1
  85. package/src/__tests__/buildQueryPlan.test.ts +1 -1
  86. package/src/__tests__/executeQueryPlan.test.ts +1171 -77
  87. package/src/__tests__/execution-utils.ts +5 -7
  88. package/src/__tests__/gateway/buildService.test.ts +3 -3
  89. package/src/__tests__/gateway/endToEnd.test.ts +1 -1
  90. package/src/__tests__/gateway/executor.test.ts +3 -1
  91. package/src/__tests__/gateway/lifecycle-hooks.test.ts +59 -121
  92. package/src/__tests__/gateway/opentelemetry.test.ts +8 -3
  93. package/src/__tests__/gateway/queryPlanCache.test.ts +25 -9
  94. package/src/__tests__/gateway/reporting.test.ts +42 -13
  95. package/src/__tests__/gateway/supergraphSdl.test.ts +397 -0
  96. package/src/__tests__/integration/aliases.test.ts +9 -3
  97. package/src/__tests__/integration/configuration.test.ts +140 -21
  98. package/src/__tests__/integration/logger.test.ts +2 -2
  99. package/src/__tests__/integration/networkRequests.test.ts +126 -149
  100. package/src/__tests__/integration/nockMocks.ts +57 -16
  101. package/src/__tests__/nockAssertions.ts +20 -0
  102. package/src/config.ts +153 -77
  103. package/src/core/__tests__/core.test.ts +6 -6
  104. package/src/datasources/LocalGraphQLDataSource.ts +1 -1
  105. package/src/datasources/RemoteGraphQLDataSource.ts +8 -2
  106. package/src/datasources/__tests__/LocalGraphQLDataSource.test.ts +1 -1
  107. package/src/datasources/__tests__/RemoteGraphQLDataSource.test.ts +4 -4
  108. package/src/datasources/types.ts +1 -1
  109. package/src/executeQueryPlan.ts +18 -9
  110. package/src/index.ts +323 -481
  111. package/src/make-fetch-happen.d.ts +1 -1
  112. package/src/operationContext.ts +2 -2
  113. package/src/schema-helper/addResolversToSchema.ts +83 -0
  114. package/src/schema-helper/error.ts +11 -0
  115. package/src/schema-helper/index.ts +3 -0
  116. package/src/schema-helper/resolverMap.ts +23 -0
  117. package/src/supergraphManagers/IntrospectAndCompose/__tests__/IntrospectAndCompose.test.ts +370 -0
  118. package/src/{__tests__ → supergraphManagers/IntrospectAndCompose/__tests__}/loadServicesFromRemoteEndpoint.test.ts +7 -7
  119. package/src/supergraphManagers/IntrospectAndCompose/__tests__/tsconfig.json +8 -0
  120. package/src/supergraphManagers/IntrospectAndCompose/index.ts +160 -0
  121. package/src/{loadServicesFromRemoteEndpoint.ts → supergraphManagers/IntrospectAndCompose/loadServicesFromRemoteEndpoint.ts} +7 -7
  122. package/src/supergraphManagers/LegacyFetcher/index.ts +226 -0
  123. package/src/supergraphManagers/LocalCompose/index.ts +79 -0
  124. package/src/supergraphManagers/UplinkFetcher/__tests__/loadSupergraphSdlFromStorage.test.ts +343 -0
  125. package/src/supergraphManagers/UplinkFetcher/__tests__/tsconfig.json +8 -0
  126. package/src/supergraphManagers/UplinkFetcher/index.ts +128 -0
  127. package/src/{loadSupergraphSdlFromStorage.ts → supergraphManagers/UplinkFetcher/loadSupergraphSdlFromStorage.ts} +63 -14
  128. package/src/supergraphManagers/UplinkFetcher/outOfBandReporter.ts +126 -0
  129. package/src/supergraphManagers/index.ts +4 -0
  130. package/src/utilities/__tests__/cleanErrorOfInaccessibleElements.test.ts +13 -10
  131. package/src/utilities/array.ts +1 -1
  132. package/src/utilities/createHash.ts +10 -0
  133. package/src/utilities/isNodeLike.ts +11 -0
  134. package/CHANGELOG.md +0 -452
  135. package/dist/legacyLoadServicesFromStorage.d.ts +0 -20
  136. package/dist/legacyLoadServicesFromStorage.d.ts.map +0 -1
  137. package/dist/legacyLoadServicesFromStorage.js +0 -62
  138. package/dist/legacyLoadServicesFromStorage.js.map +0 -1
  139. package/dist/loadServicesFromRemoteEndpoint.d.ts +0 -13
  140. package/dist/loadServicesFromRemoteEndpoint.d.ts.map +0 -1
  141. package/dist/loadServicesFromRemoteEndpoint.js.map +0 -1
  142. package/dist/loadSupergraphSdlFromStorage.d.ts +0 -12
  143. package/dist/loadSupergraphSdlFromStorage.d.ts.map +0 -1
  144. package/dist/loadSupergraphSdlFromStorage.js.map +0 -1
  145. package/dist/outOfBandReporter.d.ts +0 -15
  146. package/dist/outOfBandReporter.d.ts.map +0 -1
  147. package/dist/outOfBandReporter.js +0 -88
  148. package/dist/outOfBandReporter.js.map +0 -1
  149. package/src/__tests__/gateway/composedSdl.test.ts +0 -44
  150. package/src/__tests__/integration/legacyNetworkRequests.test.ts +0 -279
  151. package/src/__tests__/integration/legacyNockMocks.ts +0 -113
  152. package/src/__tests__/loadSupergraphSdlFromStorage.test.ts +0 -664
  153. package/src/legacyLoadServicesFromStorage.ts +0 -170
  154. 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,11 +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
- // @ts-ignore
148
- return new LocalGraphQLDataSource(buildSubgraphSchema([service]));
153
+ buildService(service) {
154
+ return localDataSources[service.name];
149
155
  },
150
156
  });
151
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`
@@ -70,9 +72,10 @@ describe('gateway configuration warnings', () => {
70
72
 
71
73
  it('warns when both manual update configurations are provided', async () => {
72
74
  gateway = new ApolloGateway({
75
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
73
76
  // @ts-ignore
74
- async experimental_updateSupergraphSdl() {},
75
- async experimental_updateServiceDefinitions() {},
77
+ experimental_updateSupergraphSdl: async () => undefined,
78
+ experimental_updateServiceDefinitions: async () => undefined,
76
79
  logger,
77
80
  });
78
81
 
@@ -110,8 +113,26 @@ describe('gateway configuration warnings', () => {
110
113
 
111
114
  gateway = new ApolloGateway({
112
115
  logger,
113
- // TODO(trevor:cloudconfig): remove
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);
@@ -300,40 +321,138 @@ describe('gateway config / env behavior', () => {
300
321
  });
301
322
  });
302
323
 
303
- // TODO(trevor:cloudconfig): this behavior will be updated
304
324
  describe('schema config delivery endpoint configuration', () => {
305
325
  it('A code config overrides the env variable', async () => {
306
326
  cleanUp = mockedEnv({
307
327
  APOLLO_SCHEMA_CONFIG_DELIVERY_ENDPOINT: 'env-config',
308
328
  });
309
329
 
310
- gateway = new ApolloGateway({
330
+ const config = {
311
331
  logger,
312
- schemaConfigDeliveryEndpoint: 'code-config',
313
- });
332
+ uplinkEndpoints: [mockCloudConfigUrl1, mockCloudConfigUrl2, mockCloudConfigUrl3],
333
+ };
334
+ gateway = new ApolloGateway(config);
314
335
 
315
- expect(gateway['schemaConfigDeliveryEndpoint']).toEqual(
316
- 'code-config',
317
- );
336
+ expect(gateway['getUplinkEndpoints'](config)).toEqual([
337
+ mockCloudConfigUrl1,
338
+ mockCloudConfigUrl2,
339
+ mockCloudConfigUrl3,
340
+ ]);
318
341
 
319
342
  gateway = null;
320
343
  });
344
+ });
321
345
 
322
- it('A code config set to `null` takes precedence over an existing env variable', async () => {
346
+ describe('deprecated schema config delivery endpoint configuration', () => {
347
+ it('A code config overrides the env variable', async () => {
323
348
  cleanUp = mockedEnv({
324
349
  APOLLO_SCHEMA_CONFIG_DELIVERY_ENDPOINT: 'env-config',
325
350
  });
326
351
 
327
- gateway = new ApolloGateway({
352
+ const config = {
328
353
  logger,
329
- schemaConfigDeliveryEndpoint: null,
330
- });
354
+ schemaConfigDeliveryEndpoint: 'code-config',
355
+ };
356
+ gateway = new ApolloGateway(config);
331
357
 
332
- expect(gateway['schemaConfigDeliveryEndpoint']).toEqual(
333
- null,
334
- );
358
+ expect(gateway['getUplinkEndpoints'](config)).toEqual(['code-config']);
335
359
 
336
360
  gateway = null;
337
361
  });
338
362
  });
339
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.
@@ -18,7 +18,7 @@ async function triggerKnownDebugMessage(logger: Logger) {
18
18
  // message outside of the constructor, but it seemed worth testing
19
19
  // the compatibility with `ApolloGateway` itself rather than generically.
20
20
  // The error does not matter, so it is caught and ignored.
21
- await new ApolloGateway({ logger }).load().catch(_e => {});
21
+ await new ApolloGateway({ logger }).load().catch(_e => undefined);
22
22
  }
23
23
 
24
24
  describe("logger", () => {