PHP Functions via Schema
This extension adds fields and directives to the GraphQL schema which expose functionalities commonly found in programming languages (such as PHP).
Description
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:
...produces:
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.
This is the list of function fields.
_and
Return an AND
operation among several boolean properties.
_arrayAddItem
Adds an element to the array.
_arrayCombine
Create a JSON object using the elements from an array as keys, and the elements from another array as values.
_arrayChunk
Split an array into chunks.
_arrayDiff
Return an array containing all the elements from the first array which are not present on any of the other arrays.
_arrayFill
Create an array filled with values.
_arrayFlipToObject
Exchanges all numeric keys with their associated values in an array, returning an object.
_arrayInnerJoinJSONObjectProperties
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
Access the element on the given position in the array.
_arrayJoin
Join all the strings in an array, using a provided separator.
_arrayKeys
Keys in an array.
_arrayLength
Number of elements in an array.
_arrayMerge
Merge two or more arrays together.
_arrayPad
Pad an array to the specified length with a value.
_arrayRandom
Randomly select one element from the provided ones.
_arrayRemoveFirst
Remove the first element in the array.
_arrayRemoveLast
Remove the last element in the array.
_arrayReverse
Reverse an array.
_arraySearch
Search in what position is an element placed in the array. If found, it returns its position, otherwise it returns false
.
_arraySetItem
Sets an element on some position of the array.
_arraySlice
Extract a slice of an array.
_arraySplice
Remove a portion of an array and replace it with something else.
_arrayUnique
Filters out all duplicated elements in the array.
_date
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
Repeat back the input, whatever it is.
_equals
Indicate if the result from a field equals a certain value.
_floatCeil
Rounds up a number to the next highest integer.
_floatDivide
Divide a number by another number.
_greaterThan
Indicate if number1 > number2.
_greaterThanOrEquals
Indicate if number1 >= number2.
_if
If a boolean property is true, execute a field, else, execute another field.
_inArray
Indicate if the array contains the value
_intAdd
Add an integer to another integer number.
_intArraySum
Sum of the integer elements in the array.
_intMultiply
Multiple an integer with another integer number.
_intSubtract
Subtract an integer from another integer number.
_isEmpty
Indicate if a value is empty.
_isNull
Indicate if a value is null.
_lowerThan
Indicate if number1 < number2.
_lowerThanOrEquals
Indicate if number1 <= number2.
_makeTime
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
Return the opposite value of a boolean property.
_notEmpty
Indicate if the value is not empty.
_notEquals
Are the two values not equal to each other.
_notInArray
Indicate if the array does not contain the value.
_notNull
Indicate if the value is not null
.
_objectAddEntry
Adds an entry to the object.
_objectIntersectKey
Computes the intersection of objects using keys for comparison.
_objectKeepProperties
Keeps specific properties only in the JSON object.
_objectProperties
Retrieve the properties in a JSON object.
_objectProperty
Retrieve a property from a JSON object.
_objectRemoveEntry
Removes an entry from the JSON object.
_objectRemoveProperties
Removes one or more entries from the JSON object.
_objectValues
Retrieve the values in a JSON object.
_or
Return an OR
operation among several boolean properties.
_propertyExistsInJSONObject
Indicate if a property exists on a JSON object.
_propertyIsSetInJSONObject
Indicate if a property exists and is not null
on a JSON object.
_sprintf
Replace placeholders inside a string with provided values.
_strAppend
Append a string to another string.
_strContains
Indicates if a string contains another string.
_strDecodeJSONObject
Decode a string into a JSON object, or return null
if it is not possible.
_strDecodeList
Decode a string into an array (of any type), or return null
if it is not possible.
_strEndsWith
Indicates if a string ends with another string.
_strLength
Length of the string.
_strLowerCase
Transform a string to lower case.
_strPad
Pad a string to a certain length with another string.
_strPos
Position of a substring within the string, or null
if not found.
_strRegexFindMatches
Execute a regular expression to extract all matches from a string.
_strRegexReplace
Execute a regular expression to search and replace a string.
_strRegexReplaceMultiple
Execute regular expressions to search and replace strings.
_strRepeat
Repeat a string
_strReplace
Replace a string with another string.
_strReplaceMultiple
Replace a list of strings with another list of strings.
_strReverse
Reverse a string.
_strShuffle
Randomly shuffles a string
_strStartsWith
Indicates if a string starts with another string.
_strStripSlashes
Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash (\).
_strSubstr
Return part of a string
_strTitleCase
Transform a string to title case.
_strToTime
Parse about any English textual datetime description into a Unix timestamp
_strTrim
Strip whitespace (or other characters) from the beginning and end of a string.
_strUpperCase
Transform a string to upper case.
_strWordCount
Number of words in the string
_time
Return the time now.
Function Directives
This is the list of function directives.
@arrayAddItem
Adds an element to the array.
@arrayDiff
Computes the difference with another array.
@arrayFilter
Filters out the null or empty elements in the array.
@arrayMerge
Merge the array with another array.
@arrayPad
Pad an array to the specified length with a value.
@arrayRemoveFirst
Remove the first element in the array.
@arrayRemoveLast
Remove the last element in the array.
@arrayReverse
Reverse an array.
@arraySetItem
Sets an element on some position of the array.
@arraySlice
Extract a slice of an array.
@arraySplice
Remove a portion of an array and replace it with something else.
@arrayUnique
Filters out all duplicated elements in the array.
@boolOpposite
Convert a bool to its opposite value.
@floatDivide
Divide the field value by a float number.
@intAdd
Add an integer number to the field value.
@intMultiply
Multiply an integer number with the field value.
@intSubtract
Subtract an integer number from the field value.
@objectAddEntry
Add an entry to the JSON object.
@objectKeepProperties
Keep specific properties only from the JSON object.
@objectRemoveEntry
Removes an entry from the JSON object.
@objectRemoveProperties
Remove specific properties from the JSON object.
@setNull
Set the field's value as null
.
@strAppend
Append some string to the end of the string in the field value.
@strLowerCase
Convert a string to lower case.
@strPad
Pad a string to a certain length with another string.
@strPrepend
Append some string to the beginning of the string in the field value.
@strRegexReplace
Execute a regular expression to search and replace a string (see documentation for PHP function preg_replace
).
@strRegexReplaceMultiple
Execute regular expressions to search and replace a list of strings (see documentation for PHP function preg_replace
).
@strRepeat
Repeat a string.
@strReplace
Replace a string with another string.
@strReplaceMultiple
Replace a list of strings with another list of strings.
@strReverse
Reverse a string.
@strShuffle
Randomly shuffles a string.
@strStripSlashes
Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash .
@strSubstr
Return part of a string.
@strTitleCase
Convert a string to title case.
@strTrim
Strip whitespace (or other characters) from the beginning and end of a string.
@strUpperCase
Convert a string to upper case.
Examples
Function Fields
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:
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:
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:
...producing:
Function Directives
If this query:
...produces these results:
...then this query:
...will produce: