Is there anyway for apollo to not write cache entr...
# apollo-kotlin
m
Is there anyway for apollo to not write cache entries when enabling localised cache? I have:
Copy code
ApolloClient.Builder()
        ...
        .normalizedCache(SqlNormalizedCacheFactory(APOLLO_CACHE_DB)) 
        .fetchPolicy(FetchPolicy.NetworkOnly)
        ...
And :
Copy code
suspend fun fetchEngineerTimeline(
        date: LocalDate,
    ): EngineerTimeline = apolloClient.query(
        GetEngineerTimelineQuery(date = Optional.Present(date))
    ).fetchPolicy(FetchPolicy.NetworkOnly)
But on the DB inspector I can isee entries for other queries where I have not added any cache policy
m
I'm AFK right now but IIRC there is a
cacheHeader()
function that should take a
NO_CACHE
value or so
m
I guess is
Copy code
object ApolloCacheHeaders {
  /**
   * Records from this request should not be stored in the [NormalizedCache].
   */
  const val DO_NOT_STORE = "do-not-store"
Where can I see some doc on how to use this ones? ( For when you are back ofc)
Ah I found:
Copy code
/**
 * @param doNotStore Whether to store the response in cache.
 *
 * Default: false
 */
fun <T> MutableExecutionOptions<T>.doNotStore(doNotStore: Boolean) = addExecutionContext(
    DoNotStoreContext(doNotStore)
)
👍 1
I wonder if I can set it on the client and then the queries that have a cache policy would override it
m
Back at the keyboard, apologies for the delay. Glad you found
doNotStore()
, this is the way 👍 About making it a default,
ApolloClient.Builder
implements
MutableExecutionOptions
so you can do this:
Copy code
ApolloClient.Builder()
   .serverUrl(..)
   // more stuff
   .doNotStore(true)
   .build()
Not that all the requests have a fetch policy though. Even if you don't set it explicitely, it will default to
CacheFirst
once you configure the cache. If you want to change that behaviour, you can define your own extension function:
Copy code
fun <D: Operation.Data> ApolloCall<D>.myFetchPolicy(fetchPolicy: FetchPolicy): ApolloCall<D> = doNotStore(false).fetchPolicy(fetchPolicy)
🙌 1
m
Oh I see! Perfect, thanks!
Apologies! But if you are still around..
Error:(2, 14) 'FieldUserAppointmentType' tried to use an undeclared directive 'typePolicy'
I get this once I added the declarative id in the extras.grapqhls file. Cant find much online, could it be just a plugin issue?
( As well as Error:(2, 41) Unknown directive "typePolicy")
s
Which version of apollo-kotlin are you using?
m
3.4.0
m
This is a plugin issue, can you try enabling "Apollo" in the JS GraphQL plugin settings?
And if you haven't already, install the Apollo plugin
💯 1
m
Hmm did both things and still the same issue. ( I have also GraphQL plugin installed should I disable it?)
b
you shouldn't disable the GraphQL plugin as it is a dependency of the Apollo plugin
m
Makes sense, not sure why I still get the error then