React Apollo - Make Multiple Queries

后端 未结 10 1937
渐次进展
渐次进展 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:38

    IMHO, one of the most neat solutions is described in the Apollo Client React implementation.
    The basic idea is to wrap your queries into nested Query components. Using closure functions as component children makes it handy to delegate the results of one query down into another query and so on.

     const QueryOne = gql`
      query One {
        one
      }
    `;
    
    const QueryTwo = gql`
      query Two {
        two
      }
    `;
    
    const NumbersWithData = () => (
      
        {({ loading: loadingOne, data: { one } }) => (
          
            {({ loading: loadingTwo, data: { two }}) => {
              if (loadingOne || loadingTwo) return loading...
              return 

    {one} is less than {two}

    }}
    )}
    );

提交回复
热议问题