Documentation

AST
in package

Various utilities dealing with AST

Table of Contents

astFromValue()  : ObjectValueNode|ListValueNode|BooleanValueNode|IntValueNode|FloatValueNode|EnumValueNode|StringValueNode|NullValueNode|null
Produces a GraphQL Value AST given a PHP value.
fromArray()  : Node
Convert representation of AST as an associative array to instance of GraphQL\Language\AST\Node.
getOperation()  : bool|string
getOperationAST()  : OperationDefinitionNode|null
Returns the operation within a document by name.
toArray()  : array<string|int, mixed>
Convert AST node to serializable array
typeFromAST()  : Type|null
Returns type definition for given AST Type node
valueFromAST()  : array<string|int, mixed>|stdClass|null
Produces a PHP value given a GraphQL Value AST.
valueFromASTUntyped()  : mixed
Produces a PHP value given a GraphQL Value AST.
isMissingVariable()  : bool
Returns true if the provided valueNode is a variable which is not defined in the set of variables.

Methods

astFromValue()

Produces a GraphQL Value AST given a PHP value.

public static astFromValue(Type|mixed|null $value, InputType $type) : ObjectValueNode|ListValueNode|BooleanValueNode|IntValueNode|FloatValueNode|EnumValueNode|StringValueNode|NullValueNode|null

Optionally, a GraphQL type may be provided, which will be used to disambiguate between value primitives.

| PHP Value | GraphQL Value | | ------------- | -------------------- | | Object | Input Object | | Assoc Array | Input Object | | Array | List | | Boolean | Boolean | | String | String / Enum Value | | Int | Int | | Float | Int / Float | | Mixed | Enum Value | | null | NullValue |

Parameters
$value : Type|mixed|null
$type : InputType
Return values
ObjectValueNode|ListValueNode|BooleanValueNode|IntValueNode|FloatValueNode|EnumValueNode|StringValueNode|NullValueNode|null

fromArray()

Convert representation of AST as an associative array to instance of GraphQL\Language\AST\Node.

public static fromArray(array<string|int, mixed> $node) : Node

For example:

AST::fromArray([
    'kind' => 'ListValue',
    'values' => [
        ['kind' => 'StringValue', 'value' => 'my str'],
        ['kind' => 'StringValue', 'value' => 'my other str']
    ],
    'loc' => ['start' => 21, 'end' => 25]
]);

Will produce instance of ListValueNode where values prop is a lazily-evaluated NodeList returning instances of StringValueNode on access.

This is a reverse operation for AST::toArray($node)

Parameters
$node : array<string|int, mixed>
Return values
Node

getOperation()

public static getOperation(DocumentNode $document[, string $operationName = null ]) : bool|string
Parameters
$document : DocumentNode
$operationName : string = null
Tags
deprecated

use getOperationAST instead.

Returns operation type ("query", "mutation" or "subscription") given a document and operation name

Return values
bool|string

getOperationAST()

Returns the operation within a document by name.

public static getOperationAST(DocumentNode $document[, string|null $operationName = null ]) : OperationDefinitionNode|null

If a name is not provided, an operation is only returned if the document has exactly one.

Parameters
$document : DocumentNode
$operationName : string|null = null
Return values
OperationDefinitionNode|null

toArray()

Convert AST node to serializable array

public static toArray(Node $node) : array<string|int, mixed>
Parameters
$node : Node
Return values
array<string|int, mixed>

valueFromAST()

Produces a PHP value given a GraphQL Value AST.

public static valueFromAST(VariableNode|NullValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|EnumValueNode|ListValueNode|ObjectValueNode|null $valueNode, Type $type[, array<string|int, mixed>|null $variables = null ]) : array<string|int, mixed>|stdClass|null

A GraphQL type must be provided, which will be used to interpret different GraphQL Value literals.

Returns null when the value could not be validly coerced according to the provided type.

| GraphQL Value | PHP Value | | -------------------- | ------------- | | Input Object | Assoc Array | | List | Array | | Boolean | Boolean | | String | String | | Int / Float | Int / Float | | Enum Value | Mixed | | Null Value | null |

Parameters
$valueNode : VariableNode|NullValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|EnumValueNode|ListValueNode|ObjectValueNode|null
$type : Type
$variables : array<string|int, mixed>|null = null
Tags
throws
Exception
Return values
array<string|int, mixed>|stdClass|null

valueFromASTUntyped()

Produces a PHP value given a GraphQL Value AST.

public static valueFromASTUntyped(Node $valueNode[, array<string|int, mixed>|null $variables = null ]) : mixed

Unlike valueFromAST(), no type is provided. The resulting PHP value will reflect the provided GraphQL value AST.

| GraphQL Value | PHP Value | | -------------------- | ------------- | | Input Object | Assoc Array | | List | Array | | Boolean | Boolean | | String | String | | Int / Float | Int / Float | | Enum | Mixed | | Null | null |

Parameters
$valueNode : Node
$variables : array<string|int, mixed>|null = null
Tags
throws
Exception
Return values
mixed

isMissingVariable()

Returns true if the provided valueNode is a variable which is not defined in the set of variables.

private static isMissingVariable(VariableNode|NullValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|EnumValueNode|ListValueNode|ObjectValueNode $valueNode, array<string|int, mixed> $variables) : bool
Parameters
$valueNode : VariableNode|NullValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|EnumValueNode|ListValueNode|ObjectValueNode
$variables : array<string|int, mixed>
Return values
bool

Search results