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

[graphcache] mutation update executed twice #653

Closed
zenios opened this issue Mar 21, 2020 · 8 comments · Fixed by #654
Closed

[graphcache] mutation update executed twice #653

zenios opened this issue Mar 21, 2020 · 8 comments · Fixed by #654
Labels
bug 🐛 Oh no! A bug or unintented behaviour.

Comments

@zenios
Copy link

zenios commented Mar 21, 2020

Hi All

I have a mutation update configured for graphcache that for some weired reason it is executed twice for a single mutation.

One execution with empty result
Second execution with filled result

I did some debugging and i can see that the first execution is done via writeOptimistic before the mutation is send to the server.

And the second is the correct execution done after the results arrives.

I dont have any optimistic configurations in place

@zenios zenios added the bug 🐛 Oh no! A bug or unintented behaviour. label Mar 21, 2020
@JoviDeCroock JoviDeCroock added needs more info ✋ A question or report that needs more info to be addressable and removed bug 🐛 Oh no! A bug or unintented behaviour. labels Mar 21, 2020
@zenios
Copy link
Author

zenios commented Mar 21, 2020

The mutation is

mutation CreateFriendRequestMutation($withUserId: ID!) {
  createFriendRequest(withUser: $withUserId)
}

I dont know what other information i can provide

@JoviDeCroock
Copy link
Collaborator

A reproduction is the source of all information, it's very hard for us to make an assumption as to what variables you are calling with, ... For instance why is there no result is it an invalid call or the same call as the second?

@zenios
Copy link
Author

zenios commented Mar 21, 2020

The mutation accepts a string and returns a boolean.

And the update is called twice one with result as {} and the second with result {createFriendRequest: true}

@JoviDeCroock
Copy link
Collaborator

JoviDeCroock commented Mar 21, 2020

So what I can derive from your current explanation is the following:

You have:

cacheExchange({
  updates: { Mutation: { createFriendRequest: () => smth  } },
  optimistic: { createFriendRequest: () => ({})},
})

And you are asking why createFriendRequest is called twice, right? The last argument of your updater function is ctx which has a property called optimistic this can help your determine if this is called because of an optimistic update or just your regular response.

This because even if it's an optimistic update you can have special logic, like adding an item to a list, we allow the user to utilise the updater function for the response they construct in optimistic and the one they get from their server-response.

@zenios
Copy link
Author

zenios commented Mar 21, 2020

Here is a sandbox

https://codesandbox.io/s/focused-night-ztnmm

I have a console log inside update that you can see it logs twice

The only difference from my case is in sandbox case both result and args are null on first execution
and args null on second execution

@JoviDeCroock
Copy link
Collaborator

Thanks for the reproduction @zenios that's appreciated, it had to do with the fact that the mutation had no selectionset, there's a fix PR up!

@zenios
Copy link
Author

zenios commented Mar 21, 2020

You are welcome

@JoviDeCroock JoviDeCroock added bug 🐛 Oh no! A bug or unintented behaviour. and removed needs more info ✋ A question or report that needs more info to be addressable labels Mar 21, 2020
@zenios zenios changed the title GraphCache mutation update executed twice [graphcache] mutation update executed twice Mar 21, 2020
@kitten
Copy link
Member

kitten commented Mar 22, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Oh no! A bug or unintented behaviour.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants