https://kotlinlang.org logo
#apollo-kotlin
Title
# apollo-kotlin
m

Marco Pierucci

11/14/2023, 10:18 PM
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

mbonnin

11/14/2023, 10:38 PM
I'm AFK right now but IIRC there is a
cacheHeader()
function that should take a
NO_CACHE
value or so
m

Marco Pierucci

11/14/2023, 10:51 PM
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

mbonnin

11/15/2023, 9:17 AM
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

Marco Pierucci

11/15/2023, 9:53 AM
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

Stylianos Gakis

11/15/2023, 10:25 AM
Which version of apollo-kotlin are you using?
m

Marco Pierucci

11/15/2023, 10:26 AM
3.4.0
m

mbonnin

11/15/2023, 10:27 AM
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

Marco Pierucci

11/15/2023, 10:32 AM
Hmm did both things and still the same issue. ( I have also GraphQL plugin installed should I disable it?)
b

bod

11/15/2023, 10:36 AM
you shouldn't disable the GraphQL plugin as it is a dependency of the Apollo plugin
m

Marco Pierucci

11/15/2023, 10:40 AM
Makes sense, not sure why I still get the error then
2 Views