@@ -433,6 +433,165 @@ class CacheOptionsTest {
433433 }
434434 }
435435
436+ @Test
437+ fun listsFromDifferentFieldsMemory () = runTest(before = { setUp() }, after = { tearDown() }) {
438+ listsFromDifferentFields(memoryCacheManager)
439+ }
440+
441+ @Test
442+ fun listsFromDifferentFieldsSql () = runTest(before = { setUp() }, after = { tearDown() }) {
443+ listsFromDifferentFields(sqlCacheManager)
444+ }
445+
446+ @Test
447+ fun listsFromDifferentFieldsMemoryThenSql () = runTest(before = { setUp() }, after = { tearDown() }) {
448+ listsFromDifferentFields(memoryThenSqlCacheManager)
449+ }
450+
451+ private suspend fun listsFromDifferentFields (cacheManager : CacheManager ) {
452+ mockServer.enqueueString(
453+ // language=JSON
454+ """
455+ {
456+ "data": {
457+ "allUsers": [
458+ {
459+ "__typename": "User",
460+ "id": "1",
461+ "firstName": "John",
462+ "lastName": "Smith",
463+ "email": "jsmith@example.com"
464+ },
465+ {
466+ "__typename": "User",
467+ "id": "2",
468+ "firstName": "Jane",
469+ "lastName": "Doe",
470+ "email": "jdoe@example.com"
471+ },
472+ null
473+ ]
474+ },
475+ "errors": [
476+ {
477+ "message": "User `3` not found",
478+ "path": ["allUsers", 2]
479+ }
480+ ]
481+ }
482+ """
483+ )
484+
485+ ApolloClient .Builder ()
486+ .serverUrl(mockServer.url())
487+ .cacheManager(cacheManager)
488+ .cachePolicyResponseMapper(cachePolicyResponseMapper)
489+ .build()
490+ .use { apolloClient ->
491+ val networkResult1 = apolloClient.query(AllUsersQuery ())
492+ .fetchPolicy(FetchPolicy .NetworkOnly )
493+ .execute()
494+ assertEquals(
495+ AllUsersQuery .Data (
496+ allUsers = listOf (
497+ AllUsersQuery .AllUser (
498+ __typename = " User" ,
499+ id = " 1" ,
500+ firstName = " John" ,
501+ lastName = " Smith" ,
502+ email = " jsmith@example.com" ,
503+ ),
504+ AllUsersQuery .AllUser (
505+ __typename = " User" ,
506+ id = " 2" ,
507+ firstName = " Jane" ,
508+ lastName = " Doe" ,
509+ email = " jdoe@example.com" ,
510+ ),
511+ null ,
512+ )
513+ ),
514+ networkResult1.data
515+ )
516+ assertErrorsEquals(
517+ listOf (
518+ Error .Builder (" User `3` not found" ).path(listOf (" allUsers" , 2 )).build()
519+ ),
520+ networkResult1.errors
521+ )
522+
523+ val cacheResult1 = apolloClient.query(AllUsersQuery ())
524+ .fetchPolicy(FetchPolicy .CacheOnly )
525+ .execute()
526+ assertEquals(
527+ AllUsersQuery .Data (
528+ allUsers = listOf (
529+ AllUsersQuery .AllUser (
530+ __typename = " User" ,
531+ id = " 1" ,
532+ firstName = " John" ,
533+ lastName = " Smith" ,
534+ email = " jsmith@example.com" ,
535+ ),
536+ AllUsersQuery .AllUser (
537+ __typename = " User" ,
538+ id = " 2" ,
539+ firstName = " Jane" ,
540+ lastName = " Doe" ,
541+ email = " jdoe@example.com" ,
542+ ),
543+ null ,
544+ )
545+ ),
546+ cacheResult1.data,
547+ )
548+ assertErrorsEquals(
549+ listOf (
550+ Error .Builder (" User `3` not found" ).path(listOf (" allUsers" , 2 )).build(),
551+ ),
552+ cacheResult1.errors,
553+ )
554+
555+ val cacheResult2 = apolloClient.query(UsersQuery (listOf (" 1" , " 2" , " 3" , " 4" )))
556+ .fetchPolicy(FetchPolicy .CacheOnly )
557+ .execute()
558+ assertEquals(
559+ UsersQuery .Data (
560+ users = listOf (
561+ UsersQuery .User (
562+ __typename = " User" ,
563+ id = " 1" ,
564+ firstName = " John" ,
565+ lastName = " Smith" ,
566+ email = " jsmith@example.com" ,
567+ ),
568+ UsersQuery .User (
569+ __typename = " User" ,
570+ id = " 2" ,
571+ firstName = " Jane" ,
572+ lastName = " Doe" ,
573+ email = " jdoe@example.com" ,
574+ ),
575+ null ,
576+ null ,
577+ )
578+ ),
579+ cacheResult2.data,
580+ )
581+ assertErrorsEquals(
582+ listOf (
583+ // From AllUsersQuery, we could expect to have the same error "User `3` not found" being returned here for User:3.
584+ // But this error is not stored at `User:3` but inside the list at `allUsers`. There is no way for the cache to know
585+ // how to store it at `User:3`, since the id is not available in the response.
586+ // So instead it results in a cache miss.
587+ Error .Builder (" Object 'User:3' not found in the cache" ).path(listOf (" users" , 2 )).build(),
588+ Error .Builder (" Object 'User:4' not found in the cache" ).path(listOf (" users" , 3 )).build(),
589+ ),
590+ cacheResult2.errors,
591+ )
592+ }
593+ }
594+
436595 @Test
437596 fun cacheMissAndErrorsMemory () = runTest(before = { setUp() }, after = { tearDown() }) {
438597 cacheMissAndErrors(memoryCacheManager)
0 commit comments