GraphQL filtering
Moving from the REST API to the GraphQL API gave us the benefit of returning only the fields we need from a particular data type.
Lord Lamington’s aim is to keep all his cafes COVID safe. However, he is more concerned about the cafes in the state of Victoria (VIC). Heartcore’s GraphQL Filtering and Ordering allows a developer to easily filter the dataset at the source rather than at the destination.
Request:
curl --location --request POST 'https://graphql.umbraco.io' \
--header 'umb-project-alias: damazingnut' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"query($state: String!){
allCafe(where: { state: $state }) {
items {
name
postcode
state
indoorSquareArea
outdoorSquareArea
}
}
}","variables":{"state":"VIC"}}'
Response:
{
"data": {
"allCafe": {
"items": [
{
"name": "Melbourne CBD",
"postcode": 3000,
"state": "VIC",
"indoorSquareArea": 100,
"outdoorSquareArea": 144
},
{
"name": "Springvale",
"postcode": 3171,
"state": "VIC",
"indoorSquareArea": 60,
"outdoorSquareArea": 80
},
{
"name": "Geelong",
"postcode": 3220,
"state": "VIC",
"indoorSquareArea": 80,
"outdoorSquareArea": 100
}
]
}
}
}
Response size:
Total response size
|
846 B
|
Body
|
326 B
|
Headers
|
520 B
|
With filtering in place, the size of the body response has halved again to 326 Bytes from 804 Bytes when we retrieved the non-filtered data. Another advantage of filtering and ordering the data at the source is that the client application does not need to manipulate any extraneous data.