diff --git a/docs/source/entities/use-contexts.mdx b/docs/source/entities/use-contexts.mdx index e782e4b53..10dc4acb5 100644 --- a/docs/source/entities/use-contexts.mdx +++ b/docs/source/entities/use-contexts.mdx @@ -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'} + }) +} +``` + +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: + +```typescript +User: () => { + __resolveReference(partialUser) { + return fetchUserById(partialUser.id); + } +} +``` \ No newline at end of file