You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rescript-relay-documentation/docs/relay-environment.md
+70-4Lines changed: 70 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,17 @@ This section of the docs is quite lacking. However, most of the information you
12
12
13
13
> API reference for missing field handlers is [available here](api-reference#missingfieldhandler)
14
14
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.
16
26
17
27
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.
18
28
@@ -22,9 +32,65 @@ You create a missing field handler by using the appropriate make-method from the
22
32
23
33
1.[`makeScalarMissingFieldHandler`](api-reference#missingfieldhandlermakescalarmissingfieldhandler) for creating a missing field handler for scalar values (like a `name` on a `User`).
24
34
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
-
26
35
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`).
27
36
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
+
queryNodeQuery {
47
+
node(id: "123") {
48
+
...onUser {
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)
0 commit comments