Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you get the return data from a mutation? #194

Open
joshuarobs opened this issue Jul 19, 2019 · 5 comments
Open

How do you get the return data from a mutation? #194

joshuarobs opened this issue Jul 19, 2019 · 5 comments

Comments

@joshuarobs
Copy link

So when you call mutations in Graphql, you can ask for some return data. How do you do that with this library? I see no way to get what the server has sent back after making a mutation.

@revskill10
Copy link

revskill10 commented Jul 19, 2019

I made a custom useMutation hook, which allows returning data, loading and error from mutation

import useClient from "./useClient";
import gql from "graphql-tag";
import { useState } from "react";

function useMutate(
  url: string,
  mutation: any,
): [any, { data: any; error: any; loading: any }] {
  const client = useClient(url);
  const gqlQuery = gql(mutation);
  const [data, setData] = useState(null);
  const [error, setError] = useState(null);
  const [loading, setLoading] = useState(false);

  const mutate = async (variables: any) => {
    try {
      setLoading(true);
      const res = await client.mutate({
        mutation: gqlQuery,
        variables
      });
      setLoading(false);
      if (res.data) {
        setData(res.data);
      } else {
        // setError(JSON.stringify(res.errors, Object.getOwnPropertyNames(res.errors))))
        console.log(res)
      }
    } catch (e) {
      // console.log(e);
      setLoading(false);
      setError(e);
    }
  };

  return [mutate, { data, error, loading }];
}

export default useMutate;

@joshuarobs
Copy link
Author

@revskill10 Thanks you for that custom hook. I actually managed to figure it out but I didn't use a custom hook instead. How does your custom hook differ from passing in update and using mutationResult?

@revskill10
Copy link

revskill10 commented Jul 19, 2019

@joshuarobs What do you mean by passing in update and using mutationResult ?

The usage is simple:

const [mutate, { data, error, loading}] = useMutate(url, mutationQuery)

@joshuarobs
Copy link
Author

joshuarobs commented Jul 19, 2019

@revskill10 This is what I am using, which I found from the readme:

const [createUser, { loading }] = useMutation(CREATE_USER_MUTATION, {
    update: (proxy, mutationResult) => {
      console.log('mutationResult: ', mutationResult);
    },
    variables: {
      firstName,
      lastName,
      email,
      password,
    },
  });

The result from the mutation is in the mutationResult argument. But it only gets the result (if successful). I have no way that I know of where I can get the error message if the mutation fails.

I'm going to try your custom hook since it appears as though it can support returning the error. Also what is useClient in your custom hook?

@revskill10
Copy link

@joshuarobs My useMutate is used differently.

const onClick = () => {
   mutate(variables)
}

This library force variables in the argument of the hook, which i see is not flexible enough.
THe useClient is just a hook to return the Apollo Client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants