Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call multiple graphql mutation from a single react component?

Currently, I have a react component which needs to call 2 mutations from the same component on different buttons clicks.

I am using react-apollo to interact with graphql server from react component.

If I use this.props.mutate to call the mutation on click event, there is no option/argument to specify which mutation I want to call.

https://www.apollographql.com/docs/react/essentials/mutations.html

If I directly use this.props.mutate({variables: {...}}) it always calls the first mutation imported to the file.

like image 926
Deepak Kumar Padhy Avatar asked Feb 14 '26 08:02

Deepak Kumar Padhy


1 Answers

Inspiration: https://github.com/sysgears/apollo-universal-starter-kit/blob/master/packages/client/src/modules/post/containers/PostComments.jsx https://github.com/sysgears/apollo-universal-starter-kit/blob/master/modules/post/client-react/containers/PostComments.web.jsx

By props property you can pass mutation/closure as named (addComment, editComment) prop. Compose query, many mutations, redux (if needed) with one component.

UPDATE:

Other solution from the same project: https://github.com/sysgears/apollo-universal-starter-kit/blob/master/modules/user/client-react/containers/UserOperations.js

like image 91
xadm Avatar answered Feb 16 '26 17:02

xadm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!