Skip to content

Commit

Permalink
keep inputRewrites when cloning a FetchGroup (#2898)
Browse files Browse the repository at this point in the history
if we do not keep them, they may disappear from the final fetch node

Co-authored-by: dariuszkuc <9501705+dariuszkuc@users.noreply.github.com>
Co-authored-by: Trevor Scheer <trevor.scheer@gmail.com>
  • Loading branch information
3 people authored Jan 5, 2024
1 parent f69a069 commit 038cf0d
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/nasty-pillows-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@apollo/query-planner": patch
---

Fix handling of `@interfaceObject` when multiple child query paths are available.

When making copies of `FetchDependencyGraph`s, we were making incomplete copies that were missing `__typename` input rewrite information required for correctly handling `@interfaceObject` resolution.
148 changes: 148 additions & 0 deletions query-planner-js/src/__tests__/buildPlan.interfaceObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,3 +696,151 @@ it('handles @interfaceObject in nested entity', () => {
}
`);
});

it('handles @interfaceObject input rewrites when cloning dependency graph', () => {
const subgraph1 = {
name: 'S1',
typeDefs: gql`
type Query {
i: I!
}
interface I @key(fields: "i1") {
i1: String!
i2: T
}
type T @key(fields: "t1", resolvable: false) {
t1: String!
}
type U implements I @key(fields: "i1") {
id: ID!
i1: String!
i2: T @shareable
}
`,
};

const subgraph2 = {
name: 'S2',
typeDefs: gql`
type I @interfaceObject @key(fields: "i1") {
i1: String!
i2: T @shareable
i3: Int
}
type T @key(fields: "t1", resolvable: false) {
t1: String!
}
`,
};

const subgraph3 = {
name: 'S3',
typeDefs: gql`
type T @key(fields: "t1") {
t1: String!
t2: String! @shareable
t3: Int
}
`,
};

const subgraph4 = {
name: 'S4',
typeDefs: gql`
type T @key(fields: "t1") {
t1: String!
t2: String! @shareable
t4: Int
}
`,
};

const [api, queryPlanner] = composeAndCreatePlanner(
subgraph1,
subgraph2,
subgraph3,
subgraph4,
);

const operation = operationFromDocument(
api,
gql`
query {
i {
__typename
i2 {
__typename
t2
}
i3
}
}
`,
);

const plan = queryPlanner.buildQueryPlan(operation);
expect(plan).toMatchInlineSnapshot(`
QueryPlan {
Sequence {
Fetch(service: "S1") {
{
i {
__typename
i1
i2 {
__typename
t1
}
}
}
},
Parallel {
Flatten(path: "i") {
Fetch(service: "S2") {
{
... on I {
__typename
i1
}
} =>
{
... on I {
i3
}
}
},
},
Flatten(path: "i.i2") {
Fetch(service: "S3") {
{
... on T {
__typename
t1
}
} =>
{
... on T {
__typename
t2
}
}
},
},
},
},
}
`);

assert(isPlanNode(plan.node), 'buildQueryPlan should return QueryPlan');
const rewrites = findFetchNodes('S2', plan.node)[0].inputRewrites;
expect(rewrites).toBeDefined();
expect(rewrites?.length).toBe(1);
const rewrite = rewrites![0];
assert(rewrite.kind === 'ValueSetter', JSON.stringify(rewrite));
expect(rewrite.path).toEqual(['... on I', '__typename']);
expect(rewrite.setValueTo).toBe('I');
});
5 changes: 2 additions & 3 deletions query-planner-js/src/buildPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -921,9 +921,6 @@ class FetchGroup {
// Set in some code-path to indicate that the selection of the group not be optimized away even if it "looks" useless.
mustPreserveSelection: boolean = false;

private readonly inputRewrites: FetchDataRewrite[] = [];


private constructor(
readonly dependencyGraph: FetchDependencyGraph,
public index: number,
Expand All @@ -941,6 +938,7 @@ class FetchGroup {
private cachedCost?: number,
// Cache used to save unecessary recomputation of the `isUseless` method.
private isKnownUseful: boolean = false,
private readonly inputRewrites: FetchDataRewrite[] = [],
) {
if (this._inputs) {
this._inputs.onUpdateCallback = () => {
Expand Down Expand Up @@ -1008,6 +1006,7 @@ class FetchGroup {
this.subgraphAndMergeAtKey,
this.cachedCost,
this.isKnownUseful,
[...this.inputRewrites],
);
}

Expand Down

0 comments on commit 038cf0d

Please sign in to comment.