Skip to content

Commit b94b595

Browse files
committed
more docs
1 parent 7207a31 commit b94b595

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

rescript-relay-documentation/docs/interacting-with-the-store.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,82 @@ A good strategy is to use `commitLocalPayload` to insert new entites, and then m
209209

210210
> Note: You must use `@raw_response_type` to get access to `commitLocalPayload`. RescriptRelay won't expose it unless you use that directive.
211211
212+
## Bulk connection operations
213+
214+
When working with connections, you often need to perform operations that affect multiple connection instances at once. RescriptRelay provides utility functions that make these operations easier and more efficient.
215+
216+
### Finding all connection instances with `findAllConnectionIds`
217+
218+
`Environment.findAllConnectionIds` helps you find all cached instances of a specific connection, regardless of what filters or arguments were used when fetching them. This is particularly useful when you want to update all instances of a connection after a mutation.
219+
220+
```rescript
221+
// Find all instances of a user's posts connection
222+
let connectionIds = environment->RescriptRelay.Environment.findAllConnectionIds(
223+
~connectionKey="UserProfile_user_postsConnection",
224+
~parentId=userId
225+
)
226+
227+
// connectionIds now contains data IDs for all cached instances:
228+
// - UserProfile_user_postsConnection({"category": "tech"})
229+
// - UserProfile_user_postsConnection({"category": "personal"})
230+
// - UserProfile_user_postsConnection({}) // no filters
231+
// etc.
232+
```
233+
234+
### Bulk record invalidation with `invalidateRecordsByIds`
235+
236+
When you need to invalidate many records at once, `RecordSourceSelectorProxy.invalidateRecordsByIds` is more efficient than calling `invalidateRecord` on each record individually.
237+
238+
```rescript
239+
RescriptRelay.commitLocalUpdate(~environment, ~updater=store => {
240+
// Find all connection instances
241+
let connectionIds = environment->RescriptRelay.Environment.findAllConnectionIds(
242+
~connectionKey=UserProfile_user_postsConnection_graphql.connectionKey,
243+
~parentId=userId
244+
)
245+
246+
// Invalidate all of them at once
247+
store->RescriptRelay.RecordSourceSelectorProxy.invalidateRecordsByIds(connectionIds)
248+
})
249+
```
250+
251+
### Common patterns
252+
253+
These functions work great together for common scenarios:
254+
255+
**Invalidating all connection instances after a mutation:**
256+
257+
```rescript
258+
// After deleting a post, invalidate all posts connections for that user
259+
CreatePostMutation.commitMutation(
260+
~environment,
261+
~variables={...},
262+
~updater=(store, _response) => {
263+
let connectionIds = environment->RescriptRelay.Environment.findAllConnectionIds(
264+
~connectionKey=UserProfile_user_postsConnection_graphql.connectionKey,
265+
~parentId=userId
266+
)
267+
store->RescriptRelay.RecordSourceSelectorProxy.invalidateRecordsByIds(connectionIds)
268+
}
269+
)
270+
```
271+
272+
**Adding a new item to all connection instances:**
273+
274+
```rescript
275+
// Add new post to all cached connection instances
276+
CreatePostMutation.commitMutation(
277+
~environment,
278+
~variables={
279+
input: newPostData,
280+
connections: environment->RescriptRelay.Environment.findAllConnectionIds(
281+
~connectionKey=UserProfile_user_postsConnection_graphql.connectionKey,
282+
~parentId=userId
283+
)
284+
}
285+
)
286+
```
287+
212288
## Imperative updates
213289

214290
> Using imperative store updates has its place, but avoid it for as long as you can.

rescript-relay-documentation/docs/mutations.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,39 @@ The type safe connection id maker will make life _much_ easier when selecting an
154154

155155
All mutation functions you use take an optional prop called `updater`. `updater` is a function that receives the Relay store (`RecordSourceSelectorProxy.t`) and the `'response` from the mutation. It lets you apply any updates to the store in response to a mutation.
156156

157+
### Bulk connection operations in mutations
158+
159+
When working with mutations that affect connections, you often need to update multiple connection instances. RescriptRelay provides utility functions that make these operations efficient:
160+
161+
```rescript
162+
// Using findAllConnectionIds to add a new item to all connection instances
163+
AddPostMutation.commitMutation(
164+
~environment,
165+
~variables={
166+
input: newPostData,
167+
connections: environment->RescriptRelay.Environment.findAllConnectionIds(
168+
~connectionKey=UserProfile_user_postsConnection_graphql.connectionKey,
169+
~parentId=userId
170+
)
171+
}
172+
)
173+
174+
// Using invalidateRecordsByIds to invalidate multiple connections after a mutation
175+
DeletePostMutation.commitMutation(
176+
~environment,
177+
~variables={postId: deletedPostId},
178+
~updater=(store, _response) => {
179+
let connectionIds = environment->RescriptRelay.Environment.findAllConnectionIds(
180+
~connectionKey=UserProfile_user_postsConnection_graphql.connectionKey,
181+
~parentId=userId
182+
)
183+
store->RescriptRelay.RecordSourceSelectorProxy.invalidateRecordsByIds(connectionIds)
184+
}
185+
)
186+
```
187+
188+
> For more details on these functions, see [Bulk connection operations](interacting-with-the-store#bulk-connection-operations) and the [API Reference](api-reference#environmentfindallconnectionids).
189+
157190
## Declarative, optimistic updates
158191

159192
Optimistically updating your UI can do wonders for UX, and Relay provides all the primitives you need to do both simple and more advanced optimistic updates. Let's add a simple optimistic update to this mutation by giving Relay the response that we expect the server to return right away:

0 commit comments

Comments
 (0)