Skip to content

Commit

Permalink
Federation: Add tests with @defer (#6278)
Browse files Browse the repository at this point in the history
* Federation: Add tests with `@defer`

* Fixes
  • Loading branch information
ardatan committed Jul 1, 2024
1 parent 374961f commit 66c99d9
Show file tree
Hide file tree
Showing 8 changed files with 661 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/eleven-plums-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphql-tools/delegate': patch
'@graphql-tools/stitch': patch
'@graphql-tools/utils': patch
---

Handle `@defer`
5 changes: 5 additions & 0 deletions .changeset/nine-mayflies-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/federation': patch
---

Exclude `@defer` in the subgraph requests
2 changes: 2 additions & 0 deletions packages/delegate/src/prepareGatewayDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ function visitSelectionSet(
if (possibleTypes.length === 0) {
newSelections.add(selection);
}
} else {
newSelections.add(selection);
}
} else if (selection.kind === Kind.FRAGMENT_SPREAD) {
const fragmentName = selection.name.value;
Expand Down
6 changes: 6 additions & 0 deletions packages/federation/src/supergraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,12 @@ export function getStitchingOptionsFromSupergraphSdl(
document: visit(
request.document,
visitWithTypeInfo(typeInfo, {
[Kind.DIRECTIVE](node) {
if (node.name.value === 'defer') {
// @defer is not available for the communication between the gw and subgraph
return null;
}
},
// To avoid resolving unresolvable interface fields
[Kind.FIELD](node) {
if (node.name.value !== '__typename') {
Expand Down
256 changes: 256 additions & 0 deletions packages/federation/test/__snapshots__/defer.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Defer defers the nested fields: defer-nested-fields 1`] = `
[
{
"data": {
"users": [
{
"id": "1",
"name": "Ada Lovelace",
},
{
"id": "2",
"name": "Alan Turing",
},
],
},
"hasNext": true,
},
{
"hasNext": true,
"incremental": [
{
"data": {
"posts": [
{
"author": {
"id": "1",
"posts": [
{
"id": "1",
"title": "Hello, World!",
},
],
},
"id": "1",
"title": "Hello, World!",
},
{
"author": {
"id": "2",
"posts": [
{
"id": "2",
"title": "My Story",
},
],
},
"id": "2",
"title": "My Story",
},
],
},
"path": [],
},
{
"data": {
"name": "Ada Lovelace",
},
"path": [
"posts",
0,
"author",
],
},
{
"data": {
"name": "Alan Turing",
},
"path": [
"posts",
1,
"author",
],
},
],
},
{
"hasNext": true,
"incremental": [
{
"data": {
"posts": [
{
"author": {
"id": "1",
},
"id": "1",
},
],
},
"path": [
"users",
0,
],
},
{
"data": {
"posts": [
{
"author": {
"id": "2",
},
"id": "2",
},
],
},
"path": [
"users",
1,
],
},
],
},
{
"hasNext": false,
"incremental": [
{
"data": {
"title": "Hello, World!",
},
"path": [
"users",
0,
"posts",
0,
],
},
{
"data": {
"title": "My Story",
},
"path": [
"users",
1,
"posts",
0,
],
},
{
"data": {
"name": "Ada Lovelace",
},
"path": [
"users",
0,
"posts",
0,
"author",
],
},
{
"data": {
"name": "Alan Turing",
},
"path": [
"users",
1,
"posts",
0,
"author",
],
},
],
},
]
`;

exports[`Defer defers the root fields: defer-root-fields 1`] = `
[
{
"data": {},
"hasNext": true,
},
{
"hasNext": true,
"incremental": [
{
"data": {
"users": [
{
"id": "1",
"name": "Ada Lovelace",
"posts": [
{
"author": {
"id": "1",
"name": "Ada Lovelace",
},
"id": "1",
"title": "Hello, World!",
},
],
},
{
"id": "2",
"name": "Alan Turing",
"posts": [
{
"author": {
"id": "2",
"name": "Alan Turing",
},
"id": "2",
"title": "My Story",
},
],
},
],
},
"path": [],
},
],
},
{
"hasNext": false,
"incremental": [
{
"data": {
"posts": [
{
"author": {
"id": "1",
"name": "Ada Lovelace",
"posts": [
{
"id": "1",
"title": "Hello, World!",
},
],
},
"id": "1",
"title": "Hello, World!",
},
{
"author": {
"id": "2",
"name": "Alan Turing",
"posts": [
{
"id": "2",
"title": "My Story",
},
],
},
"id": "2",
"title": "My Story",
},
],
},
"path": [],
},
],
},
]
`;
Loading

0 comments on commit 66c99d9

Please sign in to comment.