Filter data from an external API

If we need to fetch data from an external API, but we only need those results that satisfy some condition (such as having a field with a non-empty value), and the API does not support filtering, we can then use Gato GraphQL to implement an API gateway that removes those entries that do not satifsy our condition.

For instance, when invoking the REST API endpoint /users from some WordPress site, we can filter out those users that have the url field empty:

query FilterDataFromWordPressAPI(
# eg: https://somesite.com/wp-json/wp/v2/users/?_fields=id,name,url
$endpointURL: URL!
) {
usersWithWebsiteURL: _sendJSONObjectCollectionHTTPRequest(
input: {
url: $endpointURL
}
)
# Remove users without a website URL
@underEachArrayItem(
passValueOnwardsAs: "userDataEntry"
affectDirectivesUnderPos: [1, 2, 3]
)
@applyField(
name: "_objectProperty"
arguments: {
object: $userDataEntry
by: {
key: "url"
}
}
passOnwardsAs: "websiteURL"
)
@applyField(
name: "_isEmpty"
arguments: {
value: $websiteURL
}
passOnwardsAs: "isWebsiteURLEmpty"
)
@if(
condition: $isWebsiteURLEmpty
)
@setNull
@arrayFilter
}

Extensions required for this query permalink

  1. Conditional Field Manipulation permalink

    Apply a directive on a field only if some condition is met.

  2. Field on Field permalink

    Manipulate the value of a field by applying some other field on it.

  3. Field Value Iteration and Manipulation permalink

    Iterate and manipulate the value elements of array and object fields.

  4. HTTP Client permalink

    Addition of fields to execute HTTP requests against a webserver and fetch their response.

  5. PHP Functions via Schema permalink

    Manipulate the field output using standard programming language functions available in PHP.

Bundles containing all extensions required for this query permalink

  1. “All in One Toolbox for WordPress” Bundle permalink

    Achieve all superpowers: All of Gato GraphQL extensions, in a single plugin

  2. “Automated Content Translation & Sync for WordPress Multisite” Bundle permalink

    Automatically create a translation of a newly-published post using the Google Translate API, for every language site on a WordPress multisite

  3. “Better WordPress Webhooks” Bundle permalink

    Easily create webhooks to process incoming data from any source or service using advanced tools, directly within the wp-admin

  4. “Private GraphQL Server for WordPress” Bundle permalink

    Use GraphQL to power your application (blocks, themes and plugins), internally fetching data without exposing a public endpoint

  5. “Selective Content Import, Export & Sync for WordPress” Bundle permalink

    Import hundreds of records into your WordPress site from another site or service (such as Google Sheets), and selectively export entries to another site

  6. “Tailored WordPress Automator” Bundle permalink

    Create workflows to automate tasks (to transform data, automatically caption images, send notifications, and more)

  7. “Versatile WordPress Request API” Bundle permalink

    Interact with any external API and cloud service, posting and fetching data to/from them

Tutorial lessons explaining how the query works permalink