How to pass variables to a GraphQL query?

前端 未结 3 1456
名媛妹妹
名媛妹妹 2021-02-08 03:57

Lets assume I have the following GraphQL query in React-Apollo

const CurrentUserForLayout = gql`
  query CurrentUserForLayout($avatarID: Int!) {
    currentUser          


        
3条回答
  •  感情败类
    2021-02-08 04:27

    Before Apollo Client 2.1

    Before Apollo Client 2.1, when you had to use Higher-Order Components (HOCs), there were two solutions which were pointed out correctly by henk in the other answer. I try to summarize them briefly:

    • Since the HOC has only access to the props, you need to introduce the changing variable in your parent component. For instance, the parent component could have local state management in place for the changing variable with React's setState() method. Once the variable changes in the local state, it can be passed down to your other component which is enhanced by the HOC. The HOC has access to the props and can execute the query with a changed variable. Hint: If you don't want to introduce another component in between to change the local state, recompose's withState HOC might be a great choice.

    • The second way, but maybe less elegant way, because it shifts your code from declarative to imperative, would be to use the withApollo() HOC which gives you access to the Apollo Client instance as prop. Having this instance in your React component, you can call client.query({ query: ..., variables: ... }) in one of your component's class methods (e.g. onChange handler) once your variable changes.

    Since Apollo Client 2.1

    Since Apollo Client 2.1 you can use the new Query (and Mutation) component. The HOC still exists though, but isn't advertised by the documentation anymore. However, the new Query (and Mutation) component is used within your component. It uses React's render props pattern.

    import React from "react";
    import { Query } from "react-apollo";
    import gql from "graphql-tag";
    
    const GET_TODOS = gql`
      {
        todos {
          id
          type
        }
      }
    `;
    
    const Todos = () => (
      
        {({ loading, error, data }) => {
          if (loading) return 

    Loading...

    ; if (error) return

    Error :(

    ; return data.todos.map(({ id, type }) => (

    {type}

    )); }}
    );

    By shifting this control flow inside of the component rather than having it only co-located with a HOC, you can pass the changing prop as variable to your Query (and Mutation) component.

    import React from "react";
    import { Mutation } from "react-apollo";
    import gql from "graphql-tag";
    
    const TOGGLE_TODO = gql`
      mutation ToggleTodo($id: Int!) {
        toggleTodo(id: $id) {
          id
          completed
        }
      }
    `;
    
    const Todo = ({ id, text }) => (
      
        {(toggleTodo, { loading, error, data }) => (
          

    {text}

    {loading &&

    Loading...

    } {error &&

    Error :( Please try again

    }
    )}
    );

    If you want to learn Apollo Client in React, checkout this extensive tutorial. Note: Code Snippets are taken from the Apollo Client 2.1 release blog post.

提交回复
热议问题