Overview (simplified):
In my NodeJS server I've implemented the following GraphQL schema:
type Item {
name: String,
value: Float
}
type Query {
items(names: [String]!): [Item]
}
The client query then passes an array of names, as an argument:
{
items(names: ["total","active"] ) {
name
value
}
}
The backend API queries a mysql DB, for the "total" and "active" fields (columns on my DB table) and reduces the response like so:
[{"name":"total" , value:100} , {"name":"active" , value:50}]
I would like my graphQL API to support "ratio" Item, I.E: I would like to send the following query:
{
items(names: ["ratio"] ) {
name
value
}
}
or
{
items(names: ["total","active","ratio"] ) {
name
value
}
}
And return active / total as the calculated result of that new field ([{"name":"ratio" , value:0.5}]
). What would be a generic way to handle the "ratio" field differently?
Should it be a new type in my schema or should I implement the logic in the reducer?
items(names: ["total","active","ratio"] )
– Fruitytype Kpi {total: Int, active: Int, ratio: Float}
and make the same input type. Then you can pass that in your query but as calculating is concerned I think it must be done in your resolver functions and GraphQL has no way of doing it. – Comanche("ratio" == "active"/"total")
so when you ask for "ratio" the DB query will request active + total and then will run a post processor for the calculations. – Jonniejonny