@@ -439,6 +439,10 @@ export abstract class ApolloCache {
439439 fragment ,
440440 fragmentName
441441 ) as TypedDocumentNode < TData , TVariables > ;
442+ const transform : (
443+ result : ApolloCache . WatchFragmentResult < TData >
444+ ) => ApolloCache . WatchFragmentResult < TData > =
445+ ( options as any ) [ Symbol . for ( "apollo.transform" ) ] ?? ( ( v ) => v ) ;
442446
443447 const fromArray = Array . isArray ( from ) ? from : [ from ] ;
444448
@@ -486,15 +490,17 @@ export abstract class ApolloCache {
486490 // without the need for a similar fallback since watchFragment with
487491 // arrays is new functionality in v4.1.
488492 transform : ( result ) =>
489- from === null ? result : { ...result , data : result . data ?? { } } ,
493+ transform (
494+ from === null ? result : { ...result , data : result . data ?? { } }
495+ ) ,
490496 } ) ;
491497 }
492498
493- let currentResult : ApolloCache . WatchFragmentResult < any > ;
499+ let currentResult : ApolloCache . WatchFragmentResult < TData > ;
494500 function toResult (
495501 results : Array < ApolloCache . WatchFragmentResult < Unmasked < TData > | null > >
496502 ) : ApolloCache . WatchFragmentResult < any > {
497- const result = results . reduce (
503+ let result = results . reduce (
498504 ( finalResult , res , idx ) => {
499505 const result = res as ApolloCache . WatchFragmentResult < TData > ;
500506
@@ -513,9 +519,11 @@ export abstract class ApolloCache {
513519 data : [ ] ,
514520 dataState : "complete" ,
515521 complete : true ,
516- } as ApolloCache . WatchFragmentResult < any >
522+ } as ApolloCache . WatchFragmentResult < TData >
517523 ) ;
518524
525+ result = transform ( result ) ;
526+
519527 if ( ! equal ( currentResult , result ) ) {
520528 currentResult = result ;
521529 }
@@ -617,7 +625,15 @@ export abstract class ApolloCache {
617625 this . onAfterBroadcast ( ( ) => {
618626 const result = transform ( toWatchFragmentResult ( diff ) ) ;
619627
620- if ( ! equal ( currentResult , result ) ) {
628+ if (
629+ ! currentResult ||
630+ ! equalByQuery (
631+ fragmentQuery ,
632+ { data : currentResult . data } ,
633+ { data : result . data } ,
634+ options . variables
635+ )
636+ ) {
621637 currentResult = result ;
622638 }
623639
@@ -632,14 +648,7 @@ export abstract class ApolloCache {
632648 this . fragmentWatches . removeArray ( cacheKey ) ;
633649 } ;
634650 } ) . pipe (
635- distinctUntilChanged ( ( previous , current ) =>
636- equalByQuery (
637- fragmentQuery ,
638- { data : previous . data } ,
639- { data : current . data } ,
640- options . variables
641- )
642- ) ,
651+ distinctUntilChanged ( ) ,
643652 share ( {
644653 connector : ( ) => new ReplaySubject ( 1 ) ,
645654 // debounce so a synchronous unsubscribe+resubscribe doesn't tear down the watch and create a new one
0 commit comments