HTTP Client
Addition of fields to the GraphQL schema to execute HTTP requests against a webserver and fetch their response:
_sendJSONObjectItemHTTPRequest
_sendJSONObjectItemHTTPRequests
_sendJSONObjectCollectionHTTPRequest
_sendJSONObjectCollectionHTTPRequests
_sendHTTPRequest
_sendHTTPRequests
_sendGraphQLHTTPRequest
_sendGraphQLHTTPRequests
Due to security reasons, the URLs that can be connected to must be explicitly configured.
List of fields
The following fields are added to the schema.
_sendJSONObjectItemHTTPRequest
It retrieves the (REST) response for a single JSON object.
Signature: _sendJSONObjectItemHTTPRequest(input: HTTPRequestInput!): JSONObject
.
_sendJSONObjectItemHTTPRequests
It retrieves the (REST) response for a single JSON object from multiple endpoints, executed asynchronously (in parallel) or synchronously (one after the other).
Signature: _sendJSONObjectItemHTTPRequests(async: Boolean = true, inputs: [HTTPRequestInput!]!): [JSONObject]
.
_sendJSONObjectCollectionHTTPRequest
It retrieves the (REST) response for a collection of JSON objects.
Signature: _sendJSONObjectCollectionHTTPRequest(input: HTTPRequestInput!): [JSONObject]
.
_sendJSONObjectCollectionHTTPRequests
It retrieves the (REST) response for a collection of JSON objects from multiple endpoints, executed asynchronously (in parallel) or synchronously (one after the other).
Signature: _sendJSONObjectCollectionHTTPRequests(async: Boolean = true, inputs: [HTTPRequestInput!]!): [[JSONObject]]
.
_sendHTTPRequest
It connects to the specified URL and retrieves an HTTPResponse
object, which contains the following fields:
statusCode: Int!
contentType: String!
body: String!
headers: JSONObject!
header(name: String!): String
hasHeader(name: String!): Boolean!
Signature: _sendHTTPRequest(input: HTTPRequestInput!): HTTPResponse
.
_sendHTTPRequests
Similar to _sendHTTPRequest
but it receives multiple URLs, and allows to connect to them asynchronously (in parallel).
Signature: _sendHTTPRequests(async: Boolean = true, inputs: [HTTPRequestInput!]!): [HTTPResponse]
.
_sendGraphQLHTTPRequest
Execute a GraphQL query against the provided endpoint, and retrieve the response as a JSON object.
The input to this field accepts the data expected for GraphQL: the endpoint, GraphQL query, variables and operation name, and already sets the default method (POST
) and content type (application/json
).
Signature: _sendGraphQLHTTPRequest(input: GraphQLRequestInput!): JSONObject
.
_sendGraphQLHTTPRequests
Similar to _sendGraphQLHTTPRequests
but it executes multiple GraphQL queries concurrently, whether asynchronously (in parallel) or synchronously (one after the other).
Signature: _sendGraphQLHTTPRequests(async: Boolean = true, inputs: [GraphQLRequestInput!]!): JSONObject
.
Configuring the allowed URLs
We must configure the list of URLs that we can connect to.
Each entry can either be:
- A regex (regular expression), if it's surrounded by
/
or#
, or - The complete URL, otherwise
For instance, any of these entries match URL "https://gatographql.com/recipes/"
:
https://gatographql.com/recipes/
#https://gatographql.com/recipes/?#
#https://gatographql.com/.*#
/https:\\/\\/gatographql.com\\/(\S+)/
There are 2 places where this configuration can take place, in order of priority:
- Custom: In the corresponding Schema Configuration
- General: In the Settings page
In the Schema Configuration applied to the endpoint, select option "Use custom configuration"
and then input the desired entries:
Otherwise, the entries defined in the "Send HTTP Request Fields" tab from the Settings will be used:
There are 2 behaviors, "Allow access" and "Deny access":
- Allow access: only the configured entries can be accessed, and no other can
- Deny access: the configured entries cannot be accessed, all other entries can
When to use each field
All fields are similar but different.
_sendJSONObjectItemHTTPRequest
This field retrieves a JSON object item, which is useful when querying a single item from a REST endpoint, as from the WP REST API endpoint /wp-json/wp/v2/posts/1/
.
This query:
...retrieves this response:
_sendJSONObjectCollectionHTTPRequest
This field is similar to _sendJSONObjectItemHTTPRequest
, but it retrieves a collection of JSON objects, as from the WP REST API endpoint /wp-json/wp/v2/posts/
.
This query:
...retrieves this response:
_sendHTTPRequest
This field retrieves an HTTPResponse
object with all properties from the response, so we can independently query the body (which is of type String
, i.e. it is not cast as JSON), the status code, content type and headers.
For instance, the following query:
...brings this response:
_sendGraphQLHTTPRequest
Executing the following query:
...brings the following response:
_sendJSONObjectItemHTTPRequests
, _sendJSONObjectCollectionHTTPRequests
, _sendGraphQLHTTPRequests
and _sendHTTPRequests
Multiple-request fields:
These fields work similar to their corresponding non-multiple fields, but they retrieve data from several endpoints at once, either asynchronously (in parallel) or synchronously (one after the other). The responses are placed in a list, in the same order in which the URLs were defined in the urls
parameter.
For instance, the following query:
...produces this response:
Synchronous vs Asynchronous execution
These fields allow us to execute multiple requests:
_sendHTTPRequests
_sendJSONObjectItemHTTPRequests
_sendJSONObjectCollectionHTTPRequests
_sendGraphQLHTTPRequests
These fields receive input $async
, to define if the requests must be executed synchronously ($async => false
) or asynchronously.
Synchronous execution
The HTTP requests are executed in order, with each one executed right after the previous one has been resolved.
When all HTTP requests are successful, the field will print an array with their responses, in the same order as they appear in the input list.
If any HTTP request fails, then the execution stops right there, i.e. the subsequent HTTP requests in the input list are not executed.
Some possible causes of failing HTTP requests are:
- The server to connect to is offline
- The status code of the response is not 200: a 500 internal error, a 404 not found, a 403 forbidden, etc.
- The content type of the response is not
application/json
(The latter two are treated as an error by _sendJSONObjectItemHTTPRequests
, _sendJSONObjectCollectionHTTPRequests
and _sendGraphQLHTTPRequests
, which expect to handle JSON
types only, but not by _sendHTTPRequests
, which is not opinionated.)
In case of error, the field returns null
(i.e. the response for any previous successful HTTP request will not be printed), and the error entry will contain extension httpRequestInputArrayPosition
to indicate which is the item from the input list that failed (starting from 0):
Asynchronous execution
All HTTP requests are executed concurrently (i.e. in parallel), and it is not known in what order will the HTTP requests be resolved.
When all HTTP requests are successful, the field will print an array with their responses, in the same order as they appear in the input list.
Whenever any one HTTP request fails, the execution stops immediately, however by then all other HTTP requests may have been executed too.
In addition, the server will not indicate which is the item in the list that failed (notice that there is not httpRequestInputArrayPosition
extension in the response below):
Global Fields
All these fields are Global Fields, so they are added to every single type in the GraphQL schema: in QueryRoot
, but also in Post
, User
, Comment
, etc.
This allows us to connect to some external API endpoint generated on runtime in the same GraphQL query, based on the data stored on some entity.
For instance, we can iterate a list of users in our database and, for each, connect to an external system (such as a CRM) to retrieve further data about them.
In this query, we generate the API endpoint using the Field to Input feature, and the _arrayJoin
function field:
...producing: