Support of aggregate function in GraphQL

GraphQL, at the end of the day, responds with your defined types. You just need to put that data into a type. Whether this is a specific type for these different queries, or fields for that data on existing types, it's up to you but that's all it boils down to. GraphQL does require more effort up front in terms of defining your types and what all queries will return, which makes it more rigid, but the idea is that on other side of that lies some cool features, like introspection and type checking. If it doesn't seem to make logical sense to put that sort of "ad hoc" data structures into a GraphQL type, then it's not illegal to have non-GraphQL endpoints if you need other data sources.


@Damien, those problems are not GraphQL's problems.

Whenever you want do something in GraphQL you must define a Type of return data, Spec of function you implement, and sometimes a Type of input data to feed into your function. Finally you write code to do the job.

In fact, it looks like you (re)write your code in GraphQL language.

Take the example where you want to display the info in a Pie Chart:

SELECT age, count(*) from Ticket group by age;

Define your return data here is a list of age and count:

 type TickGroupByAge {
      age: Int
      count: Int
    }

Define your function or Query in GraphQL language:

getTicketGroupByAge : [TickGroupByAge]`

Finally write a function to implement above query:

async function(){
    const res = await client.query("SELECT age, count(*) from Ticket group by age");
    return res.rows;
}

@Ryan I totally agree with you that GraphQL forces you to write a lot of type definitions to resolve a simple task. For that reason, I ended up building my own NextQL - GraphQL-liked engine which is similar to GraphQL but simpler.

My project supports complex nested type definitions, which free you from define a lot of useless ones.

Tags:

Graphql