Schema tutorialLesson 26: Filtering data from an external API
Lesson 26: Filtering data from an external API
If the external API does not allow filtering by a certain property that we need, we can use Gato GraphQL to iterate over the entries in the API response, and remove those ones that do not satisfy our condition.
Let's refer again to REST API endpoint newapi.getpop.org/wp-json/wp/v2/users/?_fields=id,name,url
, where some users have property url
empty:
The GraphQL query below filters out those users where the url
property is empty, by:
- Retrieving data from the external API
- Iterating over the entries via
@underEachArrayItem
, and placing each entry under dynamic variable$userDataEntry
- Extracting property
url
from each entry, and placing this value under dynamic variable$websiteURL
- Checking if this value is empty, and assigning the result under dynamic variable
$isWebsiteURLEmpty
- Applying conditional directive
@if
which, if$isWebsiteURLEmpty
istrue
, sets the value of that entry asnull
- Executing directive
@arrayFilter
to filter out allnull
entries
The response is: