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.

[Error] Mutation : the application attempted to write an object with no provided id but the store already contains an id

See original GitHub issue

Hello guys,

Sorry it might be a trivial problem but I’m stuck, in my Apollo testing and learning process, by an error my mutation generates and I’m coming down to you to help me.

Intended outcome:

Mutating by adding a new friend to a user and updating the state with the newly added friend.

import { graphql, compose } from 'react-apollo';
import { connect } from 'react-redux';
import update from 'immutability-helper';

...

const addFriend = graphql(ADD_FRIEND_MUTATION, {
  props: ({ mutate }) => ({
    addFriend: username =>
      mutate({
        variables: { username },
        updateQueries: {
          user: (previousResult, { mutationResult }) => {
            const newFriend = mutationResult.data.addFriend;
            return update(previousResult, {
              user: {
                friends: {
                  $push: [newFriend],
                },
              },
            });
          },
        },
      }),
  }),
});

...

export default compose(connect(mapStateToProps, mapDispatchToProps), userQuery, addFriend)(People);

Actual outcome:

The mutation works db-wise, but the client does not want to update the app/redux state.

Weirdly the updateQueries is called twice. The first time with the “right” previousResult object, the second time with a much smaller object without id. Then the error pops. After refresh of the app, the friend is well added.

Here is the error : the application attempted to write an object with no provided id but the store already contains an id of User:18 for this object.

screen shot 2017-05-24 at 23 09 40

How to reproduce the issue:

Directly from my little test project repo : https://github.com/xavierlefevre/chat

Here is the react container with the front mutation : https://github.com/xavierlefevre/chat/blob/master/client/src/pages/people/people.container.js

In order to install the project :

  • Back
    • cd chat/server
    • yarn install
    • yarn start
  • Front
    • cd chat/client
    • yarn install
    • react-native run-ios
    • login with a user logged by the express server in your terminal
    • go to the tab “people”
    • click on the top right “plus”
    • add a user, once again output by the express server in your terminal
    • witness the error

Hope my issue is filled correctly, and thanks a bunch for the help.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

24reactions
helfercommented, Jun 2, 2017

Ah, I think I see the problem now. Your FRIENDS_QUERY doesn’t ask for the user’s id. That way Apollo Client cannot calculate a unique id for the object, and you get the error message that you saw. I’m glad you figured out a solution on your own though! 🙂

0reactions
xavierlefevrecommented, May 30, 2017

@helfer

I fixed the issue. Here is my commit : https://github.com/xavierlefevre/chat/commit/11c84529c5fc16168470109f07ab83cbe15ca181

The thing I understood :

  • I query USER_QUERY when arriving on the app, retrieving many information for this user as id, username, friends, groups etc.
  • In the page of this issue, I then make a FRIENDS_QUERY that only retrieves the friends of this same user
  • Once the mutation to add a friend to the user completes, I don’t know why, but it triggers twice, once with a full user in previousResult (id, username, etc.), and a second time with a user only matching the FRIENDS_QUERY (only friends).

So I decided to remove FRIENDS_QUERY and query/retrieve (again) USER_QUERY for this page as well, in order to make sure it has the same object shape. But I am not sure why yet I needed to do that.

If things are clear for you and you would have any hint for me, it would be amazing.

Thanks anyhow !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Store error: the application attempted to write an object with no ...
The fix is quite easy, just add id to the query without id. So the question is: should every query has defined id...
Read more >
Apollo client is giving me an error of 'store already contains an ...
Possible Unhandled Promise Rejection (id: 0): Network error: Store error: the application attempted to write an object with no provided id but the...
Read more >
The store already contains an id of - Questions - Prisma 1 Forum
Store error : the application attempted to write an object with no provided id but the store already contains an id of …...
Read more >
Require id field for all GraphQL entities in all queries ... - GitLab
Feature A's query now fails, with something like: Store error: the application attempted to write an object with no provided id but the...
Read more >
Mutations in Apollo Client - Apollo GraphQL Docs
In most cases, a mutation response should include any object(s) the mutation modified. This enables Apollo Client to normalize those objects and cache...
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