Multiple Queries/Mutation in Apollo 2.1

后端 未结 6 538
滥情空心
滥情空心 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:45

    I wrote a Medium Post about how to combine Mutation and Query on the same Component. Here is a snippet from the post

    // other import
    import {Query} from “Apollo-graphql”; // new Query Component
    import gql from "graphql-tag";
    import { graphql } from "react-apollo";
    import UserComponent from '../component/UserComponent'; // any component to display query result
    const GET_ALL_USER = gql`
        {
            allUsers: {
                firstname,
                lastname,
                username,
                # other information
            }
        }
    `
    const UPDATE_USER_STATUS = gql`
        mutation UpdateUserStatus($userID: ID!, $status: Int!){
            updateUserState(userID: $userID, status: $status){
                firstname,
                lastname
                username
                # other information
            }
        }
    `
    ExampleComponent extends React.Component{
        onEditInformation = async (user) => {
            const response  = await mutate({
                variables: {userID: user}
            })
        }
    render(){
            return(
                
                    {({data: { allUsers }}) => {
                        return allusers.map(user => {
                            return (
                                 this.onEditInformation(user)}
                                />
                            )
                        })
                    }}
                
            )
        }
    }
    export default graphql(UPDATE_USER_STATUS)(ExampleComponent);
    

提交回复
热议问题