Pinging external services

We can ping external services about new resources added to our website, passing along data both stored in the website and/or provided via parameters or headers.

In this query, we retrieve the IDs of the comments added in the last 24 hs and, for each, send a ping to some external service, passing their ID as a parameter in the URL, and forwarding some headers from the current HTTP request:

{
timeNow: _time
time24HsAgo: _intSubstract(substract: 86400, from: $__timeNow)
date24HsAgo: _date(format: "Y-m-d\\TH:i:sO", timestamp: $__time24HsAgo)

comments(filter: { dateQuery: { after: $__date24HsAgo } } ) {
commentID: id
url: _urlAddParams(
url: "https://somewebsite.com/ping-new-comment",
params: {
commentID: $__commentID
}
)
headers: _httpRequestHeaders
@remove
requiredHeaders: _objectKeepProperties(
object: $__headers,
keys: ["user-agent", "origin"]
)
@remove
headerNameValueEntryList: _objectConvertToNameValueEntryList(
object: $__requiredHeaders
)
_sendHTTPRequest(input: {
url: $__url
method: GET
options: {
headers: $__headerNameValueEntryList
}
}) {
statusCode
contentType
body
}
}
}

If the external service can receive the data for multiple resources, we can collect all of them, and then send a single ping:

query ExportData {
timeNow: _time
time24HsAgo: _intSubstract(substract: 86400, from: $__timeNow)
date24HsAgo: _date(format: "Y-m-d\\TH:i:sO", timestamp: $__time24HsAgo)

comments(filter: { dateQuery: { after: $__date24HsAgo } } )
@export(as: "commentIDs")
{
id
}

hasComments: _notEmpty(value: $__comments)
@export(as: "hasComments")
@remove
}

query SendPing
@depends(on: "ExportData")
@include(if: $hasComments)
{
url: _urlAddParams(
url: "https://somewebsite.com/ping-new-comments",
params: {
commentIDs: $commentIDs
}
)
headers: _httpRequestHeaders
@remove
requiredHeaders: _objectKeepProperties(
object: $__headers,
keys: ["user-agent", "origin"]
)
@remove
headerNameValueEntryList: _objectConvertToNameValueEntryList(
object: $__requiredHeaders
)
_sendHTTPRequest(input: {
url: $__url
method: GET
options: {
headers: $__headerNameValueEntryList
}
}) {
statusCode
contentType
body
}
}

Extensions referenced in this tutorial permalink

  1. Field Response Removal permalink

    Remove the output of a field from the response.

  2. Field To Input permalink

    Retrieve the value of a field, manipulate it, and input it into another field or directive, all within the same operation.

  3. HTTP Client permalink

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

  4. Multiple Query Execution permalink

    Combine multiple queries into a single query, sharing state across them and executing them in the requested order.

  5. PHP Functions via Schema permalink

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