How to do Error handling for Neo4j graphQL request in React

Viewed 22

I am new to Neo4J GraphQL. I have got the below request which works for me but if there is any mistake with input, I can see an error in the network tab under response. I am using Apollo-client. How can I access the error in the response and print it on the console? Appreciate your response.

client_id="7b8c903a-3402";  
text= "Some Text";

const preppedQuery = gql`
        mutation CreateMaps(
          $client_id: String!
          $text: String!
        ) {
          createMaps(
            input: [{ client_id: $client_id,  text: $text }]
          ) {
            info {
              nodesCreated
              relationshipsCreated
            }
            maps {
              uid
            }
          }
        }
      `;
      const gqlMutation = {
        mutation: gql`
          ${preppedQuery}
        `,
        variables: {
          client_id,
          text,
        },
      };
      const res = await client.mutate(gqlMutation);
1 Answers

With apollo-client:

Have a look at the apollo client getting started page, there are many great examples of how to use the apollo client with React.

For your case with async/await, however, you may simply use a try/catch:

<rest-omitted>

let res;
try {
  res = await client.mutate(gqlMutation);
} catch (error) {
  console.log(error)
}

or when using a promise you may do:

<rest-omitted>

client.mutate(gqlMutation)
 .then((data) => console.log(data))
 .catch((error) => console.log(error))

Tip: fetch your data conveniently with the useQuery react hook that is provided by the apollo client, see the apollo client getting started page.

Related