Skip to content

Commit 4f2189d

Browse files
committed
Add a test where items are fetched from 2 different lists. The error in 1st list cannot be seen from the 2nd one in the cache.
1 parent 2db964b commit 4f2189d

3 files changed

Lines changed: 169 additions & 0 deletions

File tree

tests/cache-policy-response-mapper/src/commonMain/graphql/operation.graphql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ query UsersQuery($ids: [ID!]!) {
3030
}
3131
}
3232

33+
query AllUsersQuery {
34+
allUsers {
35+
id
36+
firstName
37+
lastName
38+
email
39+
}
40+
}
41+
3342
query UserByCategoryQuery($category: Category!) {
3443
user(category: $category) {
3544
id

tests/cache-policy-response-mapper/src/commonMain/graphql/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
type Query {
22
me: User!
33
users(ids: [ID!]!): [User]!
4+
allUsers: [User]!
45
project(id: ID! = "1"): Project
56
user(category: Category!): User
67
someInt: Int

tests/cache-policy-response-mapper/src/commonTest/kotlin/test/CachePolicyResponseMapperTest.kt

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,165 @@ class StoreErrorsTest {
424424
}
425425
}
426426

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

0 commit comments

Comments
 (0)