最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to call multiple graphql mutation from a single react component? - Stack Overflow

programmeradmin1浏览0评论

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

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

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.

.html

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

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

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

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./docs/react/essentials/mutations.html

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

Share Improve this question edited Aug 17, 2018 at 10:09 Deepak Kumar Padhy asked Aug 17, 2018 at 5:53 Deepak Kumar PadhyDeepak Kumar Padhy 4,4086 gold badges51 silver badges82 bronze badges 1
  • Are you using the Mutation ponent? If so, you can wrap more than one ponents with different mutations. – Ionut Achim Commented Aug 17, 2018 at 16:21
Add a ment  | 

2 Answers 2

Reset to default 3

Inspiration: https://github./sysgears/apollo-universal-starter-kit/blob/master/packages/client/src/modules/post/containers/PostComments.jsx https://github./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 ponent.

UPDATE:

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

You can use "pose" function from react-apollo library to make multiple mutations available to be called inside your ponent.

import { pose, graphql } from "react-apollo";

const XComponent = {
...
}

const XComponentWithMutations =  pose(
    graphql(mutation1, {
    name : 'mutation1'
  }),
    graphql(mutation2, {
    name: 'mutation2'
  })
)(XComponent);

Then inside your XComponent you can call both

props.mutation1({variables: ...})

or

props.mutation2({variables: ...})
发布评论

评论列表(0)

  1. 暂无评论