React Apollo - Make Multiple Queries

后端 未结 10 1936
渐次进展
渐次进展 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);
    

提交回复
热议问题