reactjsgraphqlreact-hooksapollo-clientreact-apollo-hooks

How to get the mutation response?


I'm doing this mutation successfully in my reactjs app, but I want to get the response graphql returns but in my reactjs app, is it possible?

React code:

import { useMutation } from "@apollo/react-hooks";      

const [addStoreToDB, { error }] = useMutation(addStoreMutation);

  if (error) {
    console.log("error:", error);
    return <p>Error :(</p>;
  }

  const handleSubmit = event => {
    event.preventDefault();
    dispatch(addStoreAction(newStore));
    addStoreToDB({
      variables: {
        name: newStore.name,
        address: newStore.address,
        description: newStore.description,
        phone: newStore.phone,
        picture1: newStore.picture1
      },
      refetchQueries: [{ query: getStoresQuery }]
    });
  };

in GraphQL:

mutation {
  addStore (name:"Lorem ipsum", address:"Lorem ipsum", description:"Lorem Ipsum", phone:"555555", picture1:"https://lorem-ipsum") {
    id
  }
}

Solution

  • const [addStoreToDB, { error }] = useMutation(addStoreMutation
        {
          onCompleted: (data) => {
            console.log(data) // the response
          },
          onError: (error) => {
            console.log(error); // the error if that is the case
          },
        }
      );