1+ import { MultiDict } from 'multidict' ;
12import { pick , type BaseGeneratedSchema , type FetchOptions } from '.' ;
23import { createSchemaAccessor } from '../Accessor' ;
34import { type Cache } from '../Cache' ;
@@ -174,7 +175,7 @@ const pendingQueries = new WeakMap<Set<Set<Selection>>, Promise<unknown>>();
174175export const createResolvers = < TSchema extends BaseGeneratedSchema > ( {
175176 aliasLength,
176177 batchWindow,
177- cache : clientCache ,
178+ cache : targetCache ,
178179 debugger : debug ,
179180 depthLimit,
180181 fetchOptions,
@@ -186,6 +187,11 @@ export const createResolvers = <TSchema extends BaseGeneratedSchema>({
186187 schema,
187188 parentContext,
188189} : CreateResolversOptions ) : Resolvers < TSchema > => {
190+ // A temporary cache is created by the resolver context for no-cache policy.
191+ // When multiple queries are batched, all corresponding temporary caches must
192+ // be updated. Along with the target cache.
193+ const correlatedCaches = new MultiDict < Set < unknown > , Cache > ( ) ;
194+
189195 const createResolver = ( {
190196 cachePolicy = defaultCachePolicy ,
191197 extensions,
@@ -197,7 +203,7 @@ export const createResolvers = <TSchema extends BaseGeneratedSchema>({
197203 const selections = new Set < Selection > ( ) ;
198204 const context = createContext ( {
199205 aliasLength,
200- cache : clientCache ,
206+ cache : targetCache ,
201207 depthLimit,
202208 cachePolicy,
203209 scalars,
@@ -238,15 +244,23 @@ export const createResolvers = <TSchema extends BaseGeneratedSchema>({
238244 }
239245
240246 // Batch selections up at client level, fetch all of them "next tick".
241- const selectionsCacheKey = operationName ?? '' ;
247+ // 1. Query with operation names are never batched up with others.
248+ // 2. 'no-store' queries are tracked separately because its data is not
249+ // going into the main cache.
250+ const selectionsCacheKey = `${ operationName ?? ( cachePolicy === 'no-store' ? 'no-store' : 'default' ) } ` ;
242251
243252 const pendingSelections = addSelections (
244- clientCache ,
253+ targetCache ,
245254 selectionsCacheKey ,
246255 selections
247256 ) ;
248257
258+ // Link temporary caches together
259+ correlatedCaches . set ( pendingSelections , context . cache ) ;
260+
249261 if ( ! pendingQueries . has ( pendingSelections ) ) {
262+ // [ ] debounceMicrotask
263+
250264 pendingQueries . set (
251265 pendingSelections ,
252266 // Batching happens at the end of microtask queue
@@ -260,7 +274,7 @@ export const createResolvers = <TSchema extends BaseGeneratedSchema>({
260274
261275 const uniqueSelections = new Set < Selection > ( ) ;
262276
263- getSelectionsSet ( clientCache , selectionsCacheKey ) ?. forEach (
277+ getSelectionsSet ( targetCache , selectionsCacheKey ) ?. forEach (
264278 ( selections ) => {
265279 selections . forEach ( ( selection ) => {
266280 uniqueSelections . add ( selection ) ;
@@ -270,7 +284,7 @@ export const createResolvers = <TSchema extends BaseGeneratedSchema>({
270284
271285 pendingQueries . delete ( pendingSelections ) ;
272286
273- delSelectionSet ( clientCache , selectionsCacheKey ) ;
287+ delSelectionSet ( targetCache , selectionsCacheKey ) ;
274288
275289 const results = await fetchSelections ( uniqueSelections , {
276290 cache : context . cache ,
@@ -282,12 +296,15 @@ export const createResolvers = <TSchema extends BaseGeneratedSchema>({
282296
283297 updateCaches (
284298 results ,
285- cachePolicy !== 'no-store' && context . cache !== clientCache
286- ? [ context . cache , clientCache ]
287- : [ context . cache ] ,
299+ [
300+ ...( cachePolicy === 'no-store' ? [ ] : [ targetCache ] ) ,
301+ ...( correlatedCaches . get ( pendingSelections ) ?? [ ] ) ,
302+ ] ,
288303 { skipNotify : ! context . notifyCacheUpdate }
289304 ) ;
290305
306+ correlatedCaches . delete ( targetCache ) ;
307+
291308 return results ;
292309 } )
293310 ) ;
@@ -375,8 +392,8 @@ export const createResolvers = <TSchema extends BaseGeneratedSchema>({
375392 } else if ( data !== undefined ) {
376393 updateCaches (
377394 [ { data, error, extensions } ] ,
378- cachePolicy !== 'no-store' && context . cache !== clientCache
379- ? [ context . cache , clientCache ]
395+ cachePolicy !== 'no-store' && context . cache !== targetCache
396+ ? [ context . cache , targetCache ]
380397 : [ context . cache ] ,
381398 { skipNotify : ! context . notifyCacheUpdate }
382399 ) ;
0 commit comments