How to implement aggregation on Apollo GraphQL server?

Viewed 781

I am using Apollo GraphQL server + SQLDatasource (with knex) to fetch data from a database, but I don't know how to return the aggregated data when using SQL functions like COUNT, AVG, MAX. That's because normally GraphQL uses a static schema to describe the shape of the data returned by the graph, for instance:

const typeDefs = gql`
    type User {
        Id: ID
        Birth: Date
        Name: String
    }
`;

For instance, how could I fetch the result of the following query in GraphQL, having into account that the aggregation functions could be dynamic?

SELECT count(*) AS total_users, MAX(Id) AS max_id, Id, Birth, Name FROM User;
0 Answers
Related