React Apollo - Make Multiple Queries

后端 未结 10 1933
渐次进展
渐次进展 2021-01-30 16:36

I have a queries file that looks like this:

import {gql} from \'react-apollo\';

const queries = {
  getApps: gql`
    {
      apps {
        id
        name
            


        
相关标签:
10条回答
  • 2021-01-30 17:16

    If you don't want to reuse any of those queries independently, why not make a single request by combining both queries in one i.e:

    const combinedQueries = gql`
    {
      apps {
        id
        name
      }
      subjects {
        id
        name
      }
    }
    `
    

    and then you can use it in your component

    import React, {Component} from 'react'
    import combinedQueries from './combinedQueries'
    
    class Test extends Component {
       ...
       render() {
         ...
         if(!this.props.combinedQueries.loading) {
           console.log(this.props.combinedQueries.apps);
           console.log(this.props.combinedQueries.subjects);
         }
         ...
       }
    }
    
    export default graphql(combinedQueries, {name: 'combinedQueries'})(Test);
    
    0 讨论(0)
  • 2021-01-30 17:22

    The Emergence of Apollo Client useQuery Hooks; have changed everything. If you are reading this in 2020 or beyond; I am pretty much sure that you likely be using Apollo client useQuery hook. You can call useQuery Hook as many times as you want to execute the both queries. You can learn more about useQuery hooks in its official documentation https://www.apollographql.com/docs/react/data/queries/ I find it so useful in my recent project. E.g

    const queries = {
      getApps: gql`
        {
          apps {
            id
            name
          }
        }
      `,
    
      getSubjects: gql`
        {
          subjects {
            id
            name
          }
        }
      `
    };
    
    const { loading, error, data } = useQuery(queries);
    
    const { loading:getSubjectsLoading, error:getSubjectsError, data:getSubjects } = useQuery(getSubjects);
    
    if (loading || getSubjectsLoading) return "Loading...";
    if (error || getSubjectsError ) return <p>Error :(</p>;
    
    
    console.log(data);
    console.log(getSubjects);
    
    0 讨论(0)
  • 2021-01-30 17:23
      const { loading: cat_loading, cat_error, data: cat_data } = useQuery(categoriesAllQuery)
      const { loading: prod_loading, prod_error, data: prod_data } = useQuery(productsAllQuery)
    
      if (cat_loading || prod_loading) return <p>Loading ... </p>
      const { categoriesAll } = cat_data
      const { productsAll } = prod_data
    
    0 讨论(0)
  • 2021-01-30 17:25

    My preferred way is to use the compose functionality of the apollo client (docu).

    EDIT: If you have more than one query you should name them.

    So in your case, it could look like this:

    import React, {Component} from 'react'
    import queries from './queries'
    import { graphql, compose } from 'react-apollo';
    
    class Test extends Component {
    ...
    
      render() {
        ...
        
        console.log(this.props.subjectsQuery, this.props.appsQuery); // should show both 
        
        ...
      }
    }
    
    export default compose(
       graphql(queries.getSubjects, {
          name: "subjectsQuery"
       }),
       graphql(queries.getApps, {
          name: "appsQuery"
       }),
    )(Test);

    0 讨论(0)
  • 2021-01-30 17:25

    Another way around this is to use the props option.

    export default compose(
      graphql(QUERY_2, {
        props: ({ data }) => ({ ...data }),
      }),
      graphql(QUERY_1, {
        props: ({ data }) => ({ ...data, myCustomAttribute: data.foo }),
      }),
    )(Component);
    

    I've found that this approach is a bit nicer for my use case.

    Here is a link to the docs: https://www.apollographql.com/docs/react/api/react-apollo.html#graphql-config-props

    0 讨论(0)
  • 2021-01-30 17:30

    I'm using react-adopt to make this. It's really simple and keep our code clean.

    Simple example:

    import { adopt } from 'react-adopt';
    
    ...
    render() {
      const Composed = adopt({
        first: ({ render }) => <Query query={FIRST_QUERY}>{ render }</Query>,
        second: ({ render }) => <Query query={SECOND_QUERY}>{ render }</Query>
      });
    
      return (
        <Composed>
          ({ first, second }) => {
            console.log('first', first)
            console.log('second', second)
    
            // validations (loading, error)
    
            return (
              <div>Your JSX</div>
            )
          }
        </Composed>
      )
    }
    ...
    

    There are a lot of examples using

    const Composed = adopt({
      first: <Query query={FIRST_QUERY} />,
      second: <Query query={SECOND_QUERY} />
    });
    

    Be careful with <Query> component, It needs a children, otherwise, it will have the following error:

    Warning: Failed prop type: The prop children is marked as required in Query, but its value is undefined.
    

    To avoid the previous warning, I have found a possible solution:

    first: ({ render }) => <Query query={FIRST_QUERY}>{ render }</Query>
    

    Hope it helps you!

    0 讨论(0)
提交回复
热议问题