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

Add specific language to context docs that reference resolvers are necessary for contextual fields #3102

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions docs/source/entities/use-contexts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,35 @@ type Transaction @key(fields: "id") {
): Int!
}
```

## A note on resolvers

Fields that are used with the context directives must be resolvable via a [reference resolver](/federation/entities/#2-define-a-reference-resolver). Query plans that fetch contextual fields are based on the entity where the `@context` directive is set. Therefore, all subgraphs that resolve that entity must support looking up their fields based on the entity's `@keys` fields.

Take the [previous example](#referencing-fields-across-subgraphs). Suppose the first subgraph has a single query to retrieve the logged in user:
Copy link
Contributor

Choose a reason for hiding this comment

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

Added this anchor, as I assumed that's the example we're talking about. Could you please confirm @clenfest ?


```graphql disableCopy showLineNumbers=false
type Query {
me: User!
}
```

The following example resolver would not correctly support contextual variables:

```typescript title="❌ Incorrect example" disableCopy showLineNumbers=false
Query: {
me: () => ({
id: 1, userCurrency: { id: 431, currencyCode: 'USD'}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be isoCode?

Suggested change
id: 1, userCurrency: { id: 431, currencyCode: 'USD'}
id: 1, userCurrency: { id: 431, isoCode: 'USD'}

})
}
```

This is because the fetch set to subgraph 1 to retrieve the `isoCode` would be done on the entity. Therefore a section to resolve a User by keys would need to be added:
Copy link
Contributor

@Meschreiber Meschreiber Aug 13, 2024

Choose a reason for hiding this comment

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

This sentence confuses me. Does it mean something like this:

Suggested change
This is because the fetch set to subgraph 1 to retrieve the `isoCode` would be done on the entity. Therefore a section to resolve a User by keys would need to be added:
It's an inadequate resolver because retrieving the `isoCode` relies on the retrieving the `User` entity itself. Therefore, your resolvers file must include a reference resolver for the `User` entity:

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm also a bit confused by the examples--the incorrect example shows just a resolver for the me query, while the addition below is a reference resolver for User. Would that me query resolver work as written with just the addition? Or does it have to be updated as well?


```typescript
User: () => {
__resolveReference(partialUser) {
return fetchUserById(partialUser.id);
}
}
```