We have a use case where we want to watch for chan...
# apollo-kotlin
n
We have a use case where we want to watch for changes to four GraphQL queries, and when any of them change, we want to create and return parameters to use to make another query. In most cases adding GraphQL watch() to these works great. But there are a few cases where the cache doesn't have anything in it yet, and so I guess since there's nothing to watch() the code for making the parameters never fires. I'm stubborn, and want this to work. Any ideas on how to ensure at least one query gets made when initiating a watch()?
I found this worked for me:
Copy code
fun watch(): Flow<List<Account>> =
        apolloClient.query(GetAccountsQuery())
            .fetchPolicy(FetchPolicy.NetworkFirst)
            .refetchPolicy(FetchPolicy.CacheOnly)
            .watch()
            .map { response ->
                response.data!!.accounts.map { it.account }
            }
I had originally set the fetch policy to
CacheOnly
. And obviously this meant that if there wasn't anything in the cache, I'd be waiting until something arrived. With this change to
NetworkFirst
it goes to the network, fetches data into the cache, and my code is much happier.
🎉 2