Skip to content

Commit 51eb7ef

Browse files
committed
document more missing field handlers
1 parent b94b595 commit 51eb7ef

1 file changed

Lines changed: 70 additions & 4 deletions

File tree

rescript-relay-documentation/docs/relay-environment.md

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,17 @@ This section of the docs is quite lacking. However, most of the information you
1212
1313
> API reference for missing field handlers is [available here](api-reference#missingfieldhandler)
1414
15-
You can teach Relay about relations in your schema in order to increase cache hits. In general, there should be no need to use this for most projects. But, occasionally there will be good reason to do so.
15+
Missing field handlers are a powerful feature that allows you to teach Relay about relations in your schema in order to increase cache hits. They help Relay understand that different queries may point to the same data, enabling better cache reuse.
16+
17+
### When to use missing field handlers
18+
19+
Missing field handlers are useful when:
20+
21+
1. **Different queries access the same data**: For example, `user(id: 123)` and `node(id: 123)` might return the same user object
22+
2. **You want to derive scalar values**: Computing values from existing cached data
23+
3. **You need to resolve relationships**: Helping Relay understand connections between objects in your cache
24+
25+
In general, there should be no need to use this for most projects. But, occasionally there will be good reason to do so.
1626

1727
The section below details how missing field handlers work in `RescriptRelay`. Please start by reading the Relay documentation linked above to have a good understanding of how this works in Relay in general.
1828

@@ -22,9 +32,65 @@ You create a missing field handler by using the appropriate make-method from the
2232

2333
1. [`makeScalarMissingFieldHandler`](api-reference#missingfieldhandlermakescalarmissingfieldhandler) for creating a missing field handler for scalar values (like a `name` on a `User`).
2434
2. [`makeLinkedMissingFieldHandler`](api-reference#missingfieldhandlermakelinkedmissingfieldhandler) for creating a missing field handler for a single linked record (like a `Pet` on the field `favoritePet` on a `User`).
25-
2635
3. [`makePluralLinkedMissingFieldHandler`](api-reference#missingfieldhandlermakeplurallinkedmissingfieldhandler) for creating a missing field handler for lists of linked records (like a list of `Pet` on the field `allPets` on a `User`).
2736

28-
#### Examples
37+
### Built-in Node Interface Missing Field Handler
38+
39+
RescriptRelay ships with a built-in missing field handler for the [Node interface](https://graphql.org/learn/global-object-identification/), which is automatically enabled when you create an environment.
40+
41+
#### What it does
42+
43+
The node interface missing field handler enables automatic resolution of cached items through the `node` field. It teaches Relay that the top level `node` field can do a cache-lookup from the ID it gets:
44+
45+
```graphql
46+
query NodeQuery {
47+
node(id: "123") {
48+
... on User {
49+
name
50+
}
51+
}
52+
}
53+
```
54+
55+
If you've previously fetched `User` with the `id` of `123`, and later execute the `NodeQuery`, Relay will automatically resolve the data from cache without making a network request.
56+
57+
#### Automatic enablement
58+
59+
The node interface handler is automatically included when you create an environment:
60+
61+
```rescript
62+
// When you create an environment like this:
63+
let environment = RescriptRelay.Environment.make(
64+
~network,
65+
~store,
66+
~missingFieldHandlers=[customHandler1, customHandler2], // Your custom handlers
67+
)
68+
69+
// RescriptRelay automatically adds the node interface handler:
70+
// The actual handlers array becomes: [customHandler1, customHandler2, nodeInterfaceMissingFieldHandler]
71+
```
72+
73+
If you don't provide any custom handlers, only the node interface handler is used:
74+
75+
```rescript
76+
let environment = RescriptRelay.Environment.make(~network, ~store)
77+
// Handlers array: [nodeInterfaceMissingFieldHandler]
78+
```
79+
80+
### Adding custom missing field handlers
81+
82+
To add your own missing field handlers to an environment:
83+
84+
```rescript
85+
let customHandlers = [
86+
fullNameHandler,
87+
userLinkHandler,
88+
userPostsHandler,
89+
]
2990
30-
Coming soon.
91+
let environment = RescriptRelay.Environment.make(
92+
~network,
93+
~store,
94+
~missingFieldHandlers=customHandlers,
95+
)
96+
```

0 commit comments

Comments
 (0)