如何获得突变反应?
How to get the mutation response?
我在我的 reactjs 应用程序中成功地做了这个突变,但我想得到响应 graphql returns 但是在我的 reactjs 应用程序中,这可能吗?
反应代码:
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 }]
});
};
在 GraphQL 中:
mutation {
addStore (name:"Lorem ipsum", address:"Lorem ipsum", description:"Lorem Ipsum", phone:"555555", picture1:"https://lorem-ipsum") {
id
}
}
useMutation 返回的变异函数是一个承诺。所以你可以像
这样使用 .then
和 .catch
addStoreToDB({...stuff})
.then(response => {
//handle response
})
.catch(err =>{
//handle error
})
const [addStoreToDB, { error }] = useMutation(addStoreMutation
{
onCompleted: (data) => {
console.log(data) // the response
},
onError: (error) => {
console.log(error); // the error if that is the case
},
}
);
我在我的 reactjs 应用程序中成功地做了这个突变,但我想得到响应 graphql returns 但是在我的 reactjs 应用程序中,这可能吗?
反应代码:
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 }]
});
};
在 GraphQL 中:
mutation {
addStore (name:"Lorem ipsum", address:"Lorem ipsum", description:"Lorem Ipsum", phone:"555555", picture1:"https://lorem-ipsum") {
id
}
}
useMutation 返回的变异函数是一个承诺。所以你可以像
这样使用.then
和 .catch
addStoreToDB({...stuff})
.then(response => {
//handle response
})
.catch(err =>{
//handle error
})
const [addStoreToDB, { error }] = useMutation(addStoreMutation
{
onCompleted: (data) => {
console.log(data) // the response
},
onError: (error) => {
console.log(error); // the error if that is the case
},
}
);