question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Updating an object with a mutation that returns a different type

See original GitHub issue

I have a vote mutation that’s used to upvote different types of documents: posts, comments, etc. Since I want to use a single mutation for all these types I can’t have it return a Post or Comment type, so I created a new VoteResult type. And I’m using dataIdFromObject, so the update happens automatically as soon as the mutation returns.

The optimistic response works properly (I’m setting the __typename manually), but when the mutation returns the real result, something goes wrong and the optimistic response is overturned

Could this be because the mutation doesn’t return an object of the same GraphQL type as the object in the store that needs to be updated? In other words, can a mutation that returns type A be used to update a document of type B in the store?

By the way I can confirm that if both types are the same (i.e. I change vote to return Post object), I’m not seeing any issues.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
rnenjoycommented, Jan 9, 2017

@SachaG some code example when you have a working version of this would be great to see! We should even put it in the documentation since i think many people will run into this.

0reactions
SachaGcommented, Jan 31, 2017

I did read the documentation, but I didn’t realized the interfaces section also applied to unions, so for some reason it didn’t really click until I found an issue thread somewhere explaining the problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Updating data with mutations | Full-Stack Quickstart
The first object in useMutation 's result tuple ( login ) is the mutate function we call to execute the mutation.
Read more >
Update Mutations - GraphQL - Dgraph
Update mutations let you to update existing objects of a particular type. With update mutations, you can filter nodes and set and remove...
Read more >
Mutations and Input Types - GraphQL
Here, the mutations return a Message type, so that the client can get more information about the newly-modified Message in the same request...
Read more >
Can I restrict a mutation to update an object based on the ...
That means: If the user B tries to update the object 2, using the mutation updateObj(2, "new name") , this should be totally...
Read more >
GraphQL mutations: Partial updates implementation - Medium
In this new sandbox we've created a new mutation called updateAuthor to update an author: type Author { id: Int! firstName: String lastName:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found