Can graphql return aggregate counts?

前端 未结 5 1092
说谎
说谎 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:33

    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.

    0 讨论(0)
  • 2021-02-03 17:41

    There's no automatic aggregate function in GraphQL itself.

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

    0 讨论(0)
  • 2021-02-03 17:58

    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.

    0 讨论(0)
  • 2021-02-03 17:59

    ... 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.

    0 讨论(0)
  • 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;
        }
    
    0 讨论(0)
提交回复
热议问题