Search code examples
kotlingraphqlapolloapollo-android

GraphQL [Apollo-Android]: two `.watcher().enqueueAndWatch` creating infinite refresh loop


I'm requesting a list of 'posts' from my server using enqueueAndWatch and it is causing an infinite refresh loop.

The two queries are below:

query GetOrganizationPosts {
    user {
        organization {
            posts {
                ...post
            }
        }
    }
}

query GetUserPosts {
    user {
        posts {
            ...post
        }
    }
}

fragment post on Post {
    id
    title
    messsage
}

The view model is initialized and the infinite loop occurs when I call refreshPosts() from the posts fragment:

init {
    viewModelScope.launch {
            networkClient.apolloClient.
                query(GetOrganizationPostsQuery())
                .watcher().enqueueAndWatch(callback)
            networkClient.apolloClient
                .query(GetUserPostsQuery())
                .watcher().enqueueAndWatch(callback2)
    }
}
fun refreshPosts() {
        networkClient.apolloClient
            .query(GetOrganizationPostsQuery())
            .enqueue(null)
        networkClient.apolloClient
            .query(GetUserPostsQuery())
            .enqueue(null)
}

Thanks in advance!


Solution

  • Workaround as described in Apollo-Android pull request: https://github.com/apollographql/apollo-android/issues/2226

    Add .responseFetcher(ApolloResponseFetchers.CACHE_ONLY) to any enqueueAndWatch calls. This makes sure watchers don't perform network requests when the cache is updated.