React Apollo - Make Multiple Queries

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

    Error :(

    ; console.log(data); console.log(getSubjects);

提交回复
热议问题