hey , I noticed weird behavior. We have a centrali...
# apollo-kotlin
a
hey , I noticed weird behavior. We have a centralized entrypoint into apollo that uses the
watch
method. We have some calls that do not want to use a cache at all, so we use
FetchPolicy.NetworkOnly
. When
watch
executes, we always receive two network calls! we have traced it down to this line in `watch`:
Copy code
copy().fetchPolicyInterceptor(refetchPolicyInterceptor)
        .watch(response?.data) { _, _ ->
this line. since our request context has no cache interceptors, when the call is executed at this point, it always hits the network.
m
Howdy 🤠 . Not sure I'm following. Are you using
.watch()
together with
FetchPolicy.NetworkOnly
?
Copy code
apolloClient.query(MyQuery()).fetchPolicy(FetchPolicy.NetworkOnly).watch()
?
Or does your network query triggers a refetch on the watcher (that would be more likely I guess)
a
Correct. Watch once with network only
m
SO you're doing something like this?
Copy code
apolloClient.query(MyQuery()).fetchPolicy(FetchPolicy.NetworkOnly).watch()
a
The subsequent emission is in the refetchPolicyInterceptor which has no caching execution comtext. So it hits network on second block
m
It's relatively unusual to call
.watch()
for a network only request. Any reason your not just calling
.execute()
?
a
i agree. we have a single entry point like:
Copy code
private fun <D : Query.Data> fullQueryChain(
        query: Query<D>,
        params: ApolloQueryExecutor.Params,
        fetchPolicy: ApolloQueryExecutor.FetchPolicy
    ) = apolloClient.query(query)
     .fetchPolicy(fetchPolicy.toApolloFetchPolicy())
        .httpFetchPolicy(fetchPolicy.toHttpFetchPolicy())
        .httpExpireTimeout(params.httpExpirationInMillis)
        .watch(fetchThrows = true)
which allows different
params
to specify their own fetch policy. some requests may not want cache, while some do. so i figured using same place to determine that. it was unexpected behavior to hit a network call twice on
watch
.
we switched to checking fetch policy and switching to
toFlow()
for the specified request instead of
watch
m
👍 let us know if you want us to investigate the
.watch()
behaviour more. AFAIK, something like
apolloClient.query(MyQuery()).fetchPolicy(FetchPolicy.NetworkOnly).watch()
should only reach to the network once (because the refectPolicy defaults to
CacheOnly
) but maybe there's more to it