Skip to content

Commit

Permalink
update context example (#3024)
Browse files Browse the repository at this point in the history
  • Loading branch information
clenfest authored Jun 11, 2024
1 parent 868c73c commit 23bc21e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
20 changes: 12 additions & 8 deletions docs/source/entities-advanced.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -963,9 +963,9 @@ In the example above, the argument `currencyCode: CurrencyCode!` wouldn't appear

</Note>

### Using multiple contexts
### Using type conditions in @fromContext

As an example using multiple contexts, the subgraph below has three entities using two contexts and a nested child entity referencing fields using those contexts:
In this example, note how the `@fromContext` directive uses a series of type condition to select the desired field when accessing `Child.prop1`. A type condition is not required if all possible contexts have a field present as is the case for `Child.prop2`.

```graphql title="Example: using multiple contexts"
type Query {
Expand All @@ -974,32 +974,36 @@ type Query {
c: C!
}

type A @key(fields: "id") @context(name: "context_1") @context(name: "context_2") {
type A @key(fields: "id") @context(name: "context_1"){
id: ID!
field: String!
someField: String!
child: Child!
}

type B @key(fields: "id") @context(name: "context_1") @context(name: "context_2") {
type B @key(fields: "id") @context(name: "context_1"){
id: ID!
field: String!
someField: String!
child: Child!
}

type C @key(fields: "id") @context(name: "context_1") {
id: ID!
anotherField: String!
field: String!
someOtherField: String!
child: Child!
}

type Child @key(fields: "id") {
id: ID!
prop1(
arg: String! @fromContext(field: "$context_1 { field }")
arg: String!
@fromContext(field: "$context_1 ... on A { someField } ... on B { someField } ... on C { someOtherField }")
): Int!
prop2(
arg: String!
@fromContext(field: "$context_2 ... A { field } ... B { field } ... C { anotherField }")
@fromContext(field: "$context_1 { field }")
): Int!
}
```
Expand Down Expand Up @@ -1094,4 +1098,4 @@ type Transaction @key(fields: "id") {
@fromContext(field: "$userContext { userCurrency { isoCode } }")
): Int!
}
```
```
34 changes: 26 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 23bc21e

Please sign in to comment.