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

offline node creation : use clientMutationId instead of id #53

Open
Felix-N opened this issue Aug 9, 2020 · 5 comments
Open

offline node creation : use clientMutationId instead of id #53

Felix-N opened this issue Aug 9, 2020 · 5 comments

Comments

@Felix-N
Copy link

Felix-N commented Aug 9, 2020

Hi Morrys,

this is a bit of a question:

If I understood the documentation correct the client creating a new node (offline mutation) needs to provide the node id.

Is it possible to use the relay clientMutationId and let the backend decide on the final id?

@Felix-N Felix-N changed the title use clientMutationId offline node creation : use clientMutationId instead of id Aug 9, 2020
@morrys
Copy link
Owner

morrys commented Aug 10, 2020

I recommend that you use the onPublish function to customize the data that is entered in the offline mutation queue, including the id

@Felix-N
Copy link
Author

Felix-N commented Aug 11, 2020

hi morrys,

and thanks again for the help.

i guess i'm a bit unsure about this part in the react-relay-offline documentation:

When a new node is created by mutation the id must be generated in the browser to use it in the optimistic response

what i've done at the moment is for a mutation creating a new node:
create an id on the offline client which is used for all further offline mutations. however when a network request goes through the server returns an id.
my expectation is, that relay will roll back the optimisticupdate (including the node with the 'wrong' id) and my updater creates the correct new node with the server id.

is this what the documentation means, or am i missing something?

@morrys
Copy link
Owner

morrys commented Aug 11, 2020

Perhaps the sample project clarifies the sentence better:
During the creation mutation the id is generated in the browser and the server will use that value to identify the node and return it in the response

Offline mutations do not roll back optimisticupdate because optimistic functions cannot be stored if the application is closed.

Furthermore, the nodes created could be shared externally by the application and therefore refer to incorrect identifiers.

If it is not possible to generate IDs in the browser at the moment I recommend preventing offline mutations that create new nodes.

@Felix-N
Copy link
Author

Felix-N commented Aug 11, 2020

if an optimisticupdate in an offline scenario is not rolled back, how is data integrity ensured once the network request goes through?
i believe this is mainly important for anything which is not a primitive (eg. connections).

in the current case i have:

i am adding a new record (a), add this record a linkedRecord to (b) and add to the connections of record (c) a new edge to record (b).

for record (a) i can not generate a id on the client, but the server will identify the correct node for it and return that one (basically the combination of field values are a unique identifier in this case - but the id scheme on the backend would not allow this as id).

just as a thought: wouldn't it be possible for the offline functionality to persist results of network queries and persist / replay mutations when the app is started offline to restore the store state? for that the library would need a list of mutation functions

@morrys
Copy link
Owner

morrys commented Aug 12, 2020

If it is not possible to guarantee the state of the store in the optimistic responses, it is possible to manage these conflicts within the callback functions (onComplete, onDiscard, finish) and use the APIs made available to the environment, see local-state-management or globally-invalidating-the-relay-store

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