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 handling of __typename optimizations #3156

Merged
merged 3 commits into from
Sep 26, 2024
Merged

Conversation

dariuszkuc
Copy link
Member

Fixes handling of a __typename selection during query planning process.

When expanding fragments we were keeping references to the same Fields regardless where those fragments appeared in our original selection set. This was generally fine as in most cases we would have same inline fragment selection sets across whole operation but was causing problems when we were applying another optimization by collapsing those expanded inline fragments creating a new selection set. As a result, if any single field selection (within that fragment) would perform optimization around the usage of __typename, ALL occurrences of that field selection would get that optimization as well. See example below

foo {
  f1 {
    ... on Bar {
      __typename
      ...onBar # will be collapsed and sub selections will optimize __typename
    }
  }
  f2 {
    ...onBar # sub selections will get __typename optimization from above
  }
}

fragment onBar on Bar {
  b
  c
}

Fixes handling of a `__typename` selection during query planning process.

When expanding fragments we were keeping references to the same `Field`s regardless where those fragments appeared in our original selection set. This was generally fine as in most cases we would have same inline fragment selection sets across whole operation but was causing problems when we were applying another optimization by collapsing those expanded inline fragments creating a new selection set. As a result, if any single field selection (within that fragment) would perform optimization around the usage of `__typename`, ALL occurrences of that field selection would get that optimization as well. See example below

```graphql
foo {
  f1 {
    ... on Bar {
      __typename
      ...onBar # will be collapsed and sub selections will optimize __typename
    }
  }
  f2 {
    ...onBar # sub selections will get __typename optimization from above
  }
}

fragment onBar on Bar {
  b
  c
}
```
@dariuszkuc dariuszkuc requested a review from a team as a code owner September 26, 2024 18:31
Copy link

changeset-bot bot commented Sep 26, 2024

🦋 Changeset detected

Latest commit: f168c7b

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

Copy link

netlify bot commented Sep 26, 2024

Deploy Preview for apollo-federation-docs canceled.

Name Link
🔨 Latest commit f168c7b
🔍 Latest deploy log https://app.netlify.com/sites/apollo-federation-docs/deploys/66f5c6f73af5680008ce4533

Copy link

codesandbox-ci bot commented Sep 26, 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.

Copy link
Contributor

@sachindshinde sachindshinde left a comment

Choose a reason for hiding this comment

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

Pushed up some changes in this commit to fix some things, other than that looks good to me.

To summarize:

  1. In the changes to optimizeSiblingTypenames(), a new variable named firstFieldIndex was being set, that is expected to be the index of the first non-optimized-away field in updatedSelections (which is only populated if there are any selections to update, i.e. if a __typename field is found, or a field/inline fragment's selection set was recursively updated). The code before was not setting firstFieldIndex if updatedSelections was falsy, i.e. if no selections to update had been encountered by that point in the loop. I've updated optimizeSiblingTypenames() to always set firstFieldIndex when firstFieldSelection is set (regardless of whether updatedSelections actually gets populated).
  2. I've moved the Field-copying code inside FieldSelection.withAttachment() into a new Field.copy() method that's next to the other Field.with*() methods. The idea here is that if someone adds an optional argument to Field's constructor, it would be easier to not accidentally omit this argument copying if the methods that copy were inside Field (IMO we probably shouldn't be using optional arguments in those constructors to begin with, but that's not something to worry about dealing with now).
    • There's also a slight change here, in that we copy the attachments from the old field as well. This is for if we ever decide to add more attachments in the future, and shouldn't affect the case of the typename attachment (since we don't try to sibling-optimize the results of a sibling-optimized query).
  3. I've updated comments for the attachement->attachment rename.

@sachindshinde sachindshinde merged commit 2192f35 into main Sep 26, 2024
19 checks passed
@sachindshinde sachindshinde deleted the fix_sibling_typename branch September 26, 2024 21:19
sachindshinde pushed a commit that referenced this pull request Sep 27, 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/composition@2.9.2

### Patch Changes

- Updated dependencies
\[[`2192f355f50db33fe0807d16153f357696b9f190`](2192f35),
[`e1e2605b30efc488b57f62ba43436606a38a3607`](e1e2605),
[`5ac01b534318105e904c1e6598070f753add3bb1`](5ac01b5)]:
    -   @apollo/federation-internals@2.9.2
    -   @apollo/query-graphs@2.9.2

## @apollo/gateway@2.9.2

### Patch Changes

- Updated dependencies
\[[`2192f355f50db33fe0807d16153f357696b9f190`](2192f35),
[`e1e2605b30efc488b57f62ba43436606a38a3607`](e1e2605),
[`5ac01b534318105e904c1e6598070f753add3bb1`](5ac01b5)]:
    -   @apollo/query-planner@2.9.2
    -   @apollo/federation-internals@2.9.2
    -   @apollo/composition@2.9.2

## @apollo/federation-internals@2.9.2

### Patch Changes

- Fixes handling of a `__typename` selection during query planning
process.
([#3156](#3156))

When expanding fragments we were keeping references to the same `Field`s
regardless where those fragments appeared in our original selection set.
This was generally fine as in most cases we would have same inline
fragment selection sets across whole operation but was causing problems
when we were applying another optimization by collapsing those expanded
inline fragments creating a new selection set. As a result, if any
single field selection (within that fragment) would perform optimization
around the usage of `__typename`, ALL occurrences of that field
selection would get that optimization as well.

- Add validations for demand control directive applications
([#3148](#3148))

## @apollo/query-graphs@2.9.2

### Patch Changes

- Fixes issue where contextual parameters can have naming collisions if
used in multiple subgraphs
([#3155](#3155))

- Updated dependencies
\[[`2192f355f50db33fe0807d16153f357696b9f190`](2192f35),
[`5ac01b534318105e904c1e6598070f753add3bb1`](5ac01b5)]:
    -   @apollo/federation-internals@2.9.2

## @apollo/query-planner@2.9.2

### Patch Changes

- Fixes handling of a `__typename` selection during query planning
process.
([#3156](#3156))

When expanding fragments we were keeping references to the same `Field`s
regardless where those fragments appeared in our original selection set.
This was generally fine as in most cases we would have same inline
fragment selection sets across whole operation but was causing problems
when we were applying another optimization by collapsing those expanded
inline fragments creating a new selection set. As a result, if any
single field selection (within that fragment) would perform optimization
around the usage of `__typename`, ALL occurrences of that field
selection would get that optimization as well.

- Fixes issue where contextual parameters can have naming collisions if
used in multiple subgraphs
([#3155](#3155))

- Updated dependencies
\[[`2192f355f50db33fe0807d16153f357696b9f190`](2192f35),
[`e1e2605b30efc488b57f62ba43436606a38a3607`](e1e2605),
[`5ac01b534318105e904c1e6598070f753add3bb1`](5ac01b5)]:
    -   @apollo/federation-internals@2.9.2
    -   @apollo/query-graphs@2.9.2

## @apollo/subgraph@2.9.2

### Patch Changes

- Updated dependencies
\[[`2192f355f50db33fe0807d16153f357696b9f190`](2192f35),
[`5ac01b534318105e904c1e6598070f753add3bb1`](5ac01b5)]:
    -   @apollo/federation-internals@2.9.2

## apollo-federation-integration-testsuite@2.9.2

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants