Queries Library
Queries LibraryFilter data from an external API

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
}