简体   繁体   English

如何从单个反应组件调用多个graphql突变?

[英]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. 目前,我有一个react组件,需要在不同按钮单击时从同一组件调用2个突变。

I am using react-apollo to interact with graphql server from react component. 我正在使用react-apollo与来自react组件的graphql服务器进行交互。

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. 如果我使用this.props.mutate在点击事件中调用突变,则没有选项/参数指定我要调用的突变。

https://www.apollographql.com/docs/react/essentials/mutations.html 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. 如果我直接使用this.props.mutate({variables: {...}})它将始终调用导入到文件中的第一个突变。

You can use "compose" function from react-apollo library to make multiple mutations available to be called inside your component. 您可以使用react-apollo库中的“ compose”功能使多个变异可在组件内部调用。

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

const XComponent = {
...
}

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

Then inside your XComponent you can call both 然后,您可以在XComponent内同时调用

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

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在React中的Apollo客户端突变组件中包装GraphQL突变 - How to wrap GraphQL mutation in Apollo client mutation component in React 如何在 React 中的 GraphQL 突变组件中包装的按钮上具有多个 onClick 事件 - How to have multiple onClick events on a button wrapped in a GraphQL mutation component in React Apollo / graphQL:如何使用开放式UI对嵌套的React组件进行突变? - Apollo/graphQL: How to use optimistic UI for a mutation of a nested react component? 如何正确地将 graphql 突变推送到组件的道具中? - How to push a graphql mutation into a component's props properly? 如何使用无状态组件中的react-navigation将getParam参数传递到另一个屏幕以触发graphql突变? - How would I pass a getParam param to another screen using react-navigation in a stateless component to fire up a graphql mutation? 如何使用React Apollo 2.1的Mutation组件在mount上运行变异? - How to run a mutation on mount with React Apollo 2.1's Mutation component? 从另一个变异中调用GraphQL变异? - Calling GraphQL mutation from another mutation? 如何从基于类的反应组件中查询 graphql endpint - How to query graphql endpint from a class based react component 如何从graphql突变而不是null返回新记录 - how to return new record from graphql mutation instead of null 如何从GraphQL突变访问variableValues? - How do I access variableValues from GraphQL mutation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM