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 a query planning bug where invalid subgraph queries are generated with reuseQueryFragments set true #2963

Merged
merged 5 commits into from
Apr 3, 2024

Conversation

duckki
Copy link
Contributor

@duckki duckki commented Mar 20, 2024

This PR fixes #2952.

Summary of changes

  • updated computeExpandedSelectionSetAtType function not to trim fragment's validators if the fragment's type condition is not an object type.
  • This change is necessary because FieldsInSetCanMerge rule is more restrictive in that case.
  • The untrimmed validator should avoid generating invalid queries, but it may be less efficient.

Test plan

  • The operations.test.ts confirms the changes of named fragments' validators.
  • Added a new test (based on the github issue's reproducer) confirming that subgraph queries are no longer invalid.

@duckki duckki requested a review from a team as a code owner March 20, 2024 04:10
Copy link

netlify bot commented Mar 20, 2024

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

Visit the deploys page to approve it

Name Link
🔨 Latest commit 1a5211e

Copy link

changeset-bot bot commented Mar 20, 2024

🦋 Changeset detected

Latest commit: 1a5211e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 7 packages
Name Type
@apollo/query-planner Patch
@apollo/composition Patch
@apollo/federation-internals Patch
@apollo/gateway Patch
@apollo/query-graphs Patch
@apollo/subgraph Patch
apollo-federation-integration-testsuite Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

codesandbox-ci bot commented Mar 20, 2024

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.

- updated computeExpandedSelectionSetAtType function not to trim fragment's validators if the fragment's type condition is not an object type.
- This change is necessary because `FieldsInSetCanMerge` rule is more restrictive in that case.
- The untrimmed validator should avoid generating invalid queries, but it may be less efficient.

apollographql#2952
@duckki
Copy link
Contributor Author

duckki commented Mar 26, 2024

@dariuszkuc I just rebased this PR on top of current origin/main. Please review. Thanks!

@goto-bus-stop goto-bus-stop self-requested a review March 28, 2024 15:07
@duckki
Copy link
Contributor Author

duckki commented Apr 2, 2024

@goto-bus-stop Please also review this one :)

  • The semgrep check was failing due to the lack of github access, which I'm not sure how to fix.
  • I believe Note.js v16 failure is an intermittent issue.

Copy link
Member

@goto-bus-stop goto-bus-stop left a comment

Choose a reason for hiding this comment

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

It took me a little bit to grasp the issue here 😇

Test and implementation looks good 👍🏻

@@ -1224,6 +1224,14 @@ export class NamedFragmentDefinition extends DirectiveTargetElement<NamedFragmen
const expandedSelectionSet = this.expandedSelectionSet();
const selectionSet = expandedSelectionSet.normalize({ parentType: type });

if( ! isObjectType(this.typeCondition) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if( ! isObjectType(this.typeCondition) ) {
if (!isObjectType(this.typeCondition)) {

just style (not familiar with this repo, i'm not sure why it isn't enforced 🤔 )

* @param subgraphs
*/
function validateSubFetches(
plan: any,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
plan: any,
plan: PlanNode | SubscriptionNode | undefined,

if these types are not available in this file they should be imported from ../QueryPlan.ts

Copy link
Member

@goto-bus-stop goto-bus-stop left a comment

Choose a reason for hiding this comment

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

Pre-approval to unblock merging after the plan: any is addressed 😄

@duckki
Copy link
Contributor Author

duckki commented Apr 3, 2024

@goto-bus-stop Thanks for the review. I pushed a fix.

@duckki duckki merged commit ec04c50 into apollographql:main Apr 3, 2024
12 of 13 checks passed
@duckki duckki deleted the duckki/fed-124 branch April 3, 2024 18:31
@github-actions github-actions bot mentioned this pull request Apr 3, 2024
o0Ignition0o pushed a commit that referenced this pull request Apr 16, 2024
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @apollo/[email protected]

### Patch Changes

- Fix a query planning bug where invalid subgraph queries are generated
with `reuseQueryFragments` set true.
([#2952](#2952))
([#2963](#2963))

- Updated dependencies
\[[`ec04c50b4fb832bfd281ecf9c0c2dd7656431b96`](ec04c50),
[`a494631918156f0431ceace74281c076cf1d5d51`](a494631)]:
    -   @apollo/[email protected]
    -   @apollo/[email protected]

## @apollo/[email protected]

### Patch Changes

- Updated dependencies
\[[`ec04c50b4fb832bfd281ecf9c0c2dd7656431b96`](ec04c50),
[`3e2c845c74407a136b9e0066e44c1ad1467d3013`](3e2c845),
[`a494631918156f0431ceace74281c076cf1d5d51`](a494631)]:
    -   @apollo/[email protected]
    -   @apollo/[email protected]
    -   @apollo/[email protected]

## @apollo/[email protected]

### Patch Changes

- Fix a query planning bug where invalid subgraph queries are generated
with `reuseQueryFragments` set true.
([#2952](#2952))
([#2963](#2963))

- Fixed query planner to pass the directives from original query to
subgraph operations (#2961)
([#2967](#2967))

## @apollo/[email protected]

### Patch Changes

- Updated dependencies
\[[`ec04c50b4fb832bfd281ecf9c0c2dd7656431b96`](ec04c50),
[`a494631918156f0431ceace74281c076cf1d5d51`](a494631)]:
    -   @apollo/[email protected]

## @apollo/[email protected]

### Patch Changes

- Fix a query planning bug where invalid subgraph queries are generated
with `reuseQueryFragments` set true.
([#2952](#2952))
([#2963](#2963))

- Type conditioned fetching
([#2949](#2949))

When querying a field that is in a path of 2 or more unions, the query
planner was not able to handle different selections and would
aggressively collapse selections in fetches yielding an incorrect plan.

This change introduces new syntax to express type conditions in (key and
flatten) paths. Type conditioned fetching can be enabled through a flag,
and execution is supported in the router only. (#2938)

- Fixed query planner to pass the directives from original query to
subgraph operations (#2961)
([#2967](#2967))

- Updated dependencies
\[[`ec04c50b4fb832bfd281ecf9c0c2dd7656431b96`](ec04c50),
[`a494631918156f0431ceace74281c076cf1d5d51`](a494631)]:
    -   @apollo/[email protected]
    -   @apollo/[email protected]

## @apollo/[email protected]

### Patch Changes

- Updated dependencies
\[[`ec04c50b4fb832bfd281ecf9c0c2dd7656431b96`](ec04c50),
[`a494631918156f0431ceace74281c076cf1d5d51`](a494631)]:
    -   @apollo/[email protected]

## [email protected]

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.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.

Query planner generates invalid fetch node selection sets when reuseQueryFragments=true
2 participants