Skip to content

Commit

Permalink
try aggressive merging
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Jan 10, 2023
1 parent 594c969 commit 4080c48
Show file tree
Hide file tree
Showing 4 changed files with 615 additions and 238 deletions.
216 changes: 203 additions & 13 deletions src/execution/__tests__/defer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ const friends = [
{ name: 'C-3PO', id: 4 },
];

const deeperObject = new GraphQLObjectType({
fields: {
foo: { type: GraphQLString, resolve: () => 'foo' },
bar: { type: GraphQLString, resolve: () => 'bar' },
baz: { type: GraphQLString, resolve: () => 'baz' },
bak: { type: GraphQLString, resolve: () => 'bak' },
},
name: 'DeeperObject',
});

const nestedObject = new GraphQLObjectType({
fields: {
deeperObject: { type: deeperObject, resolve: () => ({}) },
},
name: 'NestedObject',
});

const heroType = new GraphQLObjectType({
fields: {
id: { type: GraphQLID },
Expand Down Expand Up @@ -75,6 +92,7 @@ const heroType = new GraphQLObjectType({
yield await Promise.resolve(friends[0]);
},
},
nestedObject: { type: nestedObject, resolve: () => ({}) },
},
name: 'Hero',
});
Expand Down Expand Up @@ -141,7 +159,6 @@ describe('Execute: defer directive', () => {
incremental: [
{
data: {
id: '1',
name: 'Luke',
},
path: ['hero'],
Expand Down Expand Up @@ -307,15 +324,10 @@ describe('Execute: defer directive', () => {
},
{
incremental: [
{
data: {
friends: [{ name: 'Han' }, { name: 'Leia' }, { name: 'C-3PO' }],
},
path: ['hero'],
},
{
data: {
name: 'Luke',
friends: [{ name: 'Han' }, { name: 'Leia' }, { name: 'C-3PO' }],
},
path: ['hero'],
},
Expand Down Expand Up @@ -351,9 +363,7 @@ describe('Execute: defer directive', () => {
{
incremental: [
{
data: {
name: 'Luke',
},
data: {},
path: ['hero'],
},
],
Expand Down Expand Up @@ -388,9 +398,7 @@ describe('Execute: defer directive', () => {
{
incremental: [
{
data: {
name: 'Luke',
},
data: {},
path: ['hero'],
},
],
Expand Down Expand Up @@ -423,6 +431,188 @@ describe('Execute: defer directive', () => {
},
]);
});

it('Can deduplicate multiple defers on the same object', async () => {
const document = parse(`
query {
hero {
friends {
... @defer {
...FriendFrag
... @defer {
...FriendFrag
... @defer {
...FriendFrag
... @defer {
...FriendFrag
}
}
}
}
}
}
}
fragment FriendFrag on Friend {
id
name
}
`);
const result = await complete(document);

expectJSON(result).toDeepEqual([
{
data: { hero: { friends: [{}, {}, {}] } },
hasNext: true,
},
{
incremental: [
{ data: { id: '2', name: 'Han' }, path: ['hero', 'friends', 0] },
{ data: { id: '3', name: 'Leia' }, path: ['hero', 'friends', 1] },
{ data: { id: '4', name: 'C-3PO' }, path: ['hero', 'friends', 2] },
],
hasNext: false,
},
]);
});

it('Can deduplicate fields with deferred fragments at multiple levels', async () => {
const document = parse(`
query {
hero {
nestedObject {
deeperObject {
foo
}
}
... @defer {
nestedObject {
deeperObject {
foo
bar
}
... @defer {
deeperObject {
foo
bar
baz
... @defer {
foo
bar
baz
bak
}
}
}
}
}
}
}
`);
const result = await complete(document);
expectJSON(result).toDeepEqual([
{
data: {
hero: {
nestedObject: {
deeperObject: {
foo: 'foo',
},
},
},
},
hasNext: true,
},
{
incremental: [
{
data: {
bak: 'bak',
},
path: ['hero', 'nestedObject', 'deeperObject'],
},
{
data: {
deeperObject: {
baz: 'baz',
},
},
path: ['hero', 'nestedObject'],
},
{
data: {
nestedObject: {
deeperObject: {
bar: 'bar',
},
},
},
path: ['hero'],
},
],
hasNext: false,
},
]);
});

it('Can deduplicate fields with deferred fragments at the same level in different branches', async () => {
const document = parse(`
query {
hero {
nestedObject {
deeperObject {
... @defer {
foo
}
}
}
... @defer {
nestedObject {
deeperObject {
... @defer {
bar
}
}
}
}
}
}
`);
const result = await complete(document);
expectJSON(result).toDeepEqual([
{
data: {
hero: {
nestedObject: {
deeperObject: {},
},
},
},
hasNext: true,
},
{
incremental: [
{
data: {
foo: 'foo',
bar: 'bar',
},
path: ['hero', 'nestedObject', 'deeperObject'],
},
{
data: {
nestedObject: {
deeperObject: {},
},
},
path: ['hero'],
},
],
hasNext: false,
},
]);
});

it('Handles errors thrown in deferred fragments', async () => {
const document = parse(`
query HeroNameQuery {
Expand Down
Loading

0 comments on commit 4080c48

Please sign in to comment.