React Apollo - Make Multiple Queries

后端 未结 10 1957
渐次进展
渐次进展 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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);

提交回复
热议问题