@@ -20,6 +20,7 @@ import { ApolloClient, NetworkStatus } from "@apollo/client";
2020import type {
2121 NormalizedCacheObject ,
2222 PossibleTypesMap ,
23+ Reference ,
2324} from "@apollo/client/cache" ;
2425import {
2526 createFragmentRegistry ,
@@ -3201,6 +3202,143 @@ describe("client", () => {
32013202
32023203 expect ( actualResult . data ) . toEqual ( result ) ;
32033204 } ) ;
3205+
3206+ it ( "handles read functions on arrays that return undefined items" , async ( ) => {
3207+ const query : TypedDocumentNode < {
3208+ books : Array < { __typename : "Book" ; id : number ; name : string } > ;
3209+ } > = gql `
3210+ query {
3211+ books {
3212+ id
3213+ name
3214+ }
3215+ }
3216+ ` ;
3217+
3218+ const link = new MockLink ( [
3219+ {
3220+ request : { query } ,
3221+ result : {
3222+ data : {
3223+ books : [
3224+ { __typename : "Book" , id : 1 , name : "Book 1 (fetch)" } ,
3225+ { __typename : "Book" , id : 2 , name : "Book 2 (fetch)" } ,
3226+ { __typename : "Book" , id : 3 , name : "Book 3 (fetch)" } ,
3227+ ] ,
3228+ } ,
3229+ } ,
3230+ } ,
3231+ ] ) ;
3232+
3233+ const cache = new InMemoryCache ( {
3234+ typePolicies : {
3235+ Query : {
3236+ fields : {
3237+ books : ( existing : Reference [ ] = [ ] , { canRead } ) => {
3238+ return existing . map ( ( book ) => ( canRead ( book ) ? book : undefined ) ) ;
3239+ } ,
3240+ } ,
3241+ } ,
3242+ } ,
3243+ } ) ;
3244+
3245+ const client = new ApolloClient ( { link, cache } ) ;
3246+
3247+ client . writeQuery ( {
3248+ query,
3249+ data : {
3250+ books : [
3251+ { __typename : "Book" , id : 1 , name : "Book 1 (cache)" } ,
3252+ { __typename : "Book" , id : 2 , name : "Book 2 (cache)" } ,
3253+ ] ,
3254+ } ,
3255+ } ) ;
3256+
3257+ const stream = new ObservableStream ( client . watchQuery ( { query } ) ) ;
3258+ const partialStream = new ObservableStream (
3259+ client . watchQuery ( {
3260+ query,
3261+ returnPartialData : true ,
3262+ } )
3263+ ) ;
3264+
3265+ await expect ( stream ) . toEmitTypedValue ( {
3266+ data : {
3267+ books : [
3268+ { __typename : "Book" , id : 1 , name : "Book 1 (cache)" } ,
3269+ { __typename : "Book" , id : 2 , name : "Book 2 (cache)" } ,
3270+ ] ,
3271+ } ,
3272+ dataState : "complete" ,
3273+ loading : false ,
3274+ networkStatus : NetworkStatus . ready ,
3275+ partial : false ,
3276+ } ) ;
3277+
3278+ await expect ( partialStream ) . toEmitTypedValue ( {
3279+ data : {
3280+ books : [
3281+ { __typename : "Book" , id : 1 , name : "Book 1 (cache)" } ,
3282+ { __typename : "Book" , id : 2 , name : "Book 2 (cache)" } ,
3283+ ] ,
3284+ } ,
3285+ dataState : "complete" ,
3286+ loading : false ,
3287+ networkStatus : NetworkStatus . ready ,
3288+ partial : false ,
3289+ } ) ;
3290+
3291+ cache . evict ( { id : cache . identify ( { __typename : "Book" , id : 2 } ) } ) ;
3292+
3293+ await expect ( stream ) . toEmitSimilarValue ( {
3294+ expected : ( previous ) => ( {
3295+ ...previous ,
3296+ loading : true ,
3297+ networkStatus : NetworkStatus . loading ,
3298+ } ) ,
3299+ } ) ;
3300+ await expect ( partialStream ) . toEmitTypedValue ( {
3301+ data : {
3302+ books : [ { __typename : "Book" , id : 1 , name : "Book 1 (cache)" } , { } ] ,
3303+ } ,
3304+ dataState : "partial" ,
3305+ loading : true ,
3306+ networkStatus : NetworkStatus . loading ,
3307+ partial : true ,
3308+ } ) ;
3309+
3310+ await expect ( stream ) . toEmitSimilarValue ( {
3311+ expected : ( previous ) => ( {
3312+ ...previous ,
3313+ data : {
3314+ books : [
3315+ { __typename : "Book" , id : 1 , name : "Book 1 (fetch)" } ,
3316+ { __typename : "Book" , id : 2 , name : "Book 2 (fetch)" } ,
3317+ { __typename : "Book" , id : 3 , name : "Book 3 (fetch)" } ,
3318+ ] ,
3319+ } ,
3320+ dataState : "complete" ,
3321+ loading : false ,
3322+ networkStatus : NetworkStatus . ready ,
3323+ } ) ,
3324+ } ) ;
3325+ await expect ( partialStream ) . toEmitTypedValue ( {
3326+ data : {
3327+ books : [
3328+ { __typename : "Book" , id : 1 , name : "Book 1 (fetch)" } ,
3329+ { __typename : "Book" , id : 2 , name : "Book 2 (fetch)" } ,
3330+ { __typename : "Book" , id : 3 , name : "Book 3 (fetch)" } ,
3331+ ] ,
3332+ } ,
3333+ dataState : "complete" ,
3334+ loading : false ,
3335+ networkStatus : NetworkStatus . ready ,
3336+ partial : false ,
3337+ } ) ;
3338+
3339+ await expect ( stream ) . not . toEmitAnything ( ) ;
3340+ await expect ( partialStream ) . not . toEmitAnything ( ) ;
3341+ } ) ;
32043342} ) ;
32053343
32063344describe ( "@connection" , ( ) => {
0 commit comments