Multiple Queries/Mutation in Apollo 2.1

后端 未结 6 620
滥情空心
滥情空心 2021-02-05 12:10

I need some help using the new Query and Mutation component in Apollo 2.1, especially with multiple queries and mutations.

I have the following problems:

  1. I
6条回答
  •  难免孤独
    2021-02-05 12:46

    For this purpose react-apollo exports a compose function. Using this function you may cleanly use several component enhancers at once. Including multiple graphql(), or even Redux connect() enhancers.

    import { Mutation, compose, graphql } from "react-apollo";
    
    class AddTweet extends Component {
    ....
    ....
    ....
    }
    export default compose(
      graphql(GET_AUTHORS, { name: "getAuthors" }),
      graphql(ADD_TWEET, { name: "addTweet" }),
      connect(...), // incase you are using Redux
    )(AddTweet);
    

    An important note is that compose() executes the last enhancer first and works its way backwards through the list of enhancers.

    One more thing lets say you were using this.props.data now you will get get undefined. just console.log(this.props) and you will see what is happening to props now. You will be having two properties now getAuthors and addTweet. So now it will be this.props.name-in-compose.name-of-type-in-typeDefs i.e. this.props.getAuthors.getUsers. It took me a bit to figure it out.

提交回复
热议问题