Can graphql return aggregate counts?

Can graphql return aggregate counts?

85

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?

Share
Improve this question

    6 Answers
    6

    Reset to default

    38

    You would define a new GraphQL type that is an object that contains a list and a number. The number would be defined by a resolver function.

    On your GraphQL server you can define the resolver function and as part of that, you would have to write the code that performs whatever calculations and queries are necessary to get the aggregate counts.

    This is similar to how you would write an object serializer for a REST API or a custom REST API endpoint that runs whatever database queries are needed to calculate the aggregate counts.

    GraphQL’s strength is that it gives the frontend more power in determining what data specifically is returned. Some of what you write in GraphQL will be the same as what you would write for a REST API.

    Share
    Improve this answer

    1

    • 9

      Good explanation! I think the main takeaway point is that GraphQL doesn’t do your calculations and aggregations for you. All of that is supposed to happen in the resolve function of the GraphQL query object, which needs to be told to retrieve the needed pre-aggregated data, does its work on it and then returns the result.

      – batjko

      Apr 13, 2016 at 15:11

    20

    There’s no automatic aggregate function in GraphQL itself.

    You can add a field called summary, and in the resolve function calculate the totals.

    Share
    Improve this answer

      17

      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;
          }
      

      Share
      Improve this answer

      0

        15

        … need graphql to return aggregate counts? Can this be done?

        Yes, it can be done.

        Does GraphQL does it automatically for you? No, because it does not know / care about where you get your data source.

        How? GraphQL does not dictate how you get / mutate the data that the user has queried. It’s up to your implementation to get the requested aggregated data. You could get aggregated data directly from your MongoDB and serve it back, or you get all the data you need from your data source and do the aggregation yourself.

        Share
        Improve this answer

          3

          If you are using Hasura, in the explorer, you can definitely see an “agregate” table name, thus, your query would look something similar to the following:

          query queryTable {
           table_name { 
            field1
            field2
          }
          table_name_aggregate { 
          aggregate { count } 
            } 
          }
          

          In your results, you will see the total row count for the query

          "table_name_aggregate": {
                "aggregate": {
                  "count": 9973
                }
          

          Share
          Improve this answer

            2

            This depends on whether you build the aggregator into your schema and are able to resolve the field.

            Can you share what kind of GraphQL Server you’re running? As different languages have different implementations, as well as different services (like Hasura, 8base, and Prisma).

            Also, when you say “counts”, I’m imagining a count of objects in a relation. Such as:

            query {
              user(id: "1") {
                name
                summaries {
                  count
                }
              }
            }
            
            // returns
            {
              "data": {
                "user": {
                  "name": "Steve",
                  "summaries": {
                    "count": 10
                  }
                }
              }
            }
            

            8base provides the count aggregate by default on relational queries.

            Share
            Improve this answer



              Your Answer


              Post as a guest

              Required, but never shown


              By clicking тАЬPost Your AnswerтАЭ, you agree to our terms of service, privacy policy and cookie policy

              Not the answer you’re looking for? Browse other questions tagged

              or ask your own question.

              Leave a Reply

              Your email address will not be published. Required fields are marked *