0

根据Apollo Docs,我想mutate()从 ApolloClient 获取函数到属于我的反应组件的道具中。这是正确/首选的方法吗?

class myComponent extends React.Component {
    constructor(props) {
        super(props);
        this.mutate = props.client.mutate();
    };
}
4

1 回答 1

3

如果您想使用 apollo 客户端动态调用突变,则可以像这样使用它:

import { withApollo } from 'react-apollo';

class myComponent extends React.Component {
  constructor(props) {
    super(props);
    this.mutate = props.client.mutate;
  }

  onClick = () => {
    this.mutate({
      mutation: gql `${<define your graphql mutation>}`,
      variables: { ... },
    }).then(...);
  }
  
  ...
}

export default withApollo(MyComponent);

否则我建议你静态定义你的突变,graphql然后调用突变:

class MyComponent extends React.Component {
  onClick = () => {
    this.props.mutate({ variables: { ... } }).then(....);
  }

  ...

}

const yourGraphqlMutation = gql `${<define your graphql mutation>}`;

export default graphql(yourGraphqlMutation)(MyComponent);

于 2017-06-26T10:56:46.603 回答