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

generateQueryFragments generates invalid plans with missing fragment definitions #2993

Merged
merged 5 commits into from
May 2, 2024

Conversation

xuorig
Copy link
Contributor

@xuorig xuorig commented May 2, 2024

This PR includes a failing test that show that generateQueryFragments can generate invalid GraphQL queries. It seems related to the way the hashCode is computed. Any selections of the same length and type are somehow grouped together and an additional fragment definition is not added.

Opening this in case somebody gets to the root cause fix while I investigate further.

@xuorig xuorig requested a review from a team as a code owner May 2, 2024 15:03
Copy link

netlify bot commented May 2, 2024

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

Visit the deploys page to approve it

Name Link
🔨 Latest commit c65cb04

Copy link

changeset-bot bot commented May 2, 2024

🦋 Changeset detected

Latest commit: c65cb04

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/gateway Patch
@apollo/federation-internals Patch
@apollo/composition 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

y
}

fragment _generated_onA2_1 on A {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fragment is missing in practice.

Copy link

codesandbox-ci bot commented May 2, 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.

}
namedFragments.add(fragmentDefinition);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we get here, it's because we didn't find an exact match. In these cases, we need to create a fragment definition in all cases. Double check me here, but this does seem to fix the issue.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, agree. Small nit, but could we bump this line up to where fragmentDefinition is assigned? Probably makes more sense right next to it.

Appreciate the if/else restructure 👍

@xuorig xuorig force-pushed the generate-fragment-aliases-bug branch from ed80be0 to f7644ed Compare May 2, 2024 15:41
Copy link
Member

@trevor-scheer trevor-scheer left a comment

Choose a reason for hiding this comment

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

Can you please add a changeset by running npx changeset and following the prompts for a patch? A small description of how a user might be affected by this bug would be great. Thanks for the very clear PR and test case!

}
namedFragments.add(fragmentDefinition);
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, agree. Small nit, but could we bump this line up to where fragmentDefinition is assigned? Probably makes more sense right next to it.

Appreciate the if/else restructure 👍

@xuorig
Copy link
Contributor Author

xuorig commented May 2, 2024

Addressed your comments @trevor-scheer 🙇 thanks for the quick review!

"@apollo/query-planner": patch
---

Fix issue with missing fragment definitions due to generateQueryFragments
Copy link
Member

Choose a reason for hiding this comment

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

I can't send this as a GH suggestion due to use of triple backtick in the comment, but would you mind adding this or something like it?

Fix issue with missing fragment definitions due to generateQueryFragments

An incorrect impementation detail in generateQueryFragments caused certain queries to be missing fragment definitions. Specifically, subsequent fragment "candidates" with the same type condition and the same length of selections as a previous fragment weren't correctly added to the list of fragments. An example of an affected query is:

query {
  t {
    ... on A {
      x
      y
    }
  }
  t2 {
    ... on A {
      y
      z
    }
  }
}

In this case, the second selection set would be converted to an inline fragment spread to subgraph fetches, but the fragment definition would be missing.

Copy link
Member

Choose a reason for hiding this comment

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

Ideally the changeset should have the ```graphql code fencing around the query, but that doesn't paste over from my comment.

@xuorig xuorig force-pushed the generate-fragment-aliases-bug branch from 6aeb8d1 to c65cb04 Compare May 2, 2024 16:45
Copy link
Member

@trevor-scheer trevor-scheer left a comment

Choose a reason for hiding this comment

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

Thank you!

Copy link
Member

@dariuszkuc dariuszkuc left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

@dariuszkuc dariuszkuc merged commit af4376f into apollographql:main May 2, 2024
10 of 11 checks passed
@github-actions github-actions bot mentioned this pull request May 2, 2024
@xuorig xuorig deleted the generate-fragment-aliases-bug branch May 2, 2024 19:01
dariuszkuc pushed a commit that referenced this pull request May 2, 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

-   Updated dependencies \[]:
    -   @apollo/[email protected]
    -   @apollo/[email protected]

## @apollo/[email protected]

### Patch Changes

- Updated dependencies
\[[`af4376f348d21ad4d8eca0e3d2a170600f391e4d`](af4376f)]:
    -   @apollo/[email protected]
    -   @apollo/[email protected]
    -   @apollo/[email protected]

## @apollo/[email protected]

### Patch Changes

-   Updated dependencies \[]:
    -   @apollo/[email protected]

## @apollo/[email protected]

### Patch Changes

- Fix issue with missing fragment definitions due to
`generateQueryFragments`.
([#2993](#2993))

An incorrect implementation detail in `generateQueryFragments` caused
certain queries to be missing fragment definitions. Specifically,
subsequent fragment "candidates" with the same type condition and the
same length of selections as a previous fragment weren't correctly added
to the list of fragments. An example of an affected query is:

    ```graphql
    query {
      t {
        ... on A {
          x
          y
        }
      }
      t2 {
        ... on A {
          y
          z
        }
      }
    }
    ```

In this case, the second selection set would be converted to an inline
fragment spread to subgraph fetches, but the fragment definition would
be missing.

-   Updated dependencies \[]:
    -   @apollo/[email protected]
    -   @apollo/[email protected]

## @apollo/[email protected]

### Patch Changes

-   Updated dependencies \[]:
    -   @apollo/[email protected]

## @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.

3 participants