Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with path in query plan's deferred nodes #2281

Merged
merged 2 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internals-js/src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,12 @@ export type OperationElement = Field<any> | FragmentElement;

export type OperationPath = OperationElement[];

export function operationPathToStringPath(path: OperationPath): string[] {
return path
.filter((p) => !(p.kind === 'FragmentElement' && !p.typeCondition))
.map((p) => p.kind === 'Field' ? p.responseName() : `... on ${p.typeCondition?.coordinate}`);
}

export function sameOperationPaths(p1: OperationPath, p2: OperationPath): boolean {
if (p1 === p2) {
return true;
Expand Down
5 changes: 3 additions & 2 deletions query-planner-js/src/QueryPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ export interface DeferredNode {
}[],
// The optional defer label.
label?: string,
// Path to the @defer this correspond to. The `subselection` starts at that `path`.
path: ResponsePath,
// Path, in the query, to the @defer this corresponds to. The `subselection` starts at that `queryPath`.
// This look like: `[ 'products', '... on Book', 'reviews' ]`
queryPath: string[],
// The part of the original query that "selects" the data to send in that deferred response (once the plan in `node` completes). Will be set _unless_ `node` is a `DeferNode` itself.
subselection?: string,
// The plan to get all the data for that deferred part. Usually set, but can be undefined for a `@defer` where everything has been fetched in the "primary block", that is when
Expand Down
112 changes: 104 additions & 8 deletions query-planner-js/src/__tests__/buildPlan.defer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ describe('non-router-based-defer', () => {
},
}
}, [
Deferred(depends: [], path: "t.v") {
Deferred(depends: [], path: "t/v") {
{
b
}:
Expand Down Expand Up @@ -444,7 +444,7 @@ describe('non-router-based-defer', () => {
},
}
}, [
Deferred(depends: [0], path: "t.v") {
Deferred(depends: [0], path: "t/v") {
{
u {
x
Expand Down Expand Up @@ -838,7 +838,7 @@ test('multiple (non-nested) @defer + label handling', () => {
},
}
},
Deferred(depends: [1], path: "t.v3", label: "defer_in_v3") {
Deferred(depends: [1], path: "t/v3", label: "defer_in_v3") {
{
y
}:
Expand Down Expand Up @@ -990,7 +990,7 @@ describe('nested @defer', () => {
},
}
}, [
Deferred(depends: [1], path: "me.messages.@.author") {
Deferred(depends: [1], path: "me/messages/author") {
{
messages {
body
Expand Down Expand Up @@ -1114,7 +1114,7 @@ describe('nested @defer', () => {
},
}
}, [
Deferred(depends: [], path: "me.messages.@") {
Deferred(depends: [], path: "me/messages") {
{
body {
lines
Expand Down Expand Up @@ -2180,7 +2180,7 @@ test('@defer on query root type', () => {
},
}
}, [
Deferred(depends: [0], path: "op2.next") {
Deferred(depends: [0], path: "op2/next") {
{
op1
op4
Expand Down Expand Up @@ -2721,7 +2721,7 @@ describe('defer with conditions', () => {
},
}
}, [
Deferred(depends: [1], path: "t.u") {
Deferred(depends: [1], path: "t/u") {
{
b
}:
Expand Down Expand Up @@ -2812,7 +2812,7 @@ describe('defer with conditions', () => {
},
}
},
Deferred(depends: [0], path: "t.u") {
Deferred(depends: [0], path: "t/u") {
{
b
}:
Expand Down Expand Up @@ -3552,3 +3552,99 @@ test('@defer only the key of an entity', () => {
}
`);
});

test('the path in @defer includes traversed fragments', () => {
const subgraph1 = {
name: 'Subgraph1',
typeDefs: gql`
type Query {
i : I
}

interface I {
x: Int
}

type A implements I {
x: Int
t: T
}

type T @key(fields: "id") {
id: ID!
v1: String
v2: String
}
`
}

const [api, queryPlanner] = composeAndCreatePlannerWithDefer(subgraph1);
const operation = operationFromDocument(api, gql`
{
i {
... on A {
t {
v1
... @defer {
v2
}
}
}
}
}
`);

const plan = queryPlanner.buildQueryPlan(operation);
expect(plan).toMatchInlineSnapshot(`
QueryPlan {
Defer {
Primary {
{
i {
... on A {
t {
v1
}
}
}
}:
Fetch(service: "Subgraph1", id: 0) {
{
i {
__typename
... on A {
t {
__typename
v1
id
}
}
}
}
}
}, [
Deferred(depends: [0], path: "i/... on A/t") {
{
v2
}:
Flatten(path: "i.t") {
Fetch(service: "Subgraph1") {
{
... on T {
__typename
id
}
} =>
{
... on T {
v2
}
}
},
}
},
]
},
}
`);
});
Loading