React Apollo Mutations allow me to create a component that takes in a MutationResult with { data, loading, error }
as a prop: https://www.apollographql.com/docs/react/api/react-apollo.html#mutation-render-prop
Now if I was to use the higher order component created by graphql()
function MyComponent({ mutate }) {
return (
<button onClick={() => {
mutate({
variables: { foo: 42 },
});
}}>
Mutate
</button>
);
}
export default graphql(gql`mutation { ... }`)(MyComponent);
My component will only have mutate
as a prop. How do I get { data, loading, error }
into the component?
mutate
returns a Promise. So you can use.then
to get the result from the mutation or.catch
in case there was an error. In the case of the loading, you can do it with component state, callingsetState({ loading: true})
before themutate
andsetState({ loading: false})
after it. Hope it helps! – qmateub