简体   繁体   中英

Apollo Client how to send locally cached mutations to the server

I'm trying to send all my locally cached mutations to the server on user request. The idea is that there's a "Save Changes" button in the UI which allows the user to update the remote db with all the locally aplied changes.

Example: I have a users object in the cache:

"User:1": {
   name: "Tom",
   enabled: true
},
"User:2": {
   name: "John",
   enabled: true
}

Now, let's say the UI user disables both users by setting enabled: false and presses "Save changes". At this point, I'd like all the local changes to be send; both objects should be send to remote and persisted there. I tried several approaches but they all lead to rather hacky code. Is there any canonic way of doing it?

Not sure if that's relevant, but I'm using "Hot Chocolate" graphql server for C#.

There is nothing like "locally cached mutations".

By writing to the cache you just mutate results of other operations (queries or mutations). There is nothing like "local queue", "local copy to be updated on sync" etc. - it is just a cache. You can write to it to save/avoid unnecessary queries after mutations (especially for related types).

To create "Save Changes" feature just read data from cache and use it in mutation - probably special one, accepting arrays as arguments/input types. If no such mutation available/permited you can try to connect many (aliased) mutations into one request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM