Skip to content

Commit 682e4db

Browse files
committed
RFC: add "merge" functionality
1 parent a0809da commit 682e4db

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/query/core/buildSlice.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ import {
3535
QueryThunkArg,
3636
ThunkResult,
3737
} from './buildThunks'
38-
import { AssertTagTypes, EndpointDefinitions } from '../endpointDefinitions'
38+
import {
39+
AssertTagTypes,
40+
EndpointDefinitions,
41+
QueryDefinition,
42+
} from '../endpointDefinitions'
3943
import { applyPatches, Patch } from 'immer'
4044
import { onFocus, onFocusLost, onOffline, onOnline } from './setupListeners'
4145
import {
@@ -130,10 +134,14 @@ export function buildSlice({
130134
meta.arg.queryCacheKey,
131135
(substate) => {
132136
if (substate.requestId !== meta.requestId) return
137+
const { merge = (x: any) => x } = definitions[
138+
meta.arg.endpointName
139+
] as QueryDefinition<any, any, any, any>
140+
133141
substate.status = QueryStatus.fulfilled
134142
substate.data = copyWithStructuralSharing(
135143
substate.data,
136-
payload.result
144+
merge(payload.result, substate.data)
137145
)
138146
delete substate.error
139147
substate.fulfilledTimeStamp = payload.fulfilledTimeStamp

src/query/endpointDefinitions.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ export interface QueryExtraOptions<
147147
* Not to be used. A query should not invalidate tags in the cache.
148148
*/
149149
invalidatesTags?: never
150+
/**
151+
* Can be provided to merge the current cache value into the new cache value.
152+
*
153+
* Useful if you don't want a new request to completely override the current cache value,
154+
* maybe because you have manually updated it from another source and don't want those
155+
* updates to get lost.
156+
*/
157+
merge?(
158+
newCacheValue: ResultType,
159+
currentCacheValue: ResultType | undefined
160+
): ResultType
150161
/** @deprecated please use `onQuery` instead */
151162
onStart?(arg: QueryArg, queryApi: QueryApi<ReducerPath, any>): void
152163
/** @deprecated please use `onQuery` instead */

0 commit comments

Comments
 (0)