Extension

Schema Functions

Expose functionalities from the PHP programming language via GraphQL fields and directives.

Gato GraphQL logo
Target Image

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:

query {
  postData: _sendJSONObjectItemHTTPRequest(input: {
    url: "https://some-wp-rest-api.com/wp-json/wp/v2/posts/1/"
  } )
}

...retrieves this response:

{
  "data": {
    "postData": {
      "id": 1,
      "date": "2019-08-02T07:53:57",
      "date_gmt": "2019-08-02T07:53:57",
      "guid": {
        "rendered": "https:\/\/newapi.getpop.org\/?p=1"
      },
      "modified": "2021-01-14T13:18:39",
      "modified_gmt": "2021-01-14T13:18:39",
      "slug": "hello-world",
      "status": "publish",
      "type": "post",
      "link": "https:\/\/newapi.getpop.org\/uncategorized\/hello-world\/",
      "title": {
        "rendered": "Hello world!"
      },
      "content": {
        "rendered": "\n<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!<\/p>\n\n\n\n<p>I&#8217;m demonstrating a Youtube video:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Introduction to the Component-based API by Leonardo Losoviz | JSConf.Asia 2019\" width=\"750\" height=\"422\" src=\"https:\/\/www.youtube.com\/embed\/9pT-q0SSYow?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><figcaption>This is my presentation in JSConf Asia 2019<\/figcaption><\/figure>\n",
        "protected": false
      },
      "excerpt": {
        "rendered": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing! I&#8217;m demonstrating a Youtube video:<\/p>\n",
        "protected": false
      },
      "author": 1,
      "featured_media": 0,
      "comment_status": "closed",
      "ping_status": "open",
      "sticky": false,
      "template": "",
      "format": "standard",
      "meta": [],
      "categories": [
        1
      ],
      "tags": [
        193,
        173
      ]
    }
  }
}

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:

query {
  posts {
    id
    hasComments
    notHasComments: _not(value: $__hasComments)
  }
}

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:

query {
  posts {
    title @strUpperCase
  }
}

...will produce this response:

{
  "data": {
    "posts": [
      {
        "title": "HELLO WORLD!"
      },
      {
        "title": "LOVELY WEATHER"
      }
    ]
  }
}

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:

query {
  users {
    userID: id
    url: _urlAddParams(
      url: "https://somewebsite/endpoint/user-data",
      params: {
        userID: $__userID
      }
    )
    headers: _httpRequestHeaders
    headerNameValueEntryList: _objectConvertToNameValueEntryList(
      object: $__headers
    )
    _sendHTTPRequest(input: {
      url: $__url
      options: {
        headers: $__headerNameValueEntryList
      }
    }) {
      statusCode
      contentType
      body
    }
  }
}

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:

query {
  githubAccessToken: _env(name: "GITHUB_ACCESS_TOKEN")
}

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).

mutation {
  _sendEmail(
    input: {
      to: "target@email.com"
      subject: "Email with text content"
      messageAs: {
        text: "Hello world!"
      }
    }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
}

Get your extension

Purchase the Schema Functions extension, or a bundle with all the extensions

60% Off!
Personal
$24.99
Schema Functions
License for 5 domains
Purchase ->
  • 5 domains
  • Support
  • Product updates
Organization
$49.99
Schema Functions
License for 25 domains
Purchase ->
  • 25 domains
  • Support
  • Product updates
Professional
$74.99
Schema Functions
License for 100 domains
Purchase ->
  • 100 domains
  • Support
  • Product updates
Agency
$99.99
Schema Functions
License for 500 domains
Purchase ->
  • 500 domains
  • Support
  • Product updates

The license is for 1 year (renewable every year). Prices are in USD.

30-day money back guarantee

Purchase any extensions with the confidence that you can request a refund

Features illustration
Money back guarantee
Testimonial image

We loved this plugin and are annoyed that it’s better than a similar thing we are working on 😂 (we have a network of news sites and Gato GraphQL will save us a lot of time!)

Joebe W. - Developer
Discover the power

Try demo now!

Play with Gato GraphQL + all extensions in your own sandbox site, for free