-
Notifications
You must be signed in to change notification settings - Fork 257
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
base: main
Are you sure you want to change the base?
Add specific language to context docs that reference resolvers are necessary for contextual fields #3102
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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: | ||||||
|
||||||
```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'} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be
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: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence confuses me. Does it mean something like this:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
|
||||||
```typescript | ||||||
User: () => { | ||||||
__resolveReference(partialUser) { | ||||||
return fetchUserById(partialUser.id); | ||||||
} | ||||||
} | ||||||
``` |
There was a problem hiding this comment.
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 ?