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 potential bug when an @interfaceObject has a @requires #2524

Merged
merged 1 commit into from Apr 12, 2023

Conversation

pcmanus
Copy link
Contributor

@pcmanus pcmanus commented Apr 11, 2023

When an @interfaceObject type has a field with a @requires and the query requests that field only for some specific implementations of the corresponding interface, then the generated query plan was sometimes invalid and could result in an invalid query to a subgraph (against a subgraph that rely on @apollo/subgraph, this lead the subgraph to produce an error message looking like "The _entities resolver tried to load an entity for type X, but no object or interface type of that name was found in the schema").

The underlying reason is that the plan was mistakenly requesting the required fields for any object of the interface (corresponding to the @interfaceObject in question), instead of only requesting them only for only the implementation types it needed to. Not only is that inefficient in principle, but this lead to invalid fetches because the "rewrite" logic used to fixup the __typename for @interfaceObject under the hood was only rewriting the types of the implementation types in question (only expecting those to need rewrite, which technically was correct) but was mistakenly getting values for other implementation types.

@pcmanus pcmanus requested a review from a team as a code owner April 11, 2023 15:20
@netlify
Copy link

netlify bot commented Apr 11, 2023

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

Visit the deploys page to approve it

Name Link
🔨 Latest commit eb7a22d

@changeset-bot
Copy link

changeset-bot bot commented Apr 11, 2023

🦋 Changeset detected

Latest commit: eb7a22d

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 11, 2023

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.

@@ -1598,9 +1598,18 @@ function addOneToKeyedUpdates(keyedUpdates: MultiMap<string, SelectionUpdate>, s
}
}

function maybeRebaseOnSchema(toRebase: CompositeType, schema: Schema): CompositeType {
Copy link
Contributor

Choose a reason for hiding this comment

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

This behavior is slightly different than I would expect. If the type is undefined in the new schema, why return the original type rather than, say, undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess that was me being overly cautious and defaulting to what was essentially the prior behaviour before this method was added if something unexpected happened. But truly, we should always find the type or we're having a bug, so I ended up changing this to an assertion. Probably better to get a signal early if we introduce/have such a bug (returning undefined would push the problem to the caller, which in that case wouldn't have much better it can do than failing, so having an assertion directly kept the code simpler; this is all non-exported methods after all).

When an `@interfaceObject` type has a field with a `@requires` and the
query requests that field only for some specific implementations of the
corresponding interface, then the generated query plan was sometimes
invalid and could result in an invalid query to a subgraph (against a
subgraph that rely on `@apollo/subgraph`, this lead the subgraph to
produce an error message looking like `"The _entities resolver tried to
load an entity for type X, but no object or interface type of that name
was found in the schema"`).

The underlying reason is that the plan was mistakenly requesting the
required fields for any object of the interface (corresponding to the
`@interfaceObject` in question), instead of only requesting them only
for only the implementation types it needed to. Not only is that
inefficient in principle, but this lead to invalid fetches because the
"rewrite" logic used to fixup the `__typename` for `@interfaceObject`
under the hood was only rewriting the types of the implementation types
in question (only expecting those to need rewrite, which technically was
correct) but was mistakenly getting values for other implementation
types.
@pcmanus pcmanus merged commit 179b460 into apollographql:main Apr 12, 2023
9 checks passed
pcmanus pushed a commit that referenced this pull request May 16, 2023
When an `@interfaceObject` type has a field with a `@requires` and the
query requests that field only for some specific implementations of the
corresponding interface, then the generated query plan was sometimes
invalid and could result in an invalid query to a subgraph (against a
subgraph that rely on `@apollo/subgraph`, this lead the subgraph to
produce an error message looking like `"The _entities resolver tried to
load an entity for type X, but no object or interface type of that name
was found in the schema"`).

The underlying reason is that the plan was mistakenly requesting the
required fields for any object of the interface (corresponding to the
`@interfaceObject` in question), instead of only requesting them only
for only the implementation types it needed to. Not only is that
inefficient in principle, but this lead to invalid fetches because the
"rewrite" logic used to fixup the `__typename` for `@interfaceObject`
under the hood was only rewriting the types of the implementation types
in question (only expecting those to need rewrite, which technically was
correct) but was mistakenly getting values for other implementation
types.
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.

None yet

2 participants