updateQueries will only update cache if keys in mutation variables exactly match mutation signature
See original GitHub issueHi there! I am new to graphql and apollo and building a small issue tracker to try them out. I have come across this problem while trying to use updateQueries, to update a list after a mutation and I am not sure if this is intended behaviour or a bug: if the payload of the mutation does not contain exactly the same keys as the parameters in the mutation signature, the return value of updateQueries will be ignored.
I have created a simple representation of the problem as follows:
query
const query = gql`
query issues{
issues {
id
title
description
project{
id
title
}
}
}
`;
mutation
const mutation = gql`
mutation createIssue($title: String!, $project: String!, $description: String){
createIssue(title: $title, project: $project, description: $description){
id
title
description
project{
id
title
}
}
}
`;
mutation HOC
const withMutation = graphql(mutation, {
props: ({ownProps, mutate}) => {
return {
createIssue: data => mutate({
variables: data,
updateQueries: {
issues: (prev, action) => {
console.log(action);
const {mutationResult} = action;
const newIssue = mutationResult.data.createIssue;
return {
issues: [newIssue, ...prev.issues]
}
}
}
})
}
}
});
code calling mutation
const issue = {
title: faker.hacker.phrase(),
project: projectsIds[Math.floor(Math.random()*projectsIds.length)],
description: 'test',
};
this.props.createIssue(issue).then(() => {
console.log('created issue');
});
If I leave out the optional description
key from the issue object, updateQueries will still run, but its return value will not be used to update the cache.
Is this intended behaviour, and if so, what is the reason it should work this way?
Thank you.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:14 (6 by maintainers)
Top GitHub Comments
@mkozhukharenko I’m having the same exact issue. After a lot of digging and trying, I found out that the query mutation fields need to match the query that you want to update via updateQueries, or else something weird happens and the value will be undefined.
The linked issue seems to go into more detail.
I believe this is the right issue: I’m also unable to add an item to a collection using
updateQueries
, if that collection is empty to begin with.