-
-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(graphcache): Fix defer field state becoming sticky and affecting …
…future fields (#3167)
- Loading branch information
Showing
5 changed files
with
312 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@urql/exchange-graphcache': patch | ||
--- | ||
|
||
Fix regression which caused `@defer` directives from becoming “sticky” and causing every subsequent cache read to be treated as if the field was deferred. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { TypedDocumentNode, gql } from '@urql/core'; | ||
import { FieldNode } from '@0no-co/graphql.web'; | ||
|
||
import { makeSelectionIterator, deferRef } from './shared'; | ||
import { SelectionSet } from '../ast'; | ||
|
||
const selectionOfDocument = (doc: TypedDocumentNode): SelectionSet => { | ||
for (const definition of doc.definitions) | ||
if (definition.kind === 'OperationDefinition') | ||
return definition.selectionSet.selections; | ||
return []; | ||
}; | ||
|
||
const ctx = {} as any; | ||
|
||
describe('makeSelectionIterator', () => { | ||
it('emits all fields', () => { | ||
const selection = selectionOfDocument( | ||
gql` | ||
{ | ||
a | ||
b | ||
c | ||
} | ||
` | ||
); | ||
const iterate = makeSelectionIterator( | ||
'Query', | ||
'Query', | ||
false, | ||
selection, | ||
ctx | ||
); | ||
const result: FieldNode[] = []; | ||
|
||
let node: FieldNode | void; | ||
while ((node = iterate())) result.push(node); | ||
|
||
expect(result).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"alias": undefined, | ||
"arguments": [], | ||
"directives": [], | ||
"kind": "Field", | ||
"name": { | ||
"kind": "Name", | ||
"value": "a", | ||
}, | ||
"selectionSet": undefined, | ||
}, | ||
{ | ||
"alias": undefined, | ||
"arguments": [], | ||
"directives": [], | ||
"kind": "Field", | ||
"name": { | ||
"kind": "Name", | ||
"value": "b", | ||
}, | ||
"selectionSet": undefined, | ||
}, | ||
{ | ||
"alias": undefined, | ||
"arguments": [], | ||
"directives": [], | ||
"kind": "Field", | ||
"name": { | ||
"kind": "Name", | ||
"value": "c", | ||
}, | ||
"selectionSet": undefined, | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
it('skips fields that are skipped or not included', () => { | ||
const selection = selectionOfDocument(gql` | ||
{ | ||
a @skip(if: true) | ||
b @include(if: false) | ||
} | ||
`); | ||
|
||
const iterate = makeSelectionIterator( | ||
'Query', | ||
'Query', | ||
false, | ||
selection, | ||
ctx | ||
); | ||
const result: FieldNode[] = []; | ||
|
||
let node: FieldNode | void; | ||
while ((node = iterate())) result.push(node); | ||
|
||
expect(result).toMatchInlineSnapshot('[]'); | ||
}); | ||
|
||
it('processes fragments', () => { | ||
const selection = selectionOfDocument(gql` | ||
{ | ||
a | ||
... { | ||
b | ||
} | ||
... { | ||
... { | ||
c | ||
} | ||
} | ||
} | ||
`); | ||
|
||
const iterate = makeSelectionIterator( | ||
'Query', | ||
'Query', | ||
false, | ||
selection, | ||
ctx | ||
); | ||
const result: FieldNode[] = []; | ||
|
||
let node: FieldNode | void; | ||
while ((node = iterate())) result.push(node); | ||
|
||
expect(result).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"alias": undefined, | ||
"arguments": [], | ||
"directives": [], | ||
"kind": "Field", | ||
"name": { | ||
"kind": "Name", | ||
"value": "a", | ||
}, | ||
"selectionSet": undefined, | ||
}, | ||
{ | ||
"alias": undefined, | ||
"arguments": [], | ||
"directives": [], | ||
"kind": "Field", | ||
"name": { | ||
"kind": "Name", | ||
"value": "b", | ||
}, | ||
"selectionSet": undefined, | ||
}, | ||
{ | ||
"alias": undefined, | ||
"arguments": [], | ||
"directives": [], | ||
"kind": "Field", | ||
"name": { | ||
"kind": "Name", | ||
"value": "c", | ||
}, | ||
"selectionSet": undefined, | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
it('updates deferred state as needed', () => { | ||
const selection = selectionOfDocument(gql` | ||
{ | ||
a | ||
... @defer { | ||
b | ||
} | ||
... { | ||
... @defer { | ||
c | ||
} | ||
} | ||
... { | ||
... { | ||
d | ||
} | ||
} | ||
... @defer { | ||
... { | ||
e | ||
} | ||
} | ||
... { | ||
... { | ||
f | ||
} | ||
} | ||
... { | ||
g | ||
} | ||
h | ||
} | ||
`); | ||
|
||
const iterate = makeSelectionIterator( | ||
'Query', | ||
'Query', | ||
false, | ||
selection, | ||
ctx | ||
); | ||
|
||
const deferred: boolean[] = []; | ||
while (iterate()) deferred.push(deferRef); | ||
expect(deferred).toEqual([ | ||
false, // a | ||
true, // b | ||
true, // c | ||
false, // d | ||
true, // e | ||
false, // f | ||
false, // g | ||
false, // h | ||
]); | ||
}); | ||
|
||
it('applies the parent’s defer state if needed', () => { | ||
const selection = selectionOfDocument(gql` | ||
{ | ||
a | ||
... @defer { | ||
b | ||
} | ||
... { | ||
c | ||
} | ||
} | ||
`); | ||
|
||
const iterate = makeSelectionIterator( | ||
'Query', | ||
'Query', | ||
true, | ||
selection, | ||
ctx | ||
); | ||
|
||
const deferred: boolean[] = []; | ||
while (iterate()) deferred.push(deferRef); | ||
expect(deferred).toEqual([true, true, true]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.