没有React <Mutation>组件的Apollo突变

时间:2019-06-02 17:10:34

标签: react-apollo

Apollo的<Mutation>组件通常运行良好,但是有时您需要在render()方法之外调用突变。

在某些情况下,您可以像这样简单地传递突变函数:

import React, { Component } from "react";
import { DO_MUTATION } from "./mutations";
import { Mutation } from "react-apollo";

export default class MyComponent extends Component {
    render() {
        return (
            <Mutation mutation={DO_MUTATION}>
                {(doMutation) => (
                    <Button
                        onPress={() => {
                            this.handleSomething(doMutation);
                        }}
                    />
                )}
            </Mutation>
        );
    }

    handleSomething = (doMutation) => {
        /* DO SOME STUFF */
        doMutation();
    };
}

但是在其他情况下,这不是一个非常合理的选择,例如:

import React, { Component } from "react";
import { DO_MUTATION } from "./mutations";
import { Mutation } from "react-apollo";

import SomeLibrary from "SomeLibrary";

export default class MyComponent extends Component {
    render() {
        return (
            <Mutation mutation={DO_MUTATION}>
                {(doMutation) => (
                    <Button
                        onPress={() => {
                            SomeLibrary.addListener(this.listenerHandler);
                        }}
                    />
                )}
            </Mutation>
        );
    }

    listenerHandler = () => {
        /* HOW DO I DO MUTATIONS HERE? */
    };
}

在这些情况下如何进行突变?

1 个答案:

答案 0 :(得分:1)

react-apollo包括两个名为graphql()withApollo()的HOC,可以用来完成此任务。

两者之间的区别在Apollo的文档中描述为:

  

如果您想知道何时使用withApollo()和何时使用graphql(),答案是大多数时候您将要使用graphql()。 graphql()提供了处理GraphQL数据所需的许多高级功能。如果希望GraphQL客户端不具有任何其他功能,则仅应使用withApollo()。

当提供graphql()的一个突变时,它将添加一个this.props.mutate()函数,可以这样使用:

import React, { Component } from "react";
import { DO_MUTATION } from "./mutations";
import { graphql } from "react-apollo";

import SomeLibrary from "SomeLibrary";

export class MyComponent extends Component {
    render() {
        return (
            <Button
                onPress={() => {
                    SomeLibrary.addListener(this.listenerHandler);
                }}
            />
        );
    }

    listenerHandler = () => {
        this.props.mutate({
            variables: {
                some_var: "some_val",
            },
        });
    };
}
export default graphql(DO_MUTATION)(MyComponent);

withApollo()类似,但是提供了this.props.client供您直接使用。可以这样执行突变:

import React, { Component } from "react";
import { DO_MUTATION } from "./mutations";
import { withApollo } from "react-apollo";

import SomeLibrary from "SomeLibrary";

export class MyComponent extends Component {
    render() {
        return (
            <Button
                onPress={() => {
                    SomeLibrary.addListener(this.listenerHandler);
                }}
            />
        );
    }

    listenerHandler = () => {
        this.props.client.mutate({
            mutation: DO_MUTATION,
            variables: {
                some_var: "some_val",
            },
        });
    };
}
export default withApollo(MyComponent);