Skip to content

Add a test of using partial results with @catch(to: RESULT) #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/partial-results/src/commonMain/graphql/extra.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ extend type EmployeeInfo @semanticNonNullField(name: "salary")
@semanticNonNullField(name: "department")

extend type EmployeeInfo @cacheControlField(name: "salary", maxAge: 0)

extend type DepartmentInfo @cacheControlField(name: "name", maxAge: 0)
11 changes: 11 additions & 0 deletions tests/partial-results/src/commonMain/graphql/operation.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,14 @@ query MeWithEmployeeInfoQuery {
}
}
}

query MeWithDepartmentInfoQuery {
me {
firstName
lastName
departmentInfo {
id
name @catch(to: RESULT)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type User {
category: Category!
moreInfo: Json!
employeeInfo: EmployeeInfo
departmentInfo: DepartmentInfo!
}

type Project {
Expand All @@ -35,5 +36,10 @@ type EmployeeInfo {
department: String
}

type DepartmentInfo {
id: ID!
name: String
}

scalar Category
scalar Json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.apollographql.apollo.api.ApolloRequest
import com.apollographql.apollo.api.ApolloResponse
import com.apollographql.apollo.api.Error
import com.apollographql.apollo.api.Operation
import com.apollographql.apollo.api.graphQLErrorOrNull
import com.apollographql.apollo.interceptor.ApolloInterceptor
import com.apollographql.apollo.interceptor.ApolloInterceptorChain
import com.apollographql.apollo.testing.internal.runTest
Expand Down Expand Up @@ -824,6 +825,47 @@ class CachePartialResultTest {
)
}
}

@Test
fun cacheControlWithCatchToResult() = runTest(before = { setUp() }, after = { tearDown() }) {
mockServer.enqueueString(
// language=JSON
"""
{
"data": {
"me": {
"__typename": "User",
"id": "1",
"firstName": "John",
"lastName": "Smith",
"departmentInfo": {
"id": "1",
"name": "Engineering"
}
}
}
}
"""
)
ApolloClient.Builder()
.serverUrl(mockServer.url())
.normalizedCache(MemoryCacheFactory(), cacheResolver = CacheControlCacheResolver(SchemaCoordinatesMaxAgeProvider(Cache.maxAges, Duration.INFINITE)))
.storeReceivedDate(true)
.build()
.use { apolloClient ->
apolloClient.query(MeWithDepartmentInfoQuery())
.fetchPolicy(FetchPolicy.NetworkOnly)
.execute()
val cacheMissResult = apolloClient.query(MeWithDepartmentInfoQuery())
.fetchPolicyInterceptor(PartialCacheOnlyInterceptor)
.execute()
assertErrorsEquals(
Error.Builder("Field 'name' on object '${CacheKey("User:1").append("departmentInfo").keyToString()}' is stale in the cache")
.path(listOf("me", "departmentInfo", "name")).build(),
cacheMissResult.data?.me?.departmentInfo?.name?.graphQLErrorOrNull()
)
}
}
}

val PartialCacheOnlyInterceptor = object : ApolloInterceptor {
Expand Down