Skip to content

Commit 56194fa

Browse files
committed
Rename allowCachedErrors -> serverErrorsAsCacheMisses and allowCachePartialResults -> throwOnCacheMiss
1 parent cf27450 commit 56194fa

12 files changed

Lines changed: 86 additions & 120 deletions

File tree

normalized-cache/api/normalized-cache.api

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -712,11 +712,11 @@ public final class com/apollographql/cache/normalized/memory/MemoryCacheFactory
712712
public fun create ()Lcom/apollographql/cache/normalized/memory/MemoryCache;
713713
}
714714

715-
public final class com/apollographql/cache/normalized/options/FetchAllowCachedErrorsKt {
716-
public static final fun allowCachedErrors (Lcom/apollographql/apollo/api/MutableExecutionOptions;Z)Ljava/lang/Object;
715+
public final class com/apollographql/cache/normalized/options/ServerErrorsAsCacheMissesKt {
716+
public static final fun serverErrorsAsCacheMisses (Lcom/apollographql/apollo/api/MutableExecutionOptions;Z)Ljava/lang/Object;
717717
}
718718

719-
public final class com/apollographql/cache/normalized/options/FetchAllowCachedPartialResultsKt {
720-
public static final fun allowCachedPartialResults (Lcom/apollographql/apollo/api/MutableExecutionOptions;Z)Ljava/lang/Object;
719+
public final class com/apollographql/cache/normalized/options/ThrowOnCacheMissKt {
720+
public static final fun throwOnCacheMiss (Lcom/apollographql/apollo/api/MutableExecutionOptions;Z)Ljava/lang/Object;
721721
}
722722

normalized-cache/api/normalized-cache.klib.api

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,8 @@ final fun <#A: com.apollographql.apollo.api/Operation.Data> (com.apollographql.a
627627
final fun <#A: com.apollographql.apollo.api/Operation.Data> (com.apollographql.apollo.api/ApolloResponse<#A>).com.apollographql.cache.normalized/errorsAsException(): com.apollographql.apollo.api/ApolloResponse<#A> // com.apollographql.cache.normalized/errorsAsException|errorsAsException@com.apollographql.apollo.api.ApolloResponse<0:0>(){0§<com.apollographql.apollo.api.Operation.Data>}[0]
628628
final fun <#A: com.apollographql.apollo.api/Query.Data> (com.apollographql.apollo/ApolloCall<#A>).com.apollographql.cache.normalized/watch(#A?): kotlinx.coroutines.flow/Flow<com.apollographql.apollo.api/ApolloResponse<#A>> // com.apollographql.cache.normalized/watch|watch@com.apollographql.apollo.ApolloCall<0:0>(0:0?){0§<com.apollographql.apollo.api.Query.Data>}[0]
629629
final fun <#A: com.apollographql.apollo.api/Query.Data> (com.apollographql.apollo/ApolloCall<#A>).com.apollographql.cache.normalized/watch(): kotlinx.coroutines.flow/Flow<com.apollographql.apollo.api/ApolloResponse<#A>> // com.apollographql.cache.normalized/watch|watch@com.apollographql.apollo.ApolloCall<0:0>(){0§<com.apollographql.apollo.api.Query.Data>}[0]
630-
final fun <#A: kotlin/Any?> (com.apollographql.apollo.api/MutableExecutionOptions<#A>).com.apollographql.cache.normalized.options/allowCachedErrors(kotlin/Boolean): #A // com.apollographql.cache.normalized.options/allowCachedErrors|allowCachedErrors@com.apollographql.apollo.api.MutableExecutionOptions<0:0>(kotlin.Boolean){0§<kotlin.Any?>}[0]
631-
final fun <#A: kotlin/Any?> (com.apollographql.apollo.api/MutableExecutionOptions<#A>).com.apollographql.cache.normalized.options/allowCachedPartialResults(kotlin/Boolean): #A // com.apollographql.cache.normalized.options/allowCachedPartialResults|allowCachedPartialResults@com.apollographql.apollo.api.MutableExecutionOptions<0:0>(kotlin.Boolean){0§<kotlin.Any?>}[0]
630+
final fun <#A: kotlin/Any?> (com.apollographql.apollo.api/MutableExecutionOptions<#A>).com.apollographql.cache.normalized.options/serverErrorsAsCacheMisses(kotlin/Boolean): #A // com.apollographql.cache.normalized.options/serverErrorsAsCacheMisses|serverErrorsAsCacheMisses@com.apollographql.apollo.api.MutableExecutionOptions<0:0>(kotlin.Boolean){0§<kotlin.Any?>}[0]
631+
final fun <#A: kotlin/Any?> (com.apollographql.apollo.api/MutableExecutionOptions<#A>).com.apollographql.cache.normalized.options/throwOnCacheMiss(kotlin/Boolean): #A // com.apollographql.cache.normalized.options/throwOnCacheMiss|throwOnCacheMiss@com.apollographql.apollo.api.MutableExecutionOptions<0:0>(kotlin.Boolean){0§<kotlin.Any?>}[0]
632632
final fun <#A: kotlin/Any?> (com.apollographql.apollo.api/MutableExecutionOptions<#A>).com.apollographql.cache.normalized/addCacheHeader(kotlin/String, kotlin/String): #A // com.apollographql.cache.normalized/addCacheHeader|addCacheHeader@com.apollographql.apollo.api.MutableExecutionOptions<0:0>(kotlin.String;kotlin.String){0§<kotlin.Any?>}[0]
633633
final fun <#A: kotlin/Any?> (com.apollographql.apollo.api/MutableExecutionOptions<#A>).com.apollographql.cache.normalized/cacheHeaders(com.apollographql.cache.normalized.api/CacheHeaders): #A // com.apollographql.cache.normalized/cacheHeaders|cacheHeaders@com.apollographql.apollo.api.MutableExecutionOptions<0:0>(com.apollographql.cache.normalized.api.CacheHeaders){0§<kotlin.Any?>}[0]
634634
final fun <#A: kotlin/Any?> (com.apollographql.apollo.api/MutableExecutionOptions<#A>).com.apollographql.cache.normalized/clock(kotlin/Function0<kotlin/Long>): #A // com.apollographql.cache.normalized/clock|clock@com.apollographql.apollo.api.MutableExecutionOptions<0:0>(kotlin.Function0<kotlin.Long>){0§<kotlin.Any?>}[0]

normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicy.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ enum class FetchPolicy {
1010

1111
/**
1212
* Emit the response from the cache only.
13-
*
14-
* Equivalent of executing with `onlyIfCached(true)`.
1513
*/
1614
CacheOnly,
1715

@@ -22,16 +20,12 @@ enum class FetchPolicy {
2220

2321
/**
2422
* Emit the response(s) from the network only.
25-
*
26-
* Equivalent of executing with `noCache(true)`.
2723
*/
2824
NetworkOnly,
2925

3026
/**
3127
* Emit the response from the cache first, and then emit the response(s) from the network.
32-
*
33-
* Equivalent of executing with `onlyIfCached(true)` and then with `noCache(true)`.
3428
*/
35-
@Deprecated("This is equivalent of executing with onlyIfCached(true) and then with noCache(true)")
29+
@Deprecated("This is equivalent of executing with CacheOnly and then with NetworkOnly")
3630
CacheAndNetwork,
3731
}

normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicyInterceptors.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import com.apollographql.apollo.exception.CacheMissException
1313
import com.apollographql.apollo.exception.DefaultApolloException
1414
import com.apollographql.apollo.interceptor.ApolloInterceptor
1515
import com.apollographql.apollo.interceptor.ApolloInterceptorChain
16-
import com.apollographql.cache.normalized.options.allowCachedErrors
17-
import com.apollographql.cache.normalized.options.allowCachedPartialResults
1816
import com.apollographql.cache.normalized.options.noCache
1917
import com.apollographql.cache.normalized.options.onlyIfCached
18+
import com.apollographql.cache.normalized.options.serverErrorsAsCacheMisses
19+
import com.apollographql.cache.normalized.options.throwOnCacheMiss
2020
import kotlinx.coroutines.flow.Flow
2121
import kotlinx.coroutines.flow.emitAll
2222
import kotlinx.coroutines.flow.flow
@@ -47,7 +47,7 @@ val DefaultFetchPolicyInterceptor = object : ApolloInterceptor {
4747
.fetchFromCache(true)
4848
.build()
4949
).single()
50-
.errorsAsException(allowCachedPartialResults = request.allowCachedPartialResults, allowCachedErrors = request.allowCachedErrors)
50+
.errorsAsException(throwOnCacheMiss = request.throwOnCacheMiss, serverErrorsAsCacheMisses = request.serverErrorsAsCacheMisses)
5151
emit(cacheResponse.newBuilder().isLast(request.onlyIfCached || cacheResponse.exception == null)
5252
.build()
5353
)
@@ -99,7 +99,7 @@ val NetworkFirstInterceptor = object : ApolloInterceptor {
9999
.fetchFromCache(true)
100100
.build()
101101
).single()
102-
.errorsAsException(allowCachedPartialResults = request.allowCachedPartialResults, allowCachedErrors = request.allowCachedErrors)
102+
.errorsAsException(throwOnCacheMiss = request.throwOnCacheMiss, serverErrorsAsCacheMisses = request.serverErrorsAsCacheMisses)
103103
emit(cacheResponse)
104104
}
105105
}
@@ -118,7 +118,7 @@ val CacheAndNetworkInterceptor = object : ApolloInterceptor {
118118
.fetchFromCache(true)
119119
.build()
120120
).single()
121-
.errorsAsException(allowCachedPartialResults = request.allowCachedPartialResults, allowCachedErrors = request.allowCachedErrors)
121+
.errorsAsException(throwOnCacheMiss = request.throwOnCacheMiss, serverErrorsAsCacheMisses = request.serverErrorsAsCacheMisses)
122122

123123
emit(cacheResponse.newBuilder().isLast(false).build())
124124

@@ -149,21 +149,21 @@ fun <D : Operation.Data> ApolloResponse<D>.errorsAsException(): ApolloResponse<D
149149
}
150150

151151
private fun <D : Operation.Data> ApolloResponse<D>.errorsAsException(
152-
allowCachedPartialResults: Boolean,
153-
allowCachedErrors: Boolean,
152+
throwOnCacheMiss: Boolean,
153+
serverErrorsAsCacheMisses: Boolean,
154154
): ApolloResponse<D> {
155-
return if (allowCachedPartialResults && allowCachedErrors) {
155+
return if (!throwOnCacheMiss && !serverErrorsAsCacheMisses) {
156156
this
157157
} else {
158-
val cacheMissException = if (allowCachedPartialResults) {
158+
val cacheMissException = if (!throwOnCacheMiss) {
159159
null
160160
} else {
161161
errors.orEmpty().mapNotNull { it.cacheMissException }.reduceOrNull { acc, e ->
162162
acc.addSuppressed(e)
163163
acc
164164
}
165165
}
166-
val cachedErrorException = if (allowCachedErrors) {
166+
val cachedErrorException = if (!serverErrorsAsCacheMisses) {
167167
null
168168
} else {
169169
errors.orEmpty().mapNotNull { if (it.cacheMissException != null) null else ApolloGraphQLException(it) }.reduceOrNull { acc, e ->

normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/options/fetchAllowCachedErrors.kt

Lines changed: 0 additions & 25 deletions
This file was deleted.

normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/options/fetchAllowCachedPartialResults.kt

Lines changed: 0 additions & 25 deletions
This file was deleted.

normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/options/refetchAllowCachedErrors.kt

Lines changed: 0 additions & 14 deletions
This file was deleted.

normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/options/refetchAllowCachedPartialResults.kt

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.apollographql.cache.normalized.options
2+
3+
import com.apollographql.apollo.api.ExecutionContext
4+
import com.apollographql.apollo.api.ExecutionOptions
5+
import com.apollographql.apollo.api.MutableExecutionOptions
6+
7+
internal class ServerErrorsAsCacheMisses(val value: Boolean) : ExecutionContext.Element {
8+
override val key: ExecutionContext.Key<*>
9+
get() = Key
10+
11+
companion object Key : ExecutionContext.Key<ServerErrorsAsCacheMisses>
12+
}
13+
14+
/**
15+
* Sets whether GraphQL errors in the cache should be treated as cache misses.
16+
*
17+
* When true, if any field is an Error in the cache, the returned response will have a null data and a non-null exception of type
18+
* [com.apollographql.apollo.exception.ApolloGraphQLException].
19+
*
20+
* Default: true
21+
*/
22+
internal val ExecutionOptions.serverErrorsAsCacheMisses: Boolean
23+
get() = executionContext[ServerErrorsAsCacheMisses]?.value ?: true
24+
25+
fun <T> MutableExecutionOptions<T>.serverErrorsAsCacheMisses(serverErrorsAsCacheMisses: Boolean): T =
26+
addExecutionContext(ServerErrorsAsCacheMisses(serverErrorsAsCacheMisses))
27+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.apollographql.cache.normalized.options
2+
3+
import com.apollographql.apollo.api.ExecutionContext
4+
import com.apollographql.apollo.api.ExecutionOptions
5+
import com.apollographql.apollo.api.MutableExecutionOptions
6+
7+
internal class ThrowOnCacheMissContext(val value: Boolean) : ExecutionContext.Element {
8+
override val key: ExecutionContext.Key<*>
9+
get() = Key
10+
11+
companion object Key : ExecutionContext.Key<ThrowOnCacheMissContext>
12+
}
13+
14+
internal val ExecutionOptions.throwOnCacheMiss: Boolean
15+
get() = executionContext[ThrowOnCacheMissContext]?.value ?: true
16+
17+
/**
18+
* Sets whether missing fields from the cache should result in an exception.
19+
*
20+
* When true, if any field is missing in the cache, the returned response will have a null data and a non-null exception of type
21+
* [com.apollographql.apollo.exception.CacheMissException].
22+
*
23+
* Set this to false to allow partial responses from the cache, where _some_ or _all_ of the fields may be missing.
24+
*
25+
* Default: true
26+
*/
27+
fun <T> MutableExecutionOptions<T>.throwOnCacheMiss(throwOnCacheMiss: Boolean): T =
28+
addExecutionContext(ThrowOnCacheMissContext(throwOnCacheMiss))
29+

0 commit comments

Comments
 (0)