-
Notifications
You must be signed in to change notification settings - Fork 109
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
Comments
I made a custom 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; |
@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? |
@joshuarobs What do you mean by The usage is simple: const [mutate, { data, error, loading}] = useMutate(url, mutationQuery) |
@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 I'm going to try your custom hook since it appears as though it can support returning the error. Also what is |
@joshuarobs My
This library force variables in the argument of the hook, which i see is not flexible enough. |
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.
The text was updated successfully, but these errors were encountered: