PHP Functions via Schema

1 domain5 domains25 domains

$24.99

$49.99

$99.99

Buying the extension gives you a license for 1 year (renewable every year), that includes support and access to all extension updates. Prices are in USD.

🛍️ Buy “PHP Functions via Schema” Extension


This extension adds fields and directives to the GraphQL schema which expose functionalities commonly found in programming languages (such as PHP).

Description permalink

Function fields and directives 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.

This query, containing a variety of function fields and directives:

{
_intAdd(add: 15, to: 56)
_intArraySum(array: [1, 2, 3, 4, 5])

_arrayJoin(array: ["Hello", "to", "everyone"], separator: " ")
_arrayItem(array: ["one", "two", "three", "four", "five"], position: 3)
_arraySearch(array: ["uno", "dos", "tres", "cuatro", "cinco"], element: "tres")
_arrayUnique(array: ["uno", "dos", "uno", "tres", "cuatro", "dos", "cinco", "dos"])
_arrayMerge(arrays: [["uno", "dos", "uno"], ["tres", "cuatro", "dos", "cinco", "dos"]])
_arrayDiff(arrays: [["uno", "dos"], ["tres", "cuatro", "dos"]])
_arrayAddItem(array: ["uno", "dos"], value: "tres")
_arraySetItem(array: ["uno", "dos"], index: 0, value: "tres")
_arrayKeys(array: ["uno", "dos", "tres"])
_arrayLength(array: ["uno", "dos", "tres"])

_strReplace(search: "https://", replaceWith: "http://", in: "https://gatographql.com")
_strReplaceMultiple(search: ["https://", "gato"], replaceWith: ["http://", "dog"], in: "https://gatographql.com")
_strRegexReplace(searchRegex: "/^https?:\\/\\//", replaceWith: "", in: "https://gatographql.com")
_strRegexReplaceMultiple(searchRegex: ["/^https?:\\/\\//", "/([a-z]*)/"], replaceWith: ["", "$1$1"], in: "https://gatographql.com")

_strStartsWith(search: "orld", in: "Hello world")
_strEndsWith(search: "orld", in: "Hello world")
_strUpperCase(text: "Hello world")
_strLowerCase(text: "Hello world")
_strTitleCase(text: "Hello world")


falseToTrue: _echo(value: false) @boolOpposite
trueToFalse: _echo(value: true) @boolOpposite
plusOne: _echo(value: 2) @intAdd(number: 1)
objectAddEntry: _echo(value: {
user: "Leo",
contact: {
email: "leo@test.com"
}
})
@objectAddEntry(key: "phone", value: "+0929094229", underPath: "contact")
@objectAddEntry(key: "methods", value: {}, underPath: "contact")
@objectAddEntry(key: "card", value: true, underPath: "contact.methods")
upperCase: _echo(value: "Hello world") @strUpperCase
lowerCase: _echo(value: "Hello world") @strLowerCase
titleCase: _echo(value: "Hello world") @strTitleCase
append: _echo(value: "Hello world") @strAppend(string: "!!!")
prepend: _echo(value: "Hello world") @strPrepend(string: "!!!")
arraySplice: _echo(value: ["uno", "dos", "tres"]) @arraySplice(offset: 1)
arraySpliceWithLength: _echo(value: ["uno", "dos", "tres"]) @arraySplice(offset: 1, length: 1)
arraySpliceWithReplacement: _echo(value: ["uno", "dos", "tres"]) @arraySplice(offset: 1, replacement: ["cuatro", "cinco"])
arraySpliceWithLengthAndReplacement: _echo(value: ["uno", "dos", "tres"]) @arraySplice(offset: 1, length: 1, replacement: ["cuatro", "cinco"])
arrayUnique: _echo(value: ["uno", "dos", "uno", "tres", "cuatro", "dos", "cinco", "dos"]) @arrayUnique
arrayMerge: _echo(value: ["uno", "dos", "uno"]) @arrayMerge(with: ["tres", "cuatro", "dos", "cinco", "dos"])
arrayDiff: _echo(value: ["uno", "dos"]) @arrayDiff (against: ["tres", "cuatro", "dos"])
arrayFilter: _echo(value: ["uno", "dos", null, "tres", "", "dos", []]) @arrayFilter
objectKeepProperties: _echo(value: { user: "Leo", email: "leo@test.com" } )
@objectKeepProperties(
keys: ["user"]
)
}

...produces:

{
"data": {
"_intAdd": 71,
"_intArraySum": 15,
"_arrayJoin": "Hello to everyone",
"_arrayItem": "four",
"_arraySearch": 2,
"_arrayUnique": [
"uno",
"dos",
"tres",
"cuatro",
"cinco"
],
"_arrayMerge": [
"uno",
"dos",
"uno",
"tres",
"cuatro",
"dos",
"cinco",
"dos"
],
"_arrayDiff": [
"uno"
],
"_arrayAddItem": [
"uno",
"dos",
"tres"
],
"_arraySetItem": [
"tres",
"dos"
],
"_arrayKeys": [
0,
1,
2
],
"_arrayLength": 3,
"_strReplace": "http://gatographql.com",
"_strReplaceMultiple": "http://doggraphql.com",
"_strRegexReplace": "gatographql.com",
"_strRegexReplaceMultiple": "gatographqlgatographql.comcom",
"_strStartsWith": false,
"_strEndsWith": true,
"_strUpperCase": "HELLO WORLD",
"_strLowerCase": "hello world",
"_strTitleCase": "Hello World",
"falseToTrue": true,
"trueToFalse": false,
"plusOne": 3,
"objectAddEntry": {
"user": "Leo",
"contact": {
email: "leo@test.com",
"phone": "+0929094229",
"methods": {
"card": true
}
}
},
"upperCase": "HELLO WORLD",
"lowerCase": "hello world",
"titleCase": "Hello World",
"append": "Hello world!!!",
"prepend": "!!!Hello world",
"arraySplice": [
"uno"
],
"arraySpliceWithLength": [
"uno",
"tres"
],
"arraySpliceWithReplacement": [
"uno",
"cuatro",
"cinco"
],
"arraySpliceWithLengthAndReplacement": [
"uno",
"cuatro",
"cinco",
"tres"
],
"arrayUnique": [
"uno",
"dos",
"tres",
"cuatro",
"cinco"
],
"arrayMerge": [
"uno",
"dos",
"uno",
"tres",
"cuatro",
"dos",
"cinco",
"dos"
],
"arrayDiff": [
"uno"
],
"arrayFilter": [
"uno",
"dos",
"tres",
"dos"
],
"objectKeepProperties": {
"user": "Leo"
}
}
}

Function Fields permalink

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.

This is the list of function fields.

_and permalink

Return an AND operation among several boolean properties.

_arrayAddItem permalink

Adds an element to the array.

_arrayCombine permalink

Create a JSON object using the elements from an array as keys, and the elements from another array as values.

_arrayChunk permalink

Split an array into chunks.

_arrayDiff permalink

Return an array containing all the elements from the first array which are not present on any of the other arrays.

_arrayFill permalink

Create an array filled with values.

_arrayInnerJoinJSONObjectProperties permalink

Fill the JSON objects inside a target array with properties from a JSON object from a source array, where a certain property is the same for both objects.

_arrayItem permalink

Access the element on the given position in the array.

_arrayJoin permalink

Join all the strings in an array, using a provided separator.

_arrayKeys permalink

Keys in an array.

_arrayLength permalink

Number of elements in an array.

_arrayMerge permalink

Merge two or more arrays together.

_arrayPad permalink

Pad an array to the specified length with a value.

_arrayRandom permalink

Randomly select one element from the provided ones.

_arrayRemoveFirst permalink

Remove the first element in the array.

_arrayRemoveLast permalink

Remove the last element in the array.

_arrayReverse permalink

Reverse an array.

_arraySearch permalink

Search in what position is an element placed in the array. If found, it returns its position, otherwise it returns false.

_arraySetItem permalink

Sets an element on some position of the array.

_arraySlice permalink

Extract a slice of an array.

_arraySplice permalink

Remove a portion of an array and replace it with something else.

_arrayUnique permalink

Filters out all duplicated elements in the array.

_date permalink

Returns a string formatted according to the given format string using the given integer timestamp (Unix timestamp) or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time() (provided via field _time).

_echo permalink

Repeat back the input, whatever it is.

_equals permalink

Indicate if the result from a field equals a certain value.

_floatCeil permalink

Rounds up a number to the next highest integer.

_floatDivide permalink

Divide a number by another number.

_greaterThan permalink

Indicate if number1 > number2.

_greaterThanOrEquals permalink

Indicate if number1 >= number2.

_if permalink

If a boolean property is true, execute a field, else, execute another field.

_inArray permalink

Indicate if the array contains the value

_intAdd permalink

Add an integer to another integer number.

_intArraySum permalink

Sum of the integer elements in the array.

_intMultiply permalink

Multiple an integer with another integer number.

_intSubstract permalink

Substract an integer from another integer number.

_isEmpty permalink

Indicate if a value is empty.

_isNull permalink

Indicate if a value is null.

_lowerThan permalink

Indicate if number1 < number2.

_lowerThanOrEquals permalink

Indicate if number1 <= number2.

_makeTime permalink

Returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Any optional arguments omitted or null will be set to the current value according to the local date and time.

_not permalink

Return the opposite value of a boolean property.

_notEmpty permalink

Indicate if the value is not empty.

_notEquals permalink

Are the two values not equal to each other.

_notInArray permalink

Indicate if the array does not contain the value.

_notNull permalink

Indicate if the value is not null.

_objectAddEntry permalink

Adds an entry to the object.

_objectKeepProperties permalink

Keeps specific properties only in the JSON object.

_objectProperties permalink

Retrieve the properties in a JSON object.

_objectProperty permalink

Retrieve a property from a JSON object.

_objectRemoveEntry permalink

Removes an entry from the JSON object.

_objectRemoveProperties permalink

Removes one or more entries from the JSON object.

_objectValues permalink

Retrieve the values in a JSON object.

_or permalink

Return an OR operation among several boolean properties.

_propertyExistsInJSONObject permalink

Indicate if a property exists on a JSON object.

_propertyIsSetInJSONObject permalink

Indicate if a property exists and is not null on a JSON object.

_sprintf permalink

Replace placeholders inside a string with provided values.

_strAppend permalink

Append a string to another string.

_strContains permalink

Indicates if a string contains another string.

_strDecodeJSONObject permalink

Decode a string into a JSON object, or return null if it is not possible.

_strDecodeList permalink

Decode a string into an array (of any type), or return null if it is not possible.

_strEndsWith permalink

Indicates if a string ends with another string.

_strLength permalink

Length of the string

_strLowerCase permalink

Transform a string to lower case.

_strPad permalink

Pad a string to a certain length with another string

_strPos permalink

Position of a substring within the string, or null if not found

_strRegexReplace permalink

Execute a regular expression to search and replace a string.

_strRegexReplaceMultiple permalink

Execute regular expressions to search and replace strings.

_strRepeat permalink

Repeat a string

_strReplace permalink

Replace a string with another string.

_strReplaceMultiple permalink

Replace a list of strings with another list of strings.

_strReverse permalink

Reverse a string.

_strShuffle permalink

Randomly shuffles a string

_strStartsWith permalink

Indicates if a string starts with another string.

_strStripSlashes permalink

Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash (\).

_strSubstr permalink

Return part of a string

_strTitleCase permalink

Transform a string to title case.

_strToTime permalink

Parse about any English textual datetime description into a Unix timestamp

_strTrim permalink

Strip whitespace (or other characters) from the beginning and end of a string.

_strUpperCase permalink

Transform a string to upper case.

_strWordCount permalink

Number of words in the string

_time permalink

Return the time now.

Function Directives permalink

This is the list of function directives.

@arrayAddItem permalink

Adds an element to the array.

@arrayDiff permalink

Computes the difference with another array.

@arrayFilter permalink

Filters out the null or empty elements in the array.

@arrayMerge permalink

Merge the array with another array.

@arrayPad permalink

Pad an array to the specified length with a value.

@arrayRemoveFirst permalink

Remove the first element in the array.

@arrayRemoveLast permalink

Remove the last element in the array.

@arrayReverse permalink

Reverse an array.

@arraySetItem permalink

Sets an element on some position of the array.

@arraySlice permalink

Extract a slice of an array.

@arraySplice permalink

Remove a portion of an array and replace it with something else.

@arrayUnique permalink

Filters out all duplicated elements in the array.

@boolOpposite permalink

Convert a bool to its opposite value.

@floatDivide permalink

Divide the field value by a float number.

@intAdd permalink

Add an integer number to the field value.

@intMultiply permalink

Multiply an integer number with the field value.

@intSubstract permalink

Substract an integer number from the field value.

@objectAddEntry permalink

Add an entry to the JSON object.

@objectKeepProperties permalink

Keep specific properties only from the JSON object.

@objectRemoveEntry permalink

Removes an entry from the JSON object.

@objectRemoveProperties permalink

Remove specific properties from the JSON object.

@setNull permalink

Set the field's value as null.

@strAppend permalink

Append some string to the end of the string in the field value.

@strLowerCase permalink

Convert a string to lower case.

@strPad permalink

Pad a string to a certain length with another string.

@strPrepend permalink

Append some string to the beginning of the string in the field value.

@strRegexReplace permalink

Execute a regular expression to search and replace a string (see documentation for PHP function preg_replace).

@strRegexReplaceMultiple permalink

Execute regular expressions to search and replace a list of strings (see documentation for PHP function preg_replace).

@strRepeat permalink

Repeat a string.

@strReplace permalink

Replace a string with another string.

@strReplaceMultiple permalink

Replace a list of strings with another list of strings.

@strReverse permalink

Reverse a string.

@strShuffle permalink

Randomly shuffles a string.

@strStripSlashes permalink

Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash .

@strSubstr permalink

Return part of a string.

@strTitleCase permalink

Convert a string to title case.

@strTrim permalink

Strip whitespace (or other characters) from the beginning and end of a string.

@strUpperCase permalink

Convert a string to upper case.

Examples permalink

Function Fields permalink

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:

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

We can apply function fields multiple times to perform a more complex calculation, such as generating a summary field based on the values from other fields:

{
posts {
id
content @remove
shortContent: _strSubstr(string: $__content, offset: 0, length: 150) @remove
excerpt @remove
isExcerptEmpty: _isEmpty(value: $__excerpt) @remove
summary: _if(
condition: $__isExcerptEmpty
then: $__content
else: $__excerpt
)
}
}

In combination with the HTTP Client extension, we can dynamically generate an API endpoint to connect to (based on the data on our site), and then extract some specific field from the returned data:

{
users(
pagination: { limit: 2 },
sort: { order: ASC, by: ID }
) {
id

# Dynamically generate endpoint for the user
endpoint: _arrayJoin(values: [
"https://newapi.getpop.org/wp-json/wp/v2/users/",
$__id,
"?_fields=name,avatar_urls"
])

# Retrieve the endpoint data
endpointData: _sendJSONObjectItemHTTPRequest(input: { url: $__endpoint } )

# Extract specific information
userAvatar: _objectProperty(
object: $__endpointData,
by: {
path: "avatar_urls.48"
}
)
}
}

...producing:

{
"data": {
"users": [
{
"id": 1,
"endpoint": "https://newapi.getpop.org/wp-json/wp/v2/users/1?_fields=name,avatar_urls",
"endpointData": {
"name": "leo",
"avatar_urls": {
"24": "https://secure.gravatar.com/avatar/b28085726ee66e49f08be16ad668efd5?s=24&d=mm&r=g",
"48": "https://secure.gravatar.com/avatar/b28085726ee66e49f08be16ad668efd5?s=48&d=mm&r=g",
"96": "https://secure.gravatar.com/avatar/b28085726ee66e49f08be16ad668efd5?s=96&d=mm&r=g"
},
"_links": {
"self": [
{
"href": "https://newapi.getpop.org/wp-json/wp/v2/users/1"
}
],
"collection": [
{
"href": "https://newapi.getpop.org/wp-json/wp/v2/users"
}
]
}
},
"userAvatar": "https://secure.gravatar.com/avatar/b28085726ee66e49f08be16ad668efd5?s=48&d=mm&r=g"
},
{
"id": 2,
"endpoint": "https://newapi.getpop.org/wp-json/wp/v2/users/2?_fields=name,avatar_urls",
"endpointData": {
"name": "themedemos",
"avatar_urls": {
"24": "https://secure.gravatar.com/avatar/7554514b65216821eeacde0fdcd6c6e6?s=24&d=mm&r=g",
"48": "https://secure.gravatar.com/avatar/7554514b65216821eeacde0fdcd6c6e6?s=48&d=mm&r=g",
"96": "https://secure.gravatar.com/avatar/7554514b65216821eeacde0fdcd6c6e6?s=96&d=mm&r=g"
},
"_links": {
"self": [
{
"href": "https://newapi.getpop.org/wp-json/wp/v2/users/2"
}
],
"collection": [
{
"href": "https://newapi.getpop.org/wp-json/wp/v2/users"
}
]
}
},
"userAvatar": "https://secure.gravatar.com/avatar/7554514b65216821eeacde0fdcd6c6e6?s=48&d=mm&r=g"
}
]
}
}

Function Directives permalink

If this query:

query {
posts {
title
}
}

...produces these results:

{
"data": {
"posts": [
{
"title": "Hello world!"
},
{
"title": "lovely weather"
}
]
}
}

...then this query:

query {
posts {
title @strUpperCase
}
}

...will produce:

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

Bundles including extension permalink

Recipes using extension permalink


🛍️ Buy “PHP Functions via Schema” Extension