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

Use alias in QP when querying conflicting fields #2294

Merged
merged 3 commits into from
Dec 14, 2022

Conversation

pcmanus
Copy link
Contributor

@pcmanus pcmanus commented Dec 8, 2022

In a few situations, the query planner was generating queries where the same response name was queried at the same "level" with incompatible types, resulting in invalid queries (the queries were failing the FieldsInSetCanMerge)validation for the GraphQL spec).

This commit detects this case, and when it would happen, aliases oneof the occurence in the fetch to make the query valid. Once receivingthe fetch result, the aliased value is rewritten to it's originalresponse name.

Fixes #1257

@netlify
Copy link

netlify bot commented Dec 8, 2022

👷 Deploy request for apollo-federation-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit ff621c4

@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 8, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@pcmanus
Copy link
Contributor Author

pcmanus commented Dec 8, 2022

Note that the patch is currently on top of the patch for #2277. That's because the "post-fetch" rewriting done here piggy-back on the query plan changes made by the @interfaceObject patch, which also does some fetch rewriting (pre-fetch in that case, but still fairly similar). But given that this is a slightly involved fix, I think it's worth waiting on 2.3 for this anyway, not in a 2.2 patch release.

Note that problem/solution still very much follow what I describe in this comment. The one question I asked there was if we wanted to only add aliases when strictly necessary, and I answer this positively here, not only for the reason I mentioned in the comment (this avoid modifying most existing plan) but also because it is likely more efficient since adding aliases more often would 1) result in slightly larger fetch queries in general and 2) would imply post-fetch rewritings much more often, and while it's super expensive, it has a cost nonetheless.

@pcmanus pcmanus mentioned this pull request Dec 8, 2022
@pcmanus pcmanus self-assigned this Dec 9, 2022
@@ -3516,4 +3516,1113 @@ describe('executeQueryPlan', () => {
`);
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You talk in the comments about potentially having a naming collision when it comes to aliasing and potentially needing to regenerate the name? Is there a test for that case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I had not added a test for that, so pushed one (which obviously uncovered a small issue in some cases).

@pcmanus pcmanus added this to the 2.3.0 milestone Dec 13, 2022
@pcmanus pcmanus changed the base branch from main to next December 14, 2022 10:40
Sylvain Lebresne added 2 commits December 14, 2022 14:33
In a few situations, the query planner was generating queries where
the same response name was queried at the same "level" with incompatible
types, resulting in invalid queries (the queries were failing the
[`FieldsInSetCanMerge`](https://spec.graphql.org/draft/#FieldsInSetCanMerge()))
validation for the GraphQL sepc).

This commit detects this case, and when it would happen, aliases one
of the occurence in the fetch to make the query valid. Once receiving
the fetch result, the aliased value is rewritten to it's original
response name.

Fixes apollographql#1257
@clenfest clenfest merged commit f6c39b5 into apollographql:next Dec 14, 2022
jeffjakub added a commit that referenced this pull request Dec 16, 2022
* Implements @interfaceObject and @key on interfaces (#2279)

Add support for the newly added `@interfaceObject` directive and for
`@key` on interfaces, in order to allow using interfaces as abstraction
across subgraphs. See #2277 for details.

Fixes #2277.

* Preserves source of union members and enum values in supergraph (#2288)

While composition allows unions and enums to differ between subgraphs (at
least under certain conditions), the supergraph currently was not
preserving which subgraph defined which union member/enum value, and in
particular the query planner made the (sometimes incorrect) assumption
that unions and enums were defined the same in all the subgraphs in
which they are defined.

As shown in #2256, this was leading to genuine issues in the case of
unions, where the query planner was generating invalid subgraph queries.
I am not sure this created similar issues for enum values due to a
combination of how the merging rule for enum work and the fact that
values of enum are not types, but it is nonetheless a bad idea to
run the query planner with incorrect information on the subgraph it
generates queries for, and this can get in the way of other toolings
that wants to use the supergraph (for instance, this gets in the
way of #2250).

To fix this, this patch ensures the supergraph preserve the information
regarding which subgraph defines which union member and enum values in
the supergraph by adding 2 new dedicated "join spec" directives.

Fixes #2256.

* Use alias in QP when querying conflicting fields (#2294)

* Use alias in QP when querying conflicting fields

In a few situations, the query planner was generating queries where
the same response name was queried at the same "level" with incompatible
types, resulting in invalid queries (the queries were failing the
[`FieldsInSetCanMerge`](https://spec.graphql.org/draft/#FieldsInSetCanMerge()))
validation for the GraphQL sepc).

This commit detects this case, and when it would happen, aliases one
of the occurence in the fetch to make the query valid. Once receiving
the fetch result, the aliased value is rewritten to it's original
response name.

Fixes #1257

* Review feedback: add test for alias conflicts and fix related code

* Regen error doc

* Release

 - @apollo/composition@2.3.0-alpha.0
 - apollo-federation-integration-testsuite@2.3.0-alpha.0
 - @apollo/gateway@2.3.0-alpha.0
 - @apollo/federation-internals@2.3.0-alpha.0
 - @apollo/query-graphs@2.3.0-alpha.0
 - @apollo/query-planner@2.3.0-alpha.0
 - @apollo/subgraph@2.3.0-alpha.0

Co-authored-by: Sylvain Lebresne <sylvain@apollographql.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Interface fragment expansion causes conflicting field types
2 participants