[Error] Mutation : the application attempted to write an object with no provided id but the store already contains an id
See original GitHub issueHello 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.
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:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
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! 🙂
@helfer
I fixed the issue. Here is my commit : https://github.com/xavierlefevre/chat/commit/11c84529c5fc16168470109f07ab83cbe15ca181
The thing I understood :
USER_QUERY
when arriving on the app, retrieving many information for this user as id, username, friends, groups etc.FRIENDS_QUERY
that only retrieves the friends of this same userpreviousResult
(id, username, etc.), and a second time with a user only matching theFRIENDS_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 !