apollographql / apollo-kotlin

:rocket:  A strongly-typed, caching GraphQL client for the JVM, Android, and Kotlin multiplatform.
https://www.apollographql.com/docs/kotlin
MIT License
3.76k stars 653 forks source link

Can I make mutation while no internet connection? #5548

Closed enggazzar closed 9 months ago

enggazzar commented 10 months ago

Question

I need to add items to the cart while there is no internet connection and when the mobile has internet mutation executed

martinbonnin commented 10 months ago

There is no such thing at the moment. You can mutate your cache locally but if you need that mutation to reach your server, you need to save it using something like WorkManager

martinbonnin commented 9 months ago

@enggazzar anything else we can help with here?

enggazzar commented 9 months ago

Thanks but at least I can execute mutation offline the only example is https://www.apollographql.com/docs/kotlin/caching/store/ ?

martinbonnin commented 9 months ago

Yes, that's correct, the ApolloStore API allows you to write data to the store offline and you can synchronize that later.

If you're looking at a more concrete example, you can take a look at our benchmarks. They use writeOperation here

enggazzar commented 9 months ago

writeOperation is this mean execute mutation offline the task I have is operation with cart like add to cart remove from cart is this possible to work offline?

martinbonnin commented 9 months ago

writeOperation updates the contents of your local, offline, cache with the data that you are passing as argument.

Most of the time, you don't need a mutation to write your local cache: create a Query.data instance programmatically and pass it to writeOperation. In your example, something like writeOperation(cartContentsQuery, CartsContentQuery(items = listOf(newItemHere)), customScalarAdapters).

There's no silver bullet though and if you want that cart content to be synchronized to the backend, you'll have to execute the mutation later on.

martinbonnin commented 9 months ago

Hi 👋 I'll close this one in favour of https://github.com/apollographql/apollo-kotlin/issues/5618. That's something that would be nice to have although it's probably not for the short term.

enggazzar commented 9 months ago

Sure Thanks for your help

On Mon, Feb 12, 2024 at 11:21 AM Martin Bonnin @.***> wrote:

Hi 👋 I'll close this one in favour of #5618 https://github.com/apollographql/apollo-kotlin/issues/5618. That's something that would be nice to have although it's probably not for the short term.

— Reply to this email directly, view it on GitHub https://github.com/apollographql/apollo-kotlin/issues/5548#issuecomment-1938296889, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZH7W2G6NNLPOPFNHOM2IDYTHNJRAVCNFSM6AAAAABCDHSL56VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZYGI4TMOBYHE . You are receiving this because you were mentioned.Message ID: @.***>