Skip to content

Commit

Permalink
add invalidateRecordsByIds
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Aug 10, 2024
1 parent 83a02d7 commit 2463c88
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# master

- Add `Environment.findAllConnectionIds` for finding all IDs of all connection instances for a specific connection, regardless of what configs that connection has been fetched (and cached) with.
- Add `RecordSourceSelectorProxy.invalidateRecordsByIds` for invalidating multilple records at the same time.

# 3.0.0 stable

Expand Down
22 changes: 22 additions & 0 deletions packages/rescript-relay/src/RescriptRelay.res
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ module RecordSourceSelectorProxy = {
getPluralRootField(t, ~fieldName)->optArrayOfNullableToOptArrayOfOpt

@send external invalidateStore: t => unit = "invalidateStore"

let invalidateRecordsByIds: (t, array<dataId>) => unit = (store, recordIds) => {
recordIds->Js.Array2.forEach(dataId => {
store->get(~dataId)->Belt.Option.forEach(r => r->RecordProxy.invalidateRecord)
})
}
}

module ReadOnlyRecordSourceProxy = {
Expand Down Expand Up @@ -735,6 +741,10 @@ module Environment = {
external commitPayload: (t, operationDescriptor, 'payload) => unit = "commitPayload"
@send external retain: (t, operationDescriptor) => Disposable.t = "retain"

@module("relay-runtime")
external commitLocalUpdate: (t, ~updater: RecordSourceSelectorProxy.t => unit) => unit =
"commitLocalUpdate"

@send external mapGet: (Js.Map.t<'key, 'value>, 'key) => option<'value> = "get"

type recordValue = {__ref: dataId}
Expand All @@ -755,6 +765,18 @@ module Environment = {
}
ids
}

let invalidateAllOfConnection = (environment: t, ~connectionKey: string, ~parentId: dataId) => {
environment->commitLocalUpdate(~updater=store => {
environment
->findAllConnectionIds(~connectionKey, ~parentId)
->Js.Array2.forEach(dataId => {
store
->RecordSourceSelectorProxy.get(~dataId)
->Belt.Option.forEach(r => r->RecordProxy.invalidateRecord)
})
})
}
}

module Context = {
Expand Down
6 changes: 6 additions & 0 deletions packages/rescript-relay/src/RescriptRelay.resi
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ module RecordSourceSelectorProxy: {
/**Invalidates the entire store. This means that _at the next render_, the entire store will be treated as empty, meaning Relay will refetch everything it needs to show the view it's to show.*/
@send
external invalidateStore: t => unit = "invalidateStore"

/**Invalidates each of the provided records by their ID, if they exist. */
let invalidateRecordsByIds: (t, array<dataId>) => unit
}

/**ReadOnlyRecordSourceProxy is the store, but in read-only mode.*/
Expand Down Expand Up @@ -788,6 +791,9 @@ You should use the generated `Query.retain` function on your queries instead of

/**Find all connection IDs for a specific connection and on a specific object. Useful together with `@deleteEdge` and similar where you want to remove something from all connection configurations. */
let findAllConnectionIds: (t, ~connectionKey: string, ~parentId: dataId) => array<dataId>

/**Invalidates all connection configurations of `connectionKey` on `parentId`.*/
let invalidateAllOfConnection: (t, ~connectionKey: string, ~parentId: dataId) => unit
}

/**fetchPolicy controls how you want Relay to resolve your data.*/
Expand Down

0 comments on commit 2463c88

Please sign in to comment.