Schema Functions
Expose functionalities from the PHP programming language via GraphQL fields and directives.
Click to watch tutorial video - 10:59
The GraphQL schema is provided with fields and directives which expose functionalities from the PHP programming language.
HTTP Client
The GraphQL schema is provided with global fields to execute HTTP requests against a webserver and fetch their response.
This query:
...retrieves this response:
Function Fields
Function fields are global fields, hence they are added to every single type in the GraphQL schema: in QueryRoot
, but also in Post
, User
, etc.
Function fields are useful for manipulating the data once it has been retrieved, allowing us to transform a field value in whatever way it is required, and granting us powerful data import/export capabilities.
For instance, while we have a Post.hasComments
fields, we may need the opposite value. Instead of creating a new field Post.notHasComments
(for which we'd need to edit PHP code), we can use the Field to Input feature to input the value from hasComments
into a not
field, thus calculating the new value always within the GraphQL query:
Function Directives
The GraphQL schema is provided with directives which expose functionalities commonly found in programming languages (such as PHP).
Directive fields are useful for manipulating the data once it has been retrieved, allowing us to transform a field value in whatever way it is required, and granting us powerful data import/export capabilities.
For instance, this query:
...will produce this response:
Helper Fields
The GraphQL schema is added fields which provide commonly-used helper functionality.
Helper fields are global fields, hence they are added to every single type in the GraphQL schema: in QueryRoot
, but also in Post
, User
, etc.
In this query, we retrieve the IDs of the users in the website and execute a new GraphQL query passing their ID as parameter:
Environment Fields
The GraphQL schema is provided with global field _env
, which allows to obtain a value from an environment variable, or from a PHP constant (most commonly defined in wp-config.php
, but can also be defined elsewhere).
This query retrieves the environment constant GITHUB_ACCESS_TOKEN
which we might set-up to access a private repository in GitHub:
Email Sender
The GraphQL schema is provided with global mutation _sendEmail
.
Mutation _sendEmail
sends emails by executing WordPress wp_mail
function. As a result, it will use the configuration defined for sending emails in WordPress (such as the SMTP provider to use).
The email can be sent with content types "text" or "HTML", depending on the value of the messageAs
input (which is a "oneof" InputObject, so that only one of its properties can be provided).