2

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?

Nevenoe
  • 1,032
  • 3
  • 14
  • 37

1 Answers1

2

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

vwrobel
  • 1,706
  • 15
  • 25
  • Yes, it's works! There was also an error on a date format (for "revisionDate" variable) that prevented updateQueries to work whereas the item was correctly added in database. – Nevenoe Nov 25 '16 at 08:08