Skip to content

Commit

Permalink
collect typename at context branch point (#3006)
Browse files Browse the repository at this point in the history
When looking up a condition that will be used by a context, make sure
you also collect the __typename field at the branch point
  • Loading branch information
clenfest authored May 16, 2024
1 parent daf36bd commit f04ac12
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions query-planner-js/src/__tests__/buildPlan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8899,6 +8899,7 @@ describe('@fromContext impacts on query planning', () => {
Fetch(service: "Subgraph1") {
{
t {
__typename
prop
u {
__typename
Expand Down Expand Up @@ -9144,6 +9145,7 @@ describe('@fromContext impacts on query planning', () => {
Fetch(service: "Subgraph1") {
{
t {
__typename
prop
u {
__typename
Expand Down Expand Up @@ -9322,6 +9324,7 @@ describe('@fromContext impacts on query planning', () => {
Fetch(service: "Subgraph1") {
{
t {
__typename
prop
u {
__typename
Expand Down Expand Up @@ -9589,6 +9592,7 @@ describe('@fromContext impacts on query planning', () => {
t {
__typename
... on A {
__typename
prop
u {
__typename
Expand All @@ -9597,6 +9601,7 @@ describe('@fromContext impacts on query planning', () => {
}
}
... on B {
__typename
prop
u {
__typename
Expand Down
14 changes: 13 additions & 1 deletion query-planner-js/src/buildPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4355,7 +4355,14 @@ function computeGroupsForTree(
);
updated.group = requireResult.group;
updated.path = requireResult.path;


// add __typename to site where we are retrieving context from
// this is necessary because the context rewrites path will start with a type condition
if (contextToSelection) {
assert(isCompositeType(edge.head.type), () => `Expected a composite type for ${edge.head.type}`);
updated.group.addAtPath(path.inGroup().concat(new Field(edge.head.type.typenameField()!)));
}

if (contextToSelection) {
const newContextToConditionsGroups = new Map<string, FetchGroup[]>([...contextToConditionsGroups]);
for (const context of contextToSelection) {
Expand All @@ -4365,6 +4372,11 @@ function computeGroupsForTree(
}
updateCreatedGroups(createdGroups, ...requireResult.createdGroups);
} else if (conditions) {
// add __typename to site where we are retrieving context from
// this is necessary because the context rewrites path will start with a type condition
assert(isCompositeType(edge.head.type), () => `Expected a composite type for ${edge.head.type}`);
group.addAtPath(path.inGroup().concat(new Field(edge.head.type.typenameField()!)));

const conditionsGroups = computeGroupsForTree({
dependencyGraph,
pathTree: conditions,
Expand Down

0 comments on commit f04ac12

Please sign in to comment.