React Apollo - Make Multiple Queries

后端 未结 10 1938
渐次进展
渐次进展 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: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 }) => { render },
        second: ({ render }) => { render }
      });
    
      return (
        
          ({ first, second }) => {
            console.log('first', first)
            console.log('second', second)
    
            // validations (loading, error)
    
            return (
              
    Your JSX
    ) }
    ) } ...

    There are a lot of examples using

    const Composed = adopt({
      first: ,
      second: 
    });
    

    Be careful with 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 }) => { render }
    

    Hope it helps you!

提交回复
热议问题