-
Notifications
You must be signed in to change notification settings - Fork 242
/
supergraphSdl.test.ts
219 lines (184 loc) 路 6.64 KB
/
supergraphSdl.test.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
import { ApolloGateway, SupergraphSdlUpdateFunction } from '@apollo/gateway';
import { fixturesWithUpdate } from 'apollo-federation-integration-testsuite';
import { createHash } from 'crypto';
import { ApolloServer } from 'apollo-server';
import { Logger } from 'apollo-server-types';
import { fetch } from '../../__mocks__/apollo-server-env';
import { getTestingSupergraphSdl, waitUntil } from '../execution-utils';
async function getSupergraphSdlGatewayServer() {
const server = new ApolloServer({
gateway: new ApolloGateway({
supergraphSdl: getTestingSupergraphSdl(),
}),
});
await server.listen({ port: 0 });
return server;
}
let logger: Logger;
beforeEach(() => {
logger = {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};
});
describe('Using supergraphSdl static configuration', () => {
it('successfully starts and serves requests to the proper services', async () => {
const server = await getSupergraphSdlGatewayServer();
fetch.mockJSONResponseOnce({
data: { me: { username: '@jbaxleyiii' } },
});
const result = await server.executeOperation({
query: '{ me { username } }',
});
expect(result.data).toMatchInlineSnapshot(`
Object {
"me": Object {
"username": "@jbaxleyiii",
},
}
`);
const [url, request] = fetch.mock.calls[0];
expect(url).toEqual('https://accounts.api.com');
expect(request?.body).toEqual(
JSON.stringify({ query: '{me{username}}', variables: {} }),
);
await server.stop();
});
});
describe('Using supergraphSdl dynamic configuration', () => {
it(`calls the user provided function after gateway.load() is called`, async () => {
const spy = jest.fn(async () => ({
supergraphSdl: getTestingSupergraphSdl(),
}));
const gateway = new ApolloGateway({
supergraphSdl: spy,
});
expect(spy).not.toHaveBeenCalled();
await gateway.load();
expect(spy).toHaveBeenCalled();
});
it('starts and remains in `initialized` state until user Promise resolves', async () => {
const [promise, resolve] = waitUntil();
const gateway = new ApolloGateway({
async supergraphSdl() {
await promise;
return {
supergraphSdl: getTestingSupergraphSdl(),
};
},
});
expect(gateway.__testing().state.phase).toEqual('initialized');
// If we await here, we'll get stuck.
const gatewayLoaded = gateway.load();
expect(gateway.__testing().state.phase).toEqual('initialized');
resolve();
await gatewayLoaded;
expect(gateway.__testing().state.phase).toEqual('loaded');
});
it('starts and waits in `initialized` state after calling load but before user Promise resolves', async () => {
const gateway = new ApolloGateway({
async supergraphSdl() {
return new Promise(() => {});
},
});
gateway.load();
expect(gateway.__testing().state.phase).toEqual('initialized');
});
it('moves from `initialized` to `loaded` state after calling `load()` and after user Promise resolves', async () => {
const [userPromise, resolveSupergraph] =
waitUntil<{ supergraphSdl: string }>();
const gateway = new ApolloGateway({
async supergraphSdl() {
return userPromise;
},
});
const loadPromise = gateway.load();
expect(gateway.__testing().state.phase).toEqual('initialized');
const supergraphSdl = getTestingSupergraphSdl();
const expectedCompositionId = createHash('sha256')
.update(supergraphSdl)
.digest('hex');
resolveSupergraph({ supergraphSdl });
await loadPromise;
const { state, compositionId } = gateway.__testing();
expect(state.phase).toEqual('loaded');
expect(compositionId).toEqual(expectedCompositionId);
});
it('updates its supergraph after user calls update function', async () => {
const [userPromise, resolveSupergraph] =
waitUntil<{ supergraphSdl: string }>();
let userUpdateFn: SupergraphSdlUpdateFunction;
const gateway = new ApolloGateway({
async supergraphSdl({ update }) {
userUpdateFn = update;
return userPromise;
},
});
const supergraphSdl = getTestingSupergraphSdl();
const expectedId = createHash('sha256').update(supergraphSdl).digest('hex');
resolveSupergraph({ supergraphSdl: getTestingSupergraphSdl() });
await gateway.load();
expect(gateway.__testing().compositionId).toEqual(expectedId);
const updatedSupergraphSdl = getTestingSupergraphSdl(fixturesWithUpdate);
const expectedUpdatedId = createHash('sha256')
.update(updatedSupergraphSdl)
.digest('hex');
await userUpdateFn!(updatedSupergraphSdl);
expect(gateway.__testing().compositionId).toEqual(expectedUpdatedId);
});
it('calls user-provided `cleanup` function when stopped', async () => {
const cleanup = jest.fn(() => Promise.resolve());
const gateway = new ApolloGateway({
async supergraphSdl() {
return {
supergraphSdl: getTestingSupergraphSdl(),
cleanup,
};
},
});
await gateway.load();
const { state, compositionId } = gateway.__testing();
expect(state.phase).toEqual('loaded');
expect(compositionId).toEqual(
'562c22b3382b56b1651944a96e89a361fe847b9b32660eae5ecbd12adc20bf8b',
);
await gateway.stop();
expect(cleanup).toHaveBeenCalledTimes(1);
});
describe('errors', () => {
it('fails to load if user-provided `supergraphSdl` function throws', async () => {
const failureMessage = 'Error from supergraphSdl function';
const gateway = new ApolloGateway({
async supergraphSdl() {
throw new Error(failureMessage);
},
logger,
});
await expect(() => gateway.load()).rejects.toThrowError(failureMessage);
expect(gateway.__testing().state.phase).toEqual('failed to load');
expect(logger.error).toHaveBeenCalledWith(failureMessage);
});
it('gracefully handles Promise rejections from user `cleanup` function', async () => {
const rejectionMessage = 'thrown from cleanup function';
const cleanup = jest.fn(() => Promise.reject(rejectionMessage));
const gateway = new ApolloGateway({
async supergraphSdl() {
return {
supergraphSdl: getTestingSupergraphSdl(),
cleanup,
};
},
logger,
});
await gateway.load();
await expect(gateway.stop()).resolves.toBeUndefined();
expect(cleanup).toHaveBeenCalledTimes(1);
expect(logger.error).toHaveBeenCalledWith(
'Error occured while calling user provided `cleanup` function: ' +
rejectionMessage,
);
});
});
});