Can graphql return aggregate counts?

前端 未结 5 1101
说谎
说谎 2021-02-03 16:58

Graphql is great and I\'ve started using it in my app. I have a page that displays summary information and I need graphql to return aggregate counts? Can this be done?

5条回答
  •  滥情空心
    2021-02-03 17:59

    You should define a Type of aggregated data in Graphql and a function you want to implement it. For example, if you want to write the following query:

    SELECT age, sum(score) from student group by age;

    You should define the data type that you want to return:

     type StudentScoreByAge{
          age: Int
          sumOfScore: Float
        }
    

    and a Graphql function:

     getStudentScoreByAge : [StudentScoreByAge]
        async function(){
            const res = await client.query("SELECT age, sum(score) as sumOfScore 
                                            from Student group by age");
            return res.rows;
        }
    

提交回复
热议问题