Apollo mutation disorganization and race conditions

(this is a continuation of https://github.com/apollographql/apollo-client/issues/1886 )

I am trying to create text input that will update the value by user type.

First try

At first I tried to use optimisticResponseto update the local cache by user type. This works, except that it triggers a mutation every time a key is pressed. In addition to flooding the network with requests, there is also the problem of network inconsistency. It is possible that the first mutation will be the first, and the first mutation the last. This causes the server to end with an outdated value. Here is an example of this race condition:

type: a
mutate request: a
type: b
mutate request: ab
arrives on server: ab
arrives on server: a

Now the server has written "a" in the graph, which is incorrect.

Adding debounce

, debounce . , , . - , , .

, React, , ( @jbaxleyiii github). ( -).

, . . graphql .

debounce , ( debounce), , , . , , , , . , ( ). :

type: a
send mutate request: a
type: b
queues mutate request: ab     <<  wait to send this until "a" comes back
type: c
replaces queued request: abc  << discard the queued request for "ab", it old now
response from server: a
send mutate request: abc      << send the queued mutation and clear the queue
response from server: abc

, ( ...)

. optimisticResponse , . , , optimisicRespose. . , optimisticResponse "abc" "send mutate request: abc".

, , , .

, withApollo, , writeQuery. optimisticResponse. , - . :

action                   | cache
-------------------------+------------
type: a                  | a
mutate request: a        | a
type: b                  | ab
queues request: ab       | ab
response from server: a  | a  << oh no!
mutate request: ab       | a  << we're not using optimisticResponse anymore
... network time ...     | a
response from server: ab | ab

, client.writeQuery optimisticResponse , , .

update, .

Apollo, , , - . apollo?

+4
1

, , - , .

, , , , . , , , , .

, , Redux , ( debounce).

, , , ( ++ ).

0

Source: https://habr.com/ru/post/1683711/


All Articles