Documentation

Parser
in package

Parses string containing GraphQL query or [type definition](type-system/type-language.md) to Abstract Syntax Tree.

Those magic functions allow partial parsing:

Table of Contents

$lexer  : Lexer
__callStatic()  : mixed
Parse partial source by delegating calls to the internal parseX methods.
__construct()  : mixed
argument()  : ArgumentNode
array()  : ListValueNode
constArgument()  : ArgumentNode
constArray()  : ListValueNode
constDirective()  : DirectiveNode
constObject()  : ObjectValueNode
constObjectField()  : ObjectFieldNode
constValue()  : BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|StringValueNode
constValueLiteral()  : BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|NullValueNode|ObjectValueNode|StringValueNode
definition()  : ExecutableDefinitionNode|TypeSystemDefinitionNode
description()  : StringValueNode|null
directive()  : DirectiveNode
directiveDefinition()  : DirectiveDefinitionNode
directiveLocation()  : NameNode
document()  : DocumentNode
enumTypeDefinition()  : EnumTypeDefinitionNode
enumTypeExtension()  : EnumTypeExtensionNode
enumValueDefinition()  : EnumValueDefinitionNode
executableDefinition()  : ExecutableDefinitionNode
field()  : FieldNode
fieldDefinition()  : FieldDefinitionNode
fragment()  : FragmentSpreadNode|InlineFragmentNode
fragmentDefinition()  : FragmentDefinitionNode
fragmentName()  : NameNode
inputObjectTypeDefinition()  : InputObjectTypeDefinitionNode
inputObjectTypeExtension()  : InputObjectTypeExtensionNode
inputValueDefinition()  : InputValueDefinitionNode
interfaceTypeDefinition()  : InterfaceTypeDefinitionNode
interfaceTypeExtension()  : InterfaceTypeExtensionNode
name()  : NameNode
namedType()  : NamedTypeNode
NodeList()  : void
NodeList()  : void
NodeList()  : void
NodeList()  : void
NodeList()  : void
NodeList()  : void
NodeList()  : void
NodeList()  : void
NodeList()  : void
NodeList()  : void
NodeList()  : void
NodeList()  : void
object()  : ObjectValueNode
objectField()  : ObjectFieldNode
objectTypeDefinition()  : ObjectTypeDefinitionNode
objectTypeExtension()  : ObjectTypeExtensionNode
operationDefinition()  : OperationDefinitionNode
operationType()  : string
operationTypeDefinition()  : OperationTypeDefinitionNode
parse()  : DocumentNode
Given a GraphQL source, parses it into a `GraphQL\Language\AST\DocumentNode`.
parseType()  : ListTypeNode|NamedTypeNode|NonNullTypeNode
Given a string containing a GraphQL Type (ex. `[Int!]`), parse the AST for that type.
parseValue()  : BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|ObjectValueNode|StringValueNode|VariableNode
Given a string containing a GraphQL value (ex. `[42]`), parse the AST for that value.
scalarTypeDefinition()  : ScalarTypeDefinitionNode
scalarTypeExtension()  : ScalarTypeExtensionNode
schemaDefinition()  : SchemaDefinitionNode
schemaTypeExtension()  : SchemaTypeExtensionNode
selection()  : mixed
selectionSet()  : SelectionSetNode
stringLiteral()  : StringValueNode
typeExtension()  : TypeExtensionNode
typeReference()  : ListTypeNode|NamedTypeNode|NonNullTypeNode
typeSystemDefinition()  : TypeSystemDefinitionNode
unionTypeDefinition()  : UnionTypeDefinitionNode
unionTypeExtension()  : UnionTypeExtensionNode
valueLiteral()  : BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|NullValueNode|ObjectValueNode|StringValueNode|VariableNode
variable()  : VariableNode
variableDefinition()  : VariableDefinitionNode
variableValue()  : BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|ObjectValueNode|StringValueNode|VariableNode
any()  : NodeList
Returns a possibly empty list of parse nodes, determined by the parseFn. This list begins with a lex token of openKind and ends with a lex token of closeKind. Advances the parser to the next lex token after the closing token.
expect()  : Token
If the next token is of the given kind, return that token after advancing the parser. Otherwise, do not change the parser state and return false.
expectKeyword()  : void
If the next token is a keyword with the given value, advance the lexer.
expectOptionalKeyword()  : bool
If the next token is a given keyword, return "true" after advancing the lexer. Otherwise, do not change the parser state and return "false".
loc()  : Location|null
Returns a location object, used to identify the place in the source that created a given parsed object.
many()  : NodeList
Returns a non-empty list of parse nodes, determined by the parseFn. This list begins with a lex token of openKind and ends with a lex token of closeKind. Advances the parser to the next lex token after the closing token.
parseArgument()  : ArgumentNode
parseArguments()  : NodeList
parseArgumentsDefinition()  : NodeList
parseArray()  : ListValueNode
parseConstArgument()  : ArgumentNode
parseConstValue()  : BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|StringValueNode|VariableNode
parseDefinition()  : ExecutableDefinitionNode|TypeSystemDefinitionNode
parseDescription()  : StringValueNode|null
parseDirective()  : DirectiveNode
parseDirectiveDefinition()  : DirectiveDefinitionNode
DirectiveDefinition : - Description? directive @ Name ArgumentsDefinition? `repeatable`? on DirectiveLocations
parseDirectiveLocation()  : NameNode
parseDirectiveLocations()  : NodeList
parseDirectives()  : NodeList
parseDocument()  : DocumentNode
Implements the parsing rules in the Document section.
parseEnumTypeDefinition()  : EnumTypeDefinitionNode
parseEnumTypeExtension()  : EnumTypeExtensionNode
parseEnumValueDefinition()  : EnumValueDefinitionNode
parseEnumValuesDefinition()  : NodeList
parseExecutableDefinition()  : ExecutableDefinitionNode
parseField()  : FieldNode
parseFieldDefinition()  : FieldDefinitionNode
parseFieldsDefinition()  : NodeList
parseFragment()  : FragmentSpreadNode|InlineFragmentNode
parseFragmentDefinition()  : FragmentDefinitionNode
parseFragmentName()  : NameNode
parseImplementsInterfaces()  : NodeList
ImplementsInterfaces : - implements `&`? NamedType - ImplementsInterfaces & NamedType
parseInputFieldsDefinition()  : NodeList
parseInputObjectTypeDefinition()  : InputObjectTypeDefinitionNode
parseInputObjectTypeExtension()  : InputObjectTypeExtensionNode
parseInputValueDefinition()  : InputValueDefinitionNode
parseInterfaceTypeDefinition()  : InterfaceTypeDefinitionNode
parseInterfaceTypeExtension()  : InterfaceTypeExtensionNode
parseName()  : NameNode
Converts a name lex token into a name parse node.
parseNamedType()  : NamedTypeNode
parseObject()  : ObjectValueNode
parseObjectField()  : ObjectFieldNode
parseObjectTypeDefinition()  : ObjectTypeDefinitionNode
parseObjectTypeExtension()  : ObjectTypeExtensionNode
parseOperationDefinition()  : OperationDefinitionNode
parseOperationType()  : string
parseOperationTypeDefinition()  : OperationTypeDefinitionNode
parseScalarTypeDefinition()  : ScalarTypeDefinitionNode
parseScalarTypeExtension()  : ScalarTypeExtensionNode
parseSchemaDefinition()  : SchemaDefinitionNode
parseSchemaTypeExtension()  : SchemaTypeExtensionNode
parseSelection()  : SelectionNode
Selection : - Field - FragmentSpread - InlineFragment
parseSelectionSet()  : SelectionSetNode
parseStringLiteral()  : StringValueNode
parseTypeExtension()  : TypeExtensionNode
TypeExtension : - ScalarTypeExtension - ObjectTypeExtension - InterfaceTypeExtension - UnionTypeExtension - EnumTypeExtension - InputObjectTypeDefinition
parseTypeReference()  : ListTypeNode|NamedTypeNode|NonNullTypeNode
Handles the Type: TypeName, ListType, and NonNullType parsing rules.
parseTypeSystemDefinition()  : TypeSystemDefinitionNode
TypeSystemDefinition : - SchemaDefinition - TypeDefinition - TypeExtension - DirectiveDefinition
parseUnionMemberTypes()  : NodeList
UnionMemberTypes : - = `|`? NamedType - UnionMemberTypes | NamedType
parseUnionTypeDefinition()  : UnionTypeDefinitionNode
UnionTypeDefinition : - Description? union Name Directives[Const]? UnionMemberTypes?
parseUnionTypeExtension()  : UnionTypeExtensionNode
UnionTypeExtension : - extend union Name Directives[Const]? UnionMemberTypes - extend union Name Directives[Const]
parseValueLiteral()  : BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|StringValueNode|VariableNode|ListValueNode|ObjectValueNode|NullValueNode
Value[Const] : - [~Const] Variable - IntValue - FloatValue - StringValue - BooleanValue - NullValue - EnumValue - ListValue[?Const] - ObjectValue[?Const]
parseVariable()  : VariableNode
parseVariableDefinition()  : VariableDefinitionNode
parseVariableDefinitions()  : NodeList
parseVariableValue()  : BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|ObjectValueNode|StringValueNode|VariableNode
peek()  : bool
Determines if the next token is of a given kind
peekDescription()  : bool
skip()  : bool
If the next token is of the given kind, return true after advancing the parser. Otherwise, do not change the parser state and return false.
unexpected()  : SyntaxError

Properties

Methods

__callStatic()

Parse partial source by delegating calls to the internal parseX methods.

public static __callStatic(string $name, array<string|int, bool> $arguments) : mixed
Parameters
$name : string
$arguments : array<string|int, bool>
Tags
throws
SyntaxError
Return values
mixed

__construct()

public __construct(Source|string $source[, array<string|int, bool> $options = [] ]) : mixed
Parameters
$source : Source|string
$options : array<string|int, bool> = []
Return values
mixed

argument()

public static argument(Source|string $source, array<string|int, bool> $options = []) : ArgumentNode
Parameters
$source : Source|string
$options = [] : array<string|int, bool>
Return values
ArgumentNode

constArgument()

public static constArgument(Source|string $source, array<string|int, bool> $options = []) : ArgumentNode
Parameters
$source : Source|string
$options = [] : array<string|int, bool>
Return values
ArgumentNode

constArray()

public static constArray(Source|string $source, array<string|int, bool> $options = []) : ListValueNode
Parameters
$source : Source|string
$options = [] : array<string|int, bool>
Return values
ListValueNode

constDirective()

public static constDirective(Source|string $source, array<string|int, bool> $options = []) : DirectiveNode
Parameters
$source : Source|string
$options = [] : array<string|int, bool>
Return values
DirectiveNode

directiveLocation()

public static directiveLocation(Source|string $source, array<string|int, bool> $options = []) : NameNode
Parameters
$source : Source|string
$options = [] : array<string|int, bool>
Return values
NameNode

document()

public static document(Source|string $source, array<string|int, bool> $options = []) : DocumentNode
Parameters
$source : Source|string
$options = [] : array<string|int, bool>
Return values
DocumentNode

field()

public static field(Source|string $source, array<string|int, bool> $options = []) : FieldNode
Parameters
$source : Source|string
$options = [] : array<string|int, bool>
Return values
FieldNode

fragmentName()

public static fragmentName(Source|string $source, array<string|int, bool> $options = []) : NameNode
Parameters
$source : Source|string
$options = [] : array<string|int, bool>
Return values
NameNode

name()

public static name(Source|string $source, array<string|int, bool> $options = []) : NameNode
Parameters
$source : Source|string
$options = [] : array<string|int, bool>
Return values
NameNode

NodeList()

public static NodeList() : void

<VariableDefinitionNode> variableDefinitions(Source|string $source, bool[] $options = [])

Return values
void

NodeList()

public static NodeList() : void

<ArgumentNode> arguments(Source|string $source, bool[] $options = [])

Return values
void

NodeList()

public static NodeList() : void

<ArgumentNode> constArguments(Source|string $source, bool[] $options = [])

Return values
void

NodeList()

public static NodeList() : void

<DirectiveNode> directives(Source|string $source, bool[] $options = [])

Return values
void

NodeList()

public static NodeList() : void

<DirectiveNode> constDirectives(Source|string $source, bool[] $options = [])

Return values
void

NodeList()

public static NodeList() : void

<NamedTypeNode> implementsInterfaces(Source|string $source, bool[] $options = [])

Return values
void

NodeList()

public static NodeList() : void

<FieldDefinitionNode> fieldsDefinition(Source|string $source, bool[] $options = [])

Return values
void

NodeList()

public static NodeList() : void

<InputValueDefinitionNode> argumentsDefinition(Source|string $source, bool[] $options = [])

Return values
void

NodeList()

public static NodeList() : void

<NamedTypeNode> unionMemberTypes(Source|string $source, bool[] $options = [])

Return values
void

NodeList()

public static NodeList() : void

<EnumValueDefinitionNode> enumValuesDefinition(Source|string $source, bool[] $options = [])

Return values
void

NodeList()

public static NodeList() : void

<InputValueDefinitionNode> inputFieldsDefinition(Source|string $source, bool[] $options = [])

Return values
void

NodeList()

public static NodeList() : void

<NameNode> directiveLocations(Source|string $source, bool[] $options = [])

Return values
void

operationType()

public static operationType(Source|string $source, array<string|int, bool> $options = []) : string
Parameters
$source : Source|string
$options = [] : array<string|int, bool>
Return values
string

parse()

Given a GraphQL source, parses it into a `GraphQL\Language\AST\DocumentNode`.

public static parse(Source|string $source[, array<string|int, bool> $options = [] ]) : DocumentNode

Throws GraphQL\Error\SyntaxError if a syntax error is encountered.

Available options:

noLocation: boolean, (By default, the parser creates AST nodes that know the location in the source that they correspond to. This configuration flag disables that behavior for performance or testing.)

allowLegacySDLEmptyFields: boolean If enabled, the parser will parse empty fields sets in the Schema Definition Language. Otherwise, the parser will follow the current specification.

This option is provided to ease adoption of the final SDL specification and will be removed in a future major release.

allowLegacySDLImplementsInterfaces: boolean If enabled, the parser will parse implemented interfaces with no & character between each interface. Otherwise, the parser will follow the current specification.

This option is provided to ease adoption of the final SDL specification and will be removed in a future major release.

experimentalFragmentVariables: boolean, (If enabled, the parser will understand and parse variable definitions contained in a fragment definition. They'll be represented in the variableDefinitions field of the FragmentDefinitionNode.

The syntax is identical to normal, query-defined variables. For example:

fragment A($var: Boolean = false) on T  {
  ...
}

Note: this feature is experimental and may change or be removed in the future.)

Parameters
$source : Source|string
$options : array<string|int, bool> = []
Tags
throws
SyntaxError
Return values
DocumentNode

parseType()

Given a string containing a GraphQL Type (ex. `[Int!]`), parse the AST for that type.

public static parseType(Source|string $source[, array<string|int, bool> $options = [] ]) : ListTypeNode|NamedTypeNode|NonNullTypeNode

Throws GraphQL\Error\SyntaxError if a syntax error is encountered.

This is useful within tools that operate upon GraphQL Types directly and in isolation of complete GraphQL documents.

Consider providing the results to the utility function: GraphQL\Utils\AST::typeFromAST().

Parameters
$source : Source|string
$options : array<string|int, bool> = []
Return values
ListTypeNode|NamedTypeNode|NonNullTypeNode

parseValue()

Given a string containing a GraphQL value (ex. `[42]`), parse the AST for that value.

public static parseValue(Source|string $source[, array<string|int, bool> $options = [] ]) : BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|ObjectValueNode|StringValueNode|VariableNode

Throws GraphQL\Error\SyntaxError if a syntax error is encountered.

This is useful within tools that operate upon GraphQL Values directly and in isolation of complete GraphQL documents.

Consider providing the results to the utility function: GraphQL\Utils\AST::valueFromAST().

Parameters
$source : Source|string
$options : array<string|int, bool> = []
Return values
BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|ObjectValueNode|StringValueNode|VariableNode

selection()

public static selection(Source|string $source, array<string|int, bool> $options = []) : mixed
Parameters
$source : Source|string
$options = [] : array<string|int, bool>
Return values
mixed

variable()

public static variable(Source|string $source, array<string|int, bool> $options = []) : VariableNode
Parameters
$source : Source|string
$options = [] : array<string|int, bool>
Return values
VariableNode

any()

Returns a possibly empty list of parse nodes, determined by the parseFn. This list begins with a lex token of openKind and ends with a lex token of closeKind. Advances the parser to the next lex token after the closing token.

private any(string $openKind, callable $parseFn, string $closeKind) : NodeList
Parameters
$openKind : string
$parseFn : callable
$closeKind : string
Tags
throws
SyntaxError
Return values
NodeList

expect()

If the next token is of the given kind, return that token after advancing the parser. Otherwise, do not change the parser state and return false.

private expect(string $kind) : Token
Parameters
$kind : string
Tags
throws
SyntaxError
Return values
Token

expectKeyword()

If the next token is a keyword with the given value, advance the lexer.

private expectKeyword(string $value) : void

Otherwise, throw an error.

Parameters
$value : string
Tags
throws
SyntaxError
Return values
void

expectOptionalKeyword()

If the next token is a given keyword, return "true" after advancing the lexer. Otherwise, do not change the parser state and return "false".

private expectOptionalKeyword(string $value) : bool
Parameters
$value : string
Return values
bool

loc()

Returns a location object, used to identify the place in the source that created a given parsed object.

private loc(Token $startToken) : Location|null
Parameters
$startToken : Token
Return values
Location|null

many()

Returns a non-empty list of parse nodes, determined by the parseFn. This list begins with a lex token of openKind and ends with a lex token of closeKind. Advances the parser to the next lex token after the closing token.

private many(string $openKind, callable $parseFn, string $closeKind) : NodeList
Parameters
$openKind : string
$parseFn : callable
$closeKind : string
Tags
throws
SyntaxError
Return values
NodeList

parseImplementsInterfaces()

ImplementsInterfaces : - implements `&`? NamedType - ImplementsInterfaces & NamedType

private parseImplementsInterfaces() : NodeList
Return values
NodeList

parseOperationType()

private parseOperationType() : string
Tags
throws
SyntaxError
Return values
string

parseTypeExtension()

TypeExtension : - ScalarTypeExtension - ObjectTypeExtension - InterfaceTypeExtension - UnionTypeExtension - EnumTypeExtension - InputObjectTypeDefinition

private parseTypeExtension() : TypeExtensionNode
Tags
throws
SyntaxError
Return values
TypeExtensionNode

parseTypeSystemDefinition()

TypeSystemDefinition : - SchemaDefinition - TypeDefinition - TypeExtension - DirectiveDefinition

private parseTypeSystemDefinition() : TypeSystemDefinitionNode

TypeDefinition :

  • ScalarTypeDefinition
  • ObjectTypeDefinition
  • InterfaceTypeDefinition
  • UnionTypeDefinition
  • EnumTypeDefinition
  • InputObjectTypeDefinition
Tags
throws
SyntaxError
Return values
TypeSystemDefinitionNode

parseUnionMemberTypes()

UnionMemberTypes : - = `|`? NamedType - UnionMemberTypes | NamedType

private parseUnionMemberTypes() : NodeList
Return values
NodeList

parseValueLiteral()

Value[Const] : - [~Const] Variable - IntValue - FloatValue - StringValue - BooleanValue - NullValue - EnumValue - ListValue[?Const] - ObjectValue[?Const]

private parseValueLiteral(bool $isConst) : BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|StringValueNode|VariableNode|ListValueNode|ObjectValueNode|NullValueNode

BooleanValue : one of true false

NullValue : null

EnumValue : Name but not true, false or null

Parameters
$isConst : bool
Tags
throws
SyntaxError
Return values
BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|StringValueNode|VariableNode|ListValueNode|ObjectValueNode|NullValueNode

peek()

Determines if the next token is of a given kind

private peek(string $kind) : bool
Parameters
$kind : string
Return values
bool

peekDescription()

private peekDescription() : bool
Return values
bool

skip()

If the next token is of the given kind, return true after advancing the parser. Otherwise, do not change the parser state and return false.

private skip(string $kind) : bool
Parameters
$kind : string
Return values
bool

Search results