Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache watching seems broken when writing ApolloStore operations #34

Open
cvb941 opened this issue Aug 27, 2024 · 3 comments
Open

Cache watching seems broken when writing ApolloStore operations #34

cvb941 opened this issue Aug 27, 2024 · 3 comments

Comments

@cvb941
Copy link

cvb941 commented Aug 27, 2024

After some painful debugging, I noticed that the incubating cache does not emit data on watched queries when writing data using writeOperation, even when disabling all paging support. The same code seems to work when switched back to the production Apollo cache dependency.

I put up a quick sample here:
https://github.com/cvb941/apollo-kotlin-normalized-cache-incubating/tree/main/samples/cache-watching-test

I added an update button that should increment all stars by one. It does it successfully, but the data is not refreshed on the screen (you can see the updated data when you restart the app after it fetches the cache).

val data = apolloClient.apolloStore.readOperation(RepositoryListQuery())
val updatedEdges = data.organization?.repositories?.edges?.map { edge ->
    edge?.let { e ->
        e.copy(
            node = e.node?.copy(
                repositoryFields = e.node.repositoryFields.copy(
                    stargazers = e.node.repositoryFields.stargazers.copy(
                        totalCount = e.node.repositoryFields.stargazers.totalCount + 1
                    )
                )
            )
        )
    }
}
val updatedData =
    data.copy(organization = data.organization?.copy(repositories = data.organization.repositories.copy(edges = updatedEdges)))
apolloClient.apolloStore.writeOperation(RepositoryListQuery(), updatedData)
image
@martinbonnin
Copy link
Contributor

Many thanks for the reproducer 🙏 . This has indeed been a bit of an API challenge (see apollographql/apollo-kotlin#5971) as writing the store is usually a synchronous operation but publishing needs to suspend to wait for possible (slow) consumers. To keep concerns separated, it's 2 APIs in the incubating cache:

  • writeOperation() (non-suspend)
  • publish() (suspend)

In your example, you can do something like below:

val keys = apolloClient.apolloStore.writeOperation(RepositoryListQuery(), updatedData)
apolloClient.apolloStore.publish(keys)

@cvb941
Copy link
Author

cvb941 commented Aug 29, 2024

Thank you for your response, it works now. Is the change mentioned somewhere right now? I must have missed it.

@martinbonnin
Copy link
Contributor

This repository is pretty unstable, we do not keep a very strict changelog at the moment because things are still changing a lot, sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants