Neau Adrien Neau Adrien - 7 days ago 6
React JSX Question

How to updateQueries when the Query contain edges and node with Apollo?

I use react-apollo on my React application for request a GraphQL API. I want to add an item and update my query after this add.

The item is added correctly but I can't update my query. I have to refresh my page to see my new item. I follow this guide.

This is my query:

export const allCodeReviews = gql`
query allCodeReviews {
allCodeReviews {
edges {
node {
id
reference
reviewer
revisionDate
redmineUrl
flow {
id
name
}
}
}
}
}


and this is my mutation which add my new item:

this.props.mutate({
variables: {
"reviewer": "john",
"revisionDate": "2016-11-11",
"flow": "foo",
"redmineUrl": "foo.com",
"reference": "#bar"
},
updateQueries: {
allCodeReviews: (prev, {mutationResult}) => {
const addedReview = mutationResult.data.addCodeReview.codeReview;
console.log(addedReview);
console.log(prev);
return update(prev, {
allCodeReviews: {
edges: {
$unshift: [addedReview]
}
}
});
}
}
});


How to update this query correctly?

Answer

I think you should try with: $unshift: [{node: addedReview}]. Depending on your addCodeReview mutation.

Comments