Skip to content

Commit 06a5da9

Browse files
committed
add additional tests for mergeIncrementalResults
1 parent 73a52f5 commit 06a5da9

2 files changed

Lines changed: 146 additions & 19 deletions

File tree

packages/utils/src/mergeIncrementalResult.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,15 @@ export function mergeIncrementalResult({
1717
...(incrementalResult.path ?? []),
1818
];
1919

20-
if (incrementalResult.pending) {
21-
let paths = pathsMap.get(executionResult);
22-
if (paths === undefined) {
23-
paths = new Map();
24-
pathsMap.set(executionResult, paths);
25-
}
26-
27-
for (const { id, path } of incrementalResult.pending) {
28-
paths.set(id, ['data', ...path]);
29-
}
30-
}
31-
32-
if (incrementalResult.pending) {
33-
const paths = pathsMap.get(executionResult);
20+
for (const result of [executionResult, incrementalResult]) {
21+
if (result.pending) {
22+
let paths = pathsMap.get(executionResult);
23+
if (paths === undefined) {
24+
paths = new Map();
25+
pathsMap.set(executionResult, paths);
26+
}
3427

35-
for (const { id, path } of incrementalResult.pending) {
36-
if (id !== undefined) {
37-
if (paths === undefined) {
38-
throw new Error('Invalid incremental delivery format.');
39-
}
28+
for (const { id, path } of result.pending) {
4029
paths.set(id, ['data', ...path]);
4130
}
4231
}

packages/utils/tests/mergeIncrementalResult.spec.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ describe('mergeIncrementalResult', () => {
2020
expect(executionResult).toEqual({ data: { user: { age: 42, name: 'John' } } });
2121
});
2222

23+
it('should deep merge data with basic path with new format', () => {
24+
const executionResult = { data: { user: { name: 'John' } }, pending: [{ id: '0', path: [] }] };
25+
const incrementalResult = { incremental: [{ id: '0', data: { user: { age: 42 } } }] };
26+
27+
mergeIncrementalResult({ incrementalResult, executionResult });
28+
29+
expect(executionResult.data).toEqual({ user: { age: 42, name: 'John' } });
30+
});
31+
2332
it('should merge data at path', () => {
2433
const executionResult = { data: { user: { name: 'John' } } };
2534
const incrementalResult = { path: ['user'], data: { age: 42 } };
@@ -29,6 +38,18 @@ describe('mergeIncrementalResult', () => {
2938
expect(executionResult).toEqual({ data: { user: { age: 42, name: 'John' } } });
3039
});
3140

41+
it('should merge data at path with new format', () => {
42+
const executionResult = {
43+
data: { user: { name: 'John' } },
44+
pending: [{ id: '0', path: ['user'] }],
45+
};
46+
const incrementalResult = { incremental: [{ id: '0', data: { age: 42 } }] };
47+
48+
mergeIncrementalResult({ incrementalResult, executionResult });
49+
50+
expect(executionResult.data).toEqual({ user: { age: 42, name: 'John' } });
51+
});
52+
3253
it('should push items', () => {
3354
const executionResult = { data: { user: { name: 'John' } } };
3455
const incrementalResult = {
@@ -69,6 +90,27 @@ describe('mergeIncrementalResult', () => {
6990
});
7091
});
7192

93+
it('should push items at path with new format', () => {
94+
const executionResult = {
95+
data: {
96+
user: { name: 'John', comments: ['comment 1', 'comment 2'] },
97+
},
98+
pending: [{ id: '0', path: ['user', 'comments'] }],
99+
};
100+
const incrementalResult = {
101+
incremental: [{ id: '0', items: ['comment 3', 'comment 4'] }],
102+
};
103+
104+
mergeIncrementalResult({ incrementalResult, executionResult });
105+
106+
expect(executionResult.data).toEqual({
107+
user: {
108+
name: 'John',
109+
comments: ['comment 1', 'comment 2', 'comment 3', 'comment 4'],
110+
},
111+
});
112+
});
113+
72114
it('should merge items at path', () => {
73115
const executionResult = {
74116
data: {
@@ -113,6 +155,38 @@ describe('mergeIncrementalResult', () => {
113155
});
114156
});
115157

158+
it('should add errors with new format', () => {
159+
const executionResult = { data: { user: { name: 'John' } }, pending: [{ id: '0', path: [] }] };
160+
const incrementalResult = {
161+
incremental: [
162+
{ id: '0', errors: [new GraphQLError('error 1'), new GraphQLError('error 2')] },
163+
],
164+
};
165+
166+
mergeIncrementalResult({ incrementalResult, executionResult });
167+
168+
expect(executionResult).toEqual({
169+
data: { user: { name: 'John' } },
170+
errors: [new GraphQLError('error 1'), new GraphQLError('error 2')],
171+
pending: [{ id: '0', path: [] }],
172+
});
173+
});
174+
175+
it('should add completion errors with new format', () => {
176+
const executionResult = { data: { user: { name: 'John' } }, pending: [{ id: '0', path: [] }] };
177+
const incrementalResult = {
178+
completed: [{ id: '0', errors: [new GraphQLError('error 1'), new GraphQLError('error 2')] }],
179+
};
180+
181+
mergeIncrementalResult({ incrementalResult, executionResult });
182+
183+
expect(executionResult).toEqual({
184+
data: { user: { name: 'John' } },
185+
errors: [new GraphQLError('error 1'), new GraphQLError('error 2')],
186+
pending: [{ id: '0', path: [] }],
187+
});
188+
});
189+
116190
it('should keep errors', () => {
117191
const executionResult = { errors: [new GraphQLError('error 1')] };
118192
const incrementalResult = { data: { user: { name: 'John' } }, path: [] };
@@ -125,6 +199,24 @@ describe('mergeIncrementalResult', () => {
125199
});
126200
});
127201

202+
it('should keep errors with new format', () => {
203+
const executionResult = {
204+
errors: [new GraphQLError('error 1')],
205+
pending: [{ id: '0', path: [] }],
206+
};
207+
const incrementalResult = {
208+
incremental: [{ id: '0', data: { user: { name: 'John' } }, path: [] }],
209+
};
210+
211+
mergeIncrementalResult({ incrementalResult, executionResult });
212+
213+
expect(executionResult).toEqual({
214+
data: { user: { name: 'John' } },
215+
errors: [new GraphQLError('error 1')],
216+
pending: [{ id: '0', path: [] }],
217+
});
218+
});
219+
128220
it('should merge errors', () => {
129221
const executionResult = { errors: [new GraphQLError('error 1')] };
130222

@@ -143,6 +235,52 @@ describe('mergeIncrementalResult', () => {
143235
});
144236
});
145237

238+
it('should merge errors with new format', () => {
239+
const executionResult = {
240+
errors: [new GraphQLError('error 1')],
241+
pending: [{ id: '0', path: [] }],
242+
};
243+
244+
const incrementalResult = {
245+
incremental: [
246+
{ id: '0', errors: [new GraphQLError('error 2'), new GraphQLError('error 3')] },
247+
],
248+
};
249+
250+
mergeIncrementalResult({ incrementalResult, executionResult });
251+
252+
expect(executionResult).toEqual({
253+
errors: [
254+
new GraphQLError('error 1'),
255+
new GraphQLError('error 2'),
256+
new GraphQLError('error 3'),
257+
],
258+
pending: [{ id: '0', path: [] }],
259+
});
260+
});
261+
262+
it('should merge completion errors with new format', () => {
263+
const executionResult = {
264+
errors: [new GraphQLError('error 1')],
265+
pending: [{ id: '0', path: [] }],
266+
};
267+
268+
const incrementalResult = {
269+
completed: [{ id: '0', errors: [new GraphQLError('error 2'), new GraphQLError('error 3')] }],
270+
};
271+
272+
mergeIncrementalResult({ incrementalResult, executionResult });
273+
274+
expect(executionResult).toEqual({
275+
errors: [
276+
new GraphQLError('error 1'),
277+
new GraphQLError('error 2'),
278+
new GraphQLError('error 3'),
279+
],
280+
pending: [{ id: '0', path: [] }],
281+
});
282+
});
283+
146284
it('should keep extensions', () => {
147285
const exeuctionResult = { data: { user: { name: 'John' } }, extensions: { foo: 'bar' } };
148286
const incrementalResult = { data: { user: { age: 42 } }, path: [] };

0 commit comments

Comments
 (0)