Flattening response object
Sometimes, APIs return the data you need nested in multiple object layers, like this:
{
user: {
id: 9802340,
details: {
name: "Iddo Gino",
age: 21
}
}
}
Let's say you only want the user's name and age. This can be easily accomplished with the following query:
{
user {
details {
name,
age
}
}
}
But this would return the data nested withing redundant layers of objects, and would be less nice than just having the name and age (we don't need them in the user & details objects).
Flat objects to the rescue
Adding the '-' before the object name in the query will remove that objects (and replace it with it's contents). So in our example, the following query:
{
-user {
-details {
name,
age
}
}
}
Will return the following (cleaner) response:
{
name: "Iddo Gino",
age: 21
}
This can be done with any Object response. For instance, with Http API calls, you can get rid of the call object:
{
-Http.get(url:"https://httpbin.org/get") {
origin,
url
}
}
Will return:
{
origin: "127.0.0.1",
url: "https://httpbin.org/get"
}
Updated less than a minute ago