Skip to content

Commit

Permalink
add additional tests for mergeIncrementalResults
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Jun 6, 2024
1 parent cd2a4fa commit 6d3be83
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 19 deletions.
27 changes: 8 additions & 19 deletions packages/utils/src/mergeIncrementalResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,15 @@ export function mergeIncrementalResult({
...(incrementalResult.path ?? []),
];

if (incrementalResult.pending) {
let paths = pathsMap.get(executionResult);
if (paths === undefined) {
paths = new Map();
pathsMap.set(executionResult, paths);
}

for (const { id, path } of incrementalResult.pending) {
paths.set(id, ['data', ...path]);
}
}

if (incrementalResult.pending) {
const paths = pathsMap.get(executionResult);
for (const result of [executionResult, incrementalResult]) {
if (result.pending) {
let paths = pathsMap.get(executionResult);
if (paths === undefined) {
paths = new Map();
pathsMap.set(executionResult, paths);
}

for (const { id, path } of incrementalResult.pending) {
if (id !== undefined) {
if (paths === undefined) {
throw new Error('Invalid incremental delivery format.');
}
for (const { id, path } of result.pending) {
paths.set(id, ['data', ...path]);
}
}
Expand Down
138 changes: 138 additions & 0 deletions packages/utils/tests/mergeIncrementalResult.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ describe('mergeIncrementalResult', () => {
expect(executionResult).toEqual({ data: { user: { age: 42, name: 'John' } } });
});

it('should deep merge data with basic path with new format', () => {
const executionResult = { data: { user: { name: 'John' } }, pending: [{ id: '0', path: [] }] };
const incrementalResult = { incremental: [{ id: '0', data: { user: { age: 42 } } }] };

mergeIncrementalResult({ incrementalResult, executionResult });

expect(executionResult.data).toEqual({ user: { age: 42, name: 'John' } });
});

it('should merge data at path', () => {
const executionResult = { data: { user: { name: 'John' } } };
const incrementalResult = { path: ['user'], data: { age: 42 } };
Expand All @@ -29,6 +38,18 @@ describe('mergeIncrementalResult', () => {
expect(executionResult).toEqual({ data: { user: { age: 42, name: 'John' } } });
});

it('should merge data at path with new format', () => {
const executionResult = {
data: { user: { name: 'John' } },
pending: [{ id: '0', path: ['user'] }],
};
const incrementalResult = { incremental: [{ id: '0', data: { age: 42 } }] };

mergeIncrementalResult({ incrementalResult, executionResult });

expect(executionResult.data).toEqual({ user: { age: 42, name: 'John' } });
});

it('should push items', () => {
const executionResult = { data: { user: { name: 'John' } } };
const incrementalResult = {
Expand Down Expand Up @@ -69,6 +90,27 @@ describe('mergeIncrementalResult', () => {
});
});

it('should push items at path with new format', () => {
const executionResult = {
data: {
user: { name: 'John', comments: ['comment 1', 'comment 2'] },
},
pending: [{ id: '0', path: ['user', 'comments'] }],
};
const incrementalResult = {
incremental: [{ id: '0', items: ['comment 3', 'comment 4'] }],
};

mergeIncrementalResult({ incrementalResult, executionResult });

expect(executionResult.data).toEqual({
user: {
name: 'John',
comments: ['comment 1', 'comment 2', 'comment 3', 'comment 4'],
},
});
});

it('should merge items at path', () => {
const executionResult = {
data: {
Expand Down Expand Up @@ -113,6 +155,38 @@ describe('mergeIncrementalResult', () => {
});
});

it('should add errors with new format', () => {
const executionResult = { data: { user: { name: 'John' } }, pending: [{ id: '0', path: [] }] };
const incrementalResult = {
incremental: [
{ id: '0', errors: [new GraphQLError('error 1'), new GraphQLError('error 2')] },
],
};

mergeIncrementalResult({ incrementalResult, executionResult });

expect(executionResult).toEqual({
data: { user: { name: 'John' } },
errors: [new GraphQLError('error 1'), new GraphQLError('error 2')],
pending: [{ id: '0', path: [] }],
});
});

it('should add completion errors with new format', () => {
const executionResult = { data: { user: { name: 'John' } }, pending: [{ id: '0', path: [] }] };
const incrementalResult = {
completed: [{ id: '0', errors: [new GraphQLError('error 1'), new GraphQLError('error 2')] }],
};

mergeIncrementalResult({ incrementalResult, executionResult });

expect(executionResult).toEqual({
data: { user: { name: 'John' } },
errors: [new GraphQLError('error 1'), new GraphQLError('error 2')],
pending: [{ id: '0', path: [] }],
});
});

it('should keep errors', () => {
const executionResult = { errors: [new GraphQLError('error 1')] };
const incrementalResult = { data: { user: { name: 'John' } }, path: [] };
Expand All @@ -125,6 +199,24 @@ describe('mergeIncrementalResult', () => {
});
});

it('should keep errors with new format', () => {
const executionResult = {
errors: [new GraphQLError('error 1')],
pending: [{ id: '0', path: [] }],
};
const incrementalResult = {
incremental: [{ id: '0', data: { user: { name: 'John' } }, path: [] }],
};

mergeIncrementalResult({ incrementalResult, executionResult });

expect(executionResult).toEqual({
data: { user: { name: 'John' } },
errors: [new GraphQLError('error 1')],
pending: [{ id: '0', path: [] }],
});
});

it('should merge errors', () => {
const executionResult = { errors: [new GraphQLError('error 1')] };

Expand All @@ -143,6 +235,52 @@ describe('mergeIncrementalResult', () => {
});
});

it('should merge errors with new format', () => {
const executionResult = {
errors: [new GraphQLError('error 1')],
pending: [{ id: '0', path: [] }],
};

const incrementalResult = {
incremental: [
{ id: '0', errors: [new GraphQLError('error 2'), new GraphQLError('error 3')] },
],
};

mergeIncrementalResult({ incrementalResult, executionResult });

expect(executionResult).toEqual({
errors: [
new GraphQLError('error 1'),
new GraphQLError('error 2'),
new GraphQLError('error 3'),
],
pending: [{ id: '0', path: [] }],
});
});

it('should merge completion errors with new format', () => {
const executionResult = {
errors: [new GraphQLError('error 1')],
pending: [{ id: '0', path: [] }],
};

const incrementalResult = {
completed: [{ id: '0', errors: [new GraphQLError('error 2'), new GraphQLError('error 3')] }],
};

mergeIncrementalResult({ incrementalResult, executionResult });

expect(executionResult).toEqual({
errors: [
new GraphQLError('error 1'),
new GraphQLError('error 2'),
new GraphQLError('error 3'),
],
pending: [{ id: '0', path: [] }],
});
});

it('should keep extensions', () => {
const exeuctionResult = { data: { user: { name: 'John' } }, extensions: { foo: 'bar' } };
const incrementalResult = { data: { user: { age: 42 } }, path: [] };
Expand Down

0 comments on commit 6d3be83

Please sign in to comment.