Parse, print, and visit GraphQL language source files and AST nodes.
These exports are also available from the root graphql package.
For documentation purposes, these exports are grouped into the following categories:
Category: AST
Types:
ASTNodeASTKindToNodeNameNodeDocumentNodeDefinitionNodeExecutableDefinitionNodeOperationDefinitionNodeSubscriptionOperationDefinitionNodeVariableDefinitionNodeVariableNodeSelectionSetNodeSelectionNodeFieldNodeArgumentNodeConstArgumentNodeFragmentArgumentNodeFragmentSpreadNodeInlineFragmentNodeFragmentDefinitionNodeValueNodeConstValueNodeIntValueNodeFloatValueNodeStringValueNodeBooleanValueNodeNullValueNodeEnumValueNodeListValueNodeConstListValueNodeObjectValueNodeConstObjectValueNodeObjectFieldNodeConstObjectFieldNodeDirectiveNodeConstDirectiveNodeTypeNodeNamedTypeNodeListTypeNodeNonNullTypeNodeTypeSystemDefinitionNodeSchemaDefinitionNodeOperationTypeDefinitionNodeTypeDefinitionNodeScalarTypeDefinitionNodeObjectTypeDefinitionNodeFieldDefinitionNodeInputValueDefinitionNodeInterfaceTypeDefinitionNodeUnionTypeDefinitionNodeEnumTypeDefinitionNodeEnumValueDefinitionNodeInputObjectTypeDefinitionNodeDirectiveDefinitionNodeTypeSystemExtensionNodeSchemaExtensionNodeTypeExtensionNodeScalarTypeExtensionNodeObjectTypeExtensionNodeInterfaceTypeExtensionNodeUnionTypeExtensionNodeEnumTypeExtensionNodeInputObjectTypeExtensionNodeDirectiveExtensionNodeSchemaCoordinateNodeTypeCoordinateNodeMemberCoordinateNodeArgumentCoordinateNodeDirectiveCoordinateNodeDirectiveArgumentCoordinateNode
Classes
Location
Contains a range of UTF-8 character offsets and token references that identify the region of the source from which the AST derived.
Constructor
Creates a Location instance.
Signature:
new Location(
startToken: Token,
endToken: Token,
source: Source,
);
| Name | Type | Description |
|---|---|---|
| startToken | Token | The start token. |
| endToken | Token | The end token. |
| source | Source | Source document used to derive error locations. |
Members
| Name | Type | Description |
|---|---|---|
| start | number | The character offset at which this Node begins. |
| end | number | The character offset at which this Node ends. |
| startToken | Token | The Token at which this Node begins. |
| endToken | Token | The Token at which this Node ends. |
| source | Source | The Source document the AST represents. |
toJSON()
Returns a JSON representation of this location.
Signature:
toJSON(): {
start: number;
end: number;
};
| Type | Description |
|---|---|
{ start: number; end: number } | The JSON-serializable representation. |
import { parse } from 'graphql/language';
const document = parse('{ hello }');
const location = document.loc?.toJSON();
location; // => { start: 0, end: 9 }Token
Represents a range of characters represented by a lexical token within a Source.
Constructor
Creates a Token instance.
Signature:
new Token(
kind: TokenKind,
start: number,
end: number,
line: number,
column: number,
value?: string,
);
| Name | Type | Description |
|---|---|---|
| kind | TokenKind | Token kind produced by lexical analysis. |
| start | number | Character offset where this token begins. |
| end | number | Character offset where this token ends. |
| line | number | One-indexed line number where this token begins. |
| column | number | One-indexed column number where this token begins. |
| value? | string | Interpreted value for non-punctuation tokens. |
Members
| Name | Type | Description |
|---|---|---|
| kind | TokenKind | The kind of Token. |
| start | number | The character offset at which this Node begins. |
| end | number | The character offset at which this Node ends. |
| line | number | The 1-indexed line number on which this Token appears. |
| column | number | The 1-indexed column number at which this Token begins. |
| value | string | For non-punctuation tokens, represents the interpreted value of the token. Note: is undefined for punctuation tokens, but typed as string for convenience in the parser. |
| prev | Token | null | Tokens exist as nodes in a double-linked-list amongst all tokens including ignored tokens. <SOF> is always the first node and <EOF> the last. |
| next | Token | null | Next token in the token stream, including ignored tokens. |
toJSON()
Returns a JSON representation of this token.
Signature:
toJSON(): {
kind: TokenKind;
value?: string;
line: number;
column: number;
};
| Type | Description |
|---|---|
{
kind: TokenKind;
value?: string;
line: number;
column: number;
} | The JSON-serializable representation. |
import { Lexer, Source } from 'graphql/language';
const lexer = new Lexer(new Source('{ hello }'));
const token = lexer.advance().toJSON();
token; // => { kind: '{', value: undefined, line: 1, column: 1 }Types
ASTNode
Type alias. The list of all possible AST node types.
type ASTNode =
| NameNode
| DocumentNode
| OperationDefinitionNode
| VariableDefinitionNode
| VariableNode
| SelectionSetNode
| FieldNode
| ArgumentNode
| FragmentArgumentNode
| FragmentSpreadNode
| InlineFragmentNode
| FragmentDefinitionNode
| IntValueNode
| FloatValueNode
| StringValueNode
| BooleanValueNode
| NullValueNode
| EnumValueNode
| ListValueNode
| ObjectValueNode
| ObjectFieldNode
| DirectiveNode
| NamedTypeNode
| ListTypeNode
| NonNullTypeNode
| SchemaDefinitionNode
| OperationTypeDefinitionNode
| ScalarTypeDefinitionNode
| ObjectTypeDefinitionNode
| FieldDefinitionNode
| InputValueDefinitionNode
| InterfaceTypeDefinitionNode
| UnionTypeDefinitionNode
| EnumTypeDefinitionNode
| EnumValueDefinitionNode
| InputObjectTypeDefinitionNode
| DirectiveDefinitionNode
| SchemaExtensionNode
| ScalarTypeExtensionNode
| ObjectTypeExtensionNode
| InterfaceTypeExtensionNode
| UnionTypeExtensionNode
| EnumTypeExtensionNode
| InputObjectTypeExtensionNode
| DirectiveExtensionNode
| TypeCoordinateNode
| MemberCoordinateNode
| ArgumentCoordinateNode
| DirectiveCoordinateNode
| DirectiveArgumentCoordinateNode;
ASTKindToNode
Type alias. Utility type listing all nodes indexed by their kind.
type ASTKindToNode = mapped object;
NameNode
Interface. An identifier in a GraphQL document.
| Name | Type | Description |
|---|---|---|
| kind | 'Name' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| value | string | Parsed value represented by this node. |
DocumentNode
Interface. The root AST node for a parsed GraphQL document.
| Name | Type | Description |
|---|---|---|
| kind | 'Document' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| definitions | readonly DefinitionNode[] | Top-level executable and type-system definitions in this document. |
| tokenCount? | number | The number of lexical tokens parsed for this document, if token counting was enabled. |
DefinitionNode
Type alias. Any top-level definition that may appear in a GraphQL document.
type DefinitionNode =
| ExecutableDefinitionNode
| TypeSystemDefinitionNode
| TypeSystemExtensionNode;
ExecutableDefinitionNode
Type alias. Any executable definition that may appear in an operation document.
type ExecutableDefinitionNode = OperationDefinitionNode | FragmentDefinitionNode;
OperationDefinitionNode
Interface. A query, mutation, or subscription operation definition.
| Name | Type | Description |
|---|---|---|
| kind | 'OperationDefinition' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| description? | StringValueNode | The optional GraphQL description associated with this definition. |
| operation | OperationTypeNode | The operation selected for execution. |
| name? | NameNode | Name node identifying this AST node. |
| variableDefinitions? | readonly VariableDefinitionNode[] | Variable definitions declared by this operation or fragment. |
| directives? | readonly DirectiveNode[] | Directives available in this schema or applied to this AST node. |
| selectionSet | SelectionSetNode | Selections made by this operation, field, or fragment. |
SubscriptionOperationDefinitionNode
Interface. A narrowed OperationDefinitionNode for subscription operations. Subscription operations go through a distinct execution pipeline (source event stream + per-event execution), so narrowing the operation type allows functions in that pipeline to accept only valid input.
interface SubscriptionOperationDefinitionNode extends OperationDefinitionNode
| Name | Type | Description |
|---|---|---|
| operation | 'subscription' | Subscription operation kind for this definition. |
VariableDefinitionNode
Interface. A variable declaration in an operation or experimental fragment definition.
| Name | Type | Description |
|---|---|---|
| kind | 'VariableDefinition' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| description? | StringValueNode | The optional GraphQL description associated with this definition. |
| variable | VariableNode | The variable being defined or referenced. |
| type | TypeNode | The GraphQL type reference or runtime type for this element. |
| defaultValue? | ConstValueNode | Default value used when no explicit value is supplied. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
VariableNode
Interface. A variable reference, such as $id.
| Name | Type | Description |
|---|---|---|
| kind | 'Variable' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
SelectionSetNode
Interface. A set of fields and fragments selected from an object, interface, or union.
| Name | Type | Description |
|---|---|---|
| kind | 'SelectionSet' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| selections | readonly SelectionNode[] | Fields and fragments contained in this selection set. |
SelectionNode
Type alias. Any selection that may appear inside a selection set.
type SelectionNode =
| FieldNode
| FragmentSpreadNode
| InlineFragmentNode;
FieldNode
Interface. A field selected in an executable GraphQL document.
| Name | Type | Description |
|---|---|---|
| kind | 'Field' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| alias? | NameNode | The response-key alias for this field, if one was supplied. |
| name | NameNode | Name node identifying this AST node. |
| arguments? | readonly ArgumentNode[] | Arguments supplied to this field, directive, or coordinate. |
| directives? | readonly DirectiveNode[] | Directives available in this schema or applied to this AST node. |
| selectionSet? | SelectionSetNode | Selections made by this operation, field, or fragment. |
ArgumentNode
Interface. An argument supplied to a field or directive.
| Name | Type | Description |
|---|---|---|
| kind | 'Argument' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| value | ValueNode | Parsed value represented by this node. |
ConstArgumentNode
Interface. An argument node whose value is guaranteed to be constant.
| Name | Type | Description |
|---|---|---|
| kind | 'Argument' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| value | ConstValueNode | Parsed value represented by this node. |
FragmentArgumentNode
Interface. Variable definition declared by a fragment argument.
| Name | Type | Description |
|---|---|---|
| kind | 'FragmentArgument' | AST node kind for a fragment argument. |
| loc? | Location | Source location for this fragment argument. |
| name | NameNode | Variable name declared by this fragment argument. |
| value | ValueNode | Default value literal for this fragment argument, if provided. |
FragmentSpreadNode
Interface. A named fragment spread, such as ...userFields.
| Name | Type | Description |
|---|---|---|
| kind | 'FragmentSpread' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| arguments? | readonly FragmentArgumentNode[] | Argument values supplied to the referenced fragment. |
| directives? | readonly DirectiveNode[] | Directives available in this schema or applied to this AST node. |
InlineFragmentNode
Interface. An inline fragment spread with an optional type condition.
| Name | Type | Description |
|---|---|---|
| kind | 'InlineFragment' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| typeCondition? | NamedTypeNode | The type condition that limits where this fragment applies. |
| directives? | readonly DirectiveNode[] | Directives available in this schema or applied to this AST node. |
| selectionSet | SelectionSetNode | Selections made by this operation, field, or fragment. |
FragmentDefinitionNode
Interface. A reusable fragment definition declared in an executable document.
| Name | Type | Description |
|---|---|---|
| kind | 'FragmentDefinition' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| description? | StringValueNode | The optional GraphQL description associated with this definition. |
| name | NameNode | Name node identifying this AST node. |
| variableDefinitions? | readonly VariableDefinitionNode[] | Experimental variable definitions declared by this fragment definition. |
| typeCondition | NamedTypeNode | The type condition that limits where this fragment applies. |
| directives? | readonly DirectiveNode[] | Directives available in this schema or applied to this AST node. |
| selectionSet | SelectionSetNode | Selections made by this operation, field, or fragment. |
ValueNode
Type alias. Any value literal that may appear in an executable GraphQL document.
type ValueNode =
| VariableNode
| IntValueNode
| FloatValueNode
| StringValueNode
| BooleanValueNode
| NullValueNode
| EnumValueNode
| ListValueNode
| ObjectValueNode;
ConstValueNode
Type alias. Any value literal that is guaranteed not to contain a variable reference.
type ConstValueNode =
| IntValueNode
| FloatValueNode
| StringValueNode
| BooleanValueNode
| NullValueNode
| EnumValueNode
| ConstListValueNode
| ConstObjectValueNode;
IntValueNode
Interface. An integer value literal.
| Name | Type | Description |
|---|---|---|
| kind | 'IntValue' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| value | string | Parsed value represented by this node. |
FloatValueNode
Interface. A floating-point value literal.
| Name | Type | Description |
|---|---|---|
| kind | 'FloatValue' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| value | string | Parsed value represented by this node. |
StringValueNode
Interface. A string value literal.
| Name | Type | Description |
|---|---|---|
| kind | 'StringValue' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| value | string | Parsed value represented by this node. |
| block? | boolean | Whether this string was parsed from block string syntax. |
BooleanValueNode
Interface. A boolean value literal.
| Name | Type | Description |
|---|---|---|
| kind | 'BooleanValue' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| value | boolean | Parsed value represented by this node. |
NullValueNode
Interface. A null value literal.
| Name | Type | Description |
|---|---|---|
| kind | 'NullValue' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
EnumValueNode
Interface. An enum value literal.
| Name | Type | Description |
|---|---|---|
| kind | 'EnumValue' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| value | string | Parsed value represented by this node. |
ListValueNode
Interface. A list value literal.
| Name | Type | Description |
|---|---|---|
| kind | 'ListValue' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| values | readonly ValueNode[] | Values contained in this enum, list, or input-object definition. |
ConstListValueNode
Interface. A list value literal whose elements are all constant values.
| Name | Type | Description |
|---|---|---|
| kind | 'ListValue' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| values | readonly ConstValueNode[] | Values contained in this enum, list, or input-object definition. |
ObjectValueNode
Interface. An input object value literal.
| Name | Type | Description |
|---|---|---|
| kind | 'ObjectValue' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| fields | readonly ObjectFieldNode[] | Fields declared by this object, interface, input object, or literal. |
ConstObjectValueNode
Interface. An input object value literal whose fields are all constant values.
| Name | Type | Description |
|---|---|---|
| kind | 'ObjectValue' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| fields | readonly ConstObjectFieldNode[] | Fields declared by this object, interface, input object, or literal. |
ObjectFieldNode
Interface. A field inside an input object value literal.
| Name | Type | Description |
|---|---|---|
| kind | 'ObjectField' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| value | ValueNode | Parsed value represented by this node. |
ConstObjectFieldNode
Interface. A field inside a constant input object value literal.
| Name | Type | Description |
|---|---|---|
| kind | 'ObjectField' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| value | ConstValueNode | Parsed value represented by this node. |
DirectiveNode
Interface. A directive applied to an executable or type-system location.
| Name | Type | Description |
|---|---|---|
| kind | 'Directive' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| arguments? | readonly ArgumentNode[] | Arguments supplied to this field, directive, or coordinate. |
ConstDirectiveNode
Interface. A directive whose arguments are all constant values.
| Name | Type | Description |
|---|---|---|
| kind | 'Directive' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| arguments? | readonly ConstArgumentNode[] | Arguments supplied to this field, directive, or coordinate. |
TypeNode
Type alias. Any GraphQL type reference AST node.
type TypeNode =
| NamedTypeNode
| ListTypeNode
| NonNullTypeNode;
NamedTypeNode
Interface. A named type reference.
| Name | Type | Description |
|---|---|---|
| kind | 'NamedType' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
ListTypeNode
Interface. A list type reference.
| Name | Type | Description |
|---|---|---|
| kind | 'ListType' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| type | TypeNode | The GraphQL type reference or runtime type for this element. |
NonNullTypeNode
Interface. A non-null type reference.
| Name | Type | Description |
|---|---|---|
| kind | 'NonNullType' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| type | NamedTypeNode | ListTypeNode | The GraphQL type reference or runtime type for this element. |
TypeSystemDefinitionNode
Type alias. Any type-system definition that may appear in a schema document.
type TypeSystemDefinitionNode =
| SchemaDefinitionNode
| TypeDefinitionNode
| DirectiveDefinitionNode;
SchemaDefinitionNode
Interface. A schema definition in a type-system document.
| Name | Type | Description |
|---|---|---|
| kind | 'SchemaDefinition' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| description? | StringValueNode | The optional GraphQL description associated with this definition. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
| operationTypes | readonly OperationTypeDefinitionNode[] | Root operation types declared by this schema definition or extension. |
OperationTypeDefinitionNode
Interface. A root operation type declaration inside a schema definition or extension.
| Name | Type | Description |
|---|---|---|
| kind | 'OperationTypeDefinition' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| operation | OperationTypeNode | The operation selected for execution. |
| type | NamedTypeNode | The GraphQL type reference or runtime type for this element. |
TypeDefinitionNode
Type alias. Any named type definition that may appear in a schema document.
type TypeDefinitionNode =
| ScalarTypeDefinitionNode
| ObjectTypeDefinitionNode
| InterfaceTypeDefinitionNode
| UnionTypeDefinitionNode
| EnumTypeDefinitionNode
| InputObjectTypeDefinitionNode;
ScalarTypeDefinitionNode
Interface. A scalar type definition in a type-system document.
| Name | Type | Description |
|---|---|---|
| kind | 'ScalarTypeDefinition' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| description? | StringValueNode | The optional GraphQL description associated with this definition. |
| name | NameNode | Name node identifying this AST node. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
ObjectTypeDefinitionNode
Interface. An object type definition in a type-system document.
| Name | Type | Description |
|---|---|---|
| kind | 'ObjectTypeDefinition' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| description? | StringValueNode | The optional GraphQL description associated with this definition. |
| name | NameNode | Name node identifying this AST node. |
| interfaces? | readonly NamedTypeNode[] | Interfaces implemented by this object or interface type. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
| fields? | readonly FieldDefinitionNode[] | Fields declared by this object, interface, input object, or literal. |
FieldDefinitionNode
Interface. A field definition declared by an object or interface type.
| Name | Type | Description |
|---|---|---|
| kind | 'FieldDefinition' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| description? | StringValueNode | The optional GraphQL description associated with this definition. |
| name | NameNode | Name node identifying this AST node. |
| arguments? | readonly InputValueDefinitionNode[] | Arguments supplied to this field, directive, or coordinate. |
| type | TypeNode | The GraphQL type reference or runtime type for this element. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
InputValueDefinitionNode
Interface. An argument or input-field definition.
| Name | Type | Description |
|---|---|---|
| kind | 'InputValueDefinition' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| description? | StringValueNode | The optional GraphQL description associated with this definition. |
| name | NameNode | Name node identifying this AST node. |
| type | TypeNode | The GraphQL type reference or runtime type for this element. |
| defaultValue? | ConstValueNode | Default value used when no explicit value is supplied. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
InterfaceTypeDefinitionNode
Interface. An interface type definition in a type-system document.
| Name | Type | Description |
|---|---|---|
| kind | 'InterfaceTypeDefinition' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| description? | StringValueNode | The optional GraphQL description associated with this definition. |
| name | NameNode | Name node identifying this AST node. |
| interfaces? | readonly NamedTypeNode[] | Interfaces implemented by this object or interface type. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
| fields? | readonly FieldDefinitionNode[] | Fields declared by this object, interface, input object, or literal. |
UnionTypeDefinitionNode
Interface. A union type definition in a type-system document.
| Name | Type | Description |
|---|---|---|
| kind | 'UnionTypeDefinition' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| description? | StringValueNode | The optional GraphQL description associated with this definition. |
| name | NameNode | Name node identifying this AST node. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
| types? | readonly NamedTypeNode[] | Object types that belong to this union type. |
EnumTypeDefinitionNode
Interface. An enum type definition in a type-system document.
| Name | Type | Description |
|---|---|---|
| kind | 'EnumTypeDefinition' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| description? | StringValueNode | The optional GraphQL description associated with this definition. |
| name | NameNode | Name node identifying this AST node. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
| values? | readonly EnumValueDefinitionNode[] | Values contained in this enum, list, or input-object definition. |
EnumValueDefinitionNode
Interface. An enum value definition.
| Name | Type | Description |
|---|---|---|
| kind | 'EnumValueDefinition' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| description? | StringValueNode | The optional GraphQL description associated with this definition. |
| name | NameNode | Name node identifying this AST node. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
InputObjectTypeDefinitionNode
Interface. An input object type definition in a type-system document.
| Name | Type | Description |
|---|---|---|
| kind | 'InputObjectTypeDefinition' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| description? | StringValueNode | The optional GraphQL description associated with this definition. |
| name | NameNode | Name node identifying this AST node. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
| fields? | readonly InputValueDefinitionNode[] | Fields declared by this object, interface, input object, or literal. |
DirectiveDefinitionNode
Interface. A directive definition in a type-system document.
| Name | Type | Description |
|---|---|---|
| kind | 'DirectiveDefinition' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| description? | StringValueNode | The optional GraphQL description associated with this definition. |
| name | NameNode | Name node identifying this AST node. |
| arguments? | readonly InputValueDefinitionNode[] | Arguments supplied to this field, directive, or coordinate. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
| repeatable | boolean | Whether this directive may appear more than once at the same location. |
| locations | readonly NameNode[] | Locations where this directive may be applied. |
TypeSystemExtensionNode
Type alias. Any type-system extension that may appear in a schema extension document.
type TypeSystemExtensionNode =
| SchemaExtensionNode
| TypeExtensionNode
| DirectiveExtensionNode;
SchemaExtensionNode
Interface. A schema extension in a type-system document.
| Name | Type | Description |
|---|---|---|
| kind | 'SchemaExtension' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
| operationTypes? | readonly OperationTypeDefinitionNode[] | Root operation types declared by this schema definition or extension. |
TypeExtensionNode
Type alias. Any named type extension that may appear in a schema extension document.
type TypeExtensionNode =
| ScalarTypeExtensionNode
| ObjectTypeExtensionNode
| InterfaceTypeExtensionNode
| UnionTypeExtensionNode
| EnumTypeExtensionNode
| InputObjectTypeExtensionNode;
ScalarTypeExtensionNode
Interface. A scalar type extension.
| Name | Type | Description |
|---|---|---|
| kind | 'ScalarTypeExtension' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
ObjectTypeExtensionNode
Interface. An object type extension.
| Name | Type | Description |
|---|---|---|
| kind | 'ObjectTypeExtension' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| interfaces? | readonly NamedTypeNode[] | Interfaces implemented by this object or interface type. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
| fields? | readonly FieldDefinitionNode[] | Fields declared by this object, interface, input object, or literal. |
InterfaceTypeExtensionNode
Interface. An interface type extension.
| Name | Type | Description |
|---|---|---|
| kind | 'InterfaceTypeExtension' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| interfaces? | readonly NamedTypeNode[] | Interfaces implemented by this object or interface type. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
| fields? | readonly FieldDefinitionNode[] | Fields declared by this object, interface, input object, or literal. |
UnionTypeExtensionNode
Interface. A union type extension.
| Name | Type | Description |
|---|---|---|
| kind | 'UnionTypeExtension' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
| types? | readonly NamedTypeNode[] | Object types that belong to this union type. |
EnumTypeExtensionNode
Interface. An enum type extension.
| Name | Type | Description |
|---|---|---|
| kind | 'EnumTypeExtension' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
| values? | readonly EnumValueDefinitionNode[] | Values contained in this enum, list, or input-object definition. |
InputObjectTypeExtensionNode
Interface. An input object type extension.
| Name | Type | Description |
|---|---|---|
| kind | 'InputObjectTypeExtension' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
| fields? | readonly InputValueDefinitionNode[] | Fields declared by this object, interface, input object, or literal. |
DirectiveExtensionNode
Interface. A directive extension.
| Name | Type | Description |
|---|---|---|
| kind | 'DirectiveExtension' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| directives? | readonly ConstDirectiveNode[] | Directives available in this schema or applied to this AST node. |
SchemaCoordinateNode
Type alias. Any AST node representing a GraphQL schema coordinate.
type SchemaCoordinateNode =
| TypeCoordinateNode
| MemberCoordinateNode
| ArgumentCoordinateNode
| DirectiveCoordinateNode
| DirectiveArgumentCoordinateNode;
TypeCoordinateNode
Interface. A schema coordinate that refers to a named type.
| Name | Type | Description |
|---|---|---|
| kind | 'TypeCoordinate' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
MemberCoordinateNode
Interface. A schema coordinate that refers to a member of a named type.
| Name | Type | Description |
|---|---|---|
| kind | 'MemberCoordinate' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| memberName | NameNode | The member name referenced by this schema coordinate. |
ArgumentCoordinateNode
Interface. A schema coordinate that refers to a field or directive argument.
| Name | Type | Description |
|---|---|---|
| kind | 'ArgumentCoordinate' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| fieldName | NameNode | The field name referenced by this schema coordinate. |
| argumentName | NameNode | The argument name referenced by this schema coordinate. |
DirectiveCoordinateNode
Interface. A schema coordinate that refers to a directive.
| Name | Type | Description |
|---|---|---|
| kind | 'DirectiveCoordinate' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
DirectiveArgumentCoordinateNode
Interface. A schema coordinate that refers to a directive argument.
| Name | Type | Description |
|---|---|---|
| kind | 'DirectiveArgumentCoordinate' | The discriminator identifying the concrete AST or introspection kind. |
| loc? | Location | The source location for this AST node, if location tracking was enabled. |
| name | NameNode | Name node identifying this AST node. |
| argumentName | NameNode | The argument name referenced by this schema coordinate. |
Category: Kinds
Enumerations:
OperationTypeNodeDirectiveLocationKind
Enumerations
OperationTypeNode
Enumeration. The operation types supported by GraphQL executable definitions.
This is not a TypeScript
enum. GraphQL.js exportsOperationTypeNodeas both a runtime const object of literal values and a TypeScript type alias for those values.
| Name | Value |
|---|---|
QUERY | "query" |
MUTATION | "mutation" |
SUBSCRIPTION | "subscription" |
DirectiveLocation
Enumeration. The set of allowed directive location values.
This is not a TypeScript
enum. GraphQL.js exportsDirectiveLocationas both a runtime const object of literal values and a TypeScript type alias for those values.
| Name | Value | Description |
|---|---|---|
QUERY | "QUERY" | Directive location for query operations. |
MUTATION | "MUTATION" | Directive location for mutation operations. |
SUBSCRIPTION | "SUBSCRIPTION" | Directive location for subscription operations. |
FIELD | "FIELD" | Directive location for field selections. |
FRAGMENT_DEFINITION | "FRAGMENT_DEFINITION" | Directive location for fragment definitions. |
FRAGMENT_SPREAD | "FRAGMENT_SPREAD" | Directive location for fragment spreads. |
INLINE_FRAGMENT | "INLINE_FRAGMENT" | Directive location for inline fragments. |
VARIABLE_DEFINITION | "VARIABLE_DEFINITION" | Directive location for variable definitions. |
FRAGMENT_VARIABLE_DEFINITION | "FRAGMENT_VARIABLE_DEFINITION" | Directive location for fragment variable definitions. |
SCHEMA | "SCHEMA" | Directive location for schema definitions and extensions. |
SCALAR | "SCALAR" | Directive location for scalar type definitions and extensions. |
OBJECT | "OBJECT" | Directive location for object type definitions and extensions. |
FIELD_DEFINITION | "FIELD_DEFINITION" | Directive location for field definitions. |
ARGUMENT_DEFINITION | "ARGUMENT_DEFINITION" | Directive location for argument definitions. |
INTERFACE | "INTERFACE" | Directive location for interface type definitions and extensions. |
UNION | "UNION" | Directive location for union type definitions and extensions. |
ENUM | "ENUM" | Directive location for enum type definitions and extensions. |
ENUM_VALUE | "ENUM_VALUE" | Directive location for enum value definitions. |
INPUT_OBJECT | "INPUT_OBJECT" | Directive location for input object type definitions and extensions. |
INPUT_FIELD_DEFINITION | "INPUT_FIELD_DEFINITION" | Directive location for input object field definitions. |
DIRECTIVE_DEFINITION | "DIRECTIVE_DEFINITION" | Directive location for directive definitions and extensions. |
Kind
Enumeration.
This is not a TypeScript
enum. GraphQL.js exportsKindas both a runtime namespace object of literal values and a TypeScript type alias for those values.
| Name | Value | Description |
|---|---|---|
NAME | "Name" | AST kind for name nodes. |
DOCUMENT | "Document" | AST kind for document nodes. |
OPERATION_DEFINITION | "OperationDefinition" | AST kind for operation definition nodes. |
VARIABLE_DEFINITION | "VariableDefinition" | AST kind for variable definition nodes. |
SELECTION_SET | "SelectionSet" | AST kind for selection set nodes. |
FIELD | "Field" | AST kind for field selection nodes. |
ARGUMENT | "Argument" | AST kind for argument nodes. |
FRAGMENT_ARGUMENT | "FragmentArgument" | AST kind for fragment argument nodes. |
FRAGMENT_SPREAD | "FragmentSpread" | AST kind for fragment spread nodes. |
INLINE_FRAGMENT | "InlineFragment" | AST kind for inline fragment nodes. |
FRAGMENT_DEFINITION | "FragmentDefinition" | AST kind for fragment definition nodes. |
VARIABLE | "Variable" | AST kind for variable reference nodes. |
INT | "IntValue" | AST kind for integer value nodes. |
FLOAT | "FloatValue" | AST kind for floating-point value nodes. |
STRING | "StringValue" | AST kind for string value nodes. |
BOOLEAN | "BooleanValue" | AST kind for boolean value nodes. |
NULL | "NullValue" | AST kind for null value nodes. |
ENUM | "EnumValue" | AST kind for enum value nodes. |
LIST | "ListValue" | AST kind for list value nodes. |
OBJECT | "ObjectValue" | AST kind for object value nodes. |
OBJECT_FIELD | "ObjectField" | AST kind for object field nodes. |
DIRECTIVE | "Directive" | AST kind for directive nodes. |
NAMED_TYPE | "NamedType" | AST kind for named type reference nodes. |
LIST_TYPE | "ListType" | AST kind for list type reference nodes. |
NON_NULL_TYPE | "NonNullType" | AST kind for non-null type reference nodes. |
SCHEMA_DEFINITION | "SchemaDefinition" | AST kind for schema definition nodes. |
OPERATION_TYPE_DEFINITION | "OperationTypeDefinition" | AST kind for operation type definition nodes. |
SCALAR_TYPE_DEFINITION | "ScalarTypeDefinition" | AST kind for scalar type definition nodes. |
OBJECT_TYPE_DEFINITION | "ObjectTypeDefinition" | AST kind for object type definition nodes. |
FIELD_DEFINITION | "FieldDefinition" | AST kind for field definition nodes. |
INPUT_VALUE_DEFINITION | "InputValueDefinition" | AST kind for input value definition nodes. |
INTERFACE_TYPE_DEFINITION | "InterfaceTypeDefinition" | AST kind for interface type definition nodes. |
UNION_TYPE_DEFINITION | "UnionTypeDefinition" | AST kind for union type definition nodes. |
ENUM_TYPE_DEFINITION | "EnumTypeDefinition" | AST kind for enum type definition nodes. |
ENUM_VALUE_DEFINITION | "EnumValueDefinition" | AST kind for enum value definition nodes. |
INPUT_OBJECT_TYPE_DEFINITION | "InputObjectTypeDefinition" | AST kind for input object type definition nodes. |
DIRECTIVE_DEFINITION | "DirectiveDefinition" | AST kind for directive definition nodes. |
SCHEMA_EXTENSION | "SchemaExtension" | AST kind for schema extension nodes. |
DIRECTIVE_EXTENSION | "DirectiveExtension" | AST kind for directive extension nodes. |
SCALAR_TYPE_EXTENSION | "ScalarTypeExtension" | AST kind for scalar type extension nodes. |
OBJECT_TYPE_EXTENSION | "ObjectTypeExtension" | AST kind for object type extension nodes. |
INTERFACE_TYPE_EXTENSION | "InterfaceTypeExtension" | AST kind for interface type extension nodes. |
UNION_TYPE_EXTENSION | "UnionTypeExtension" | AST kind for union type extension nodes. |
ENUM_TYPE_EXTENSION | "EnumTypeExtension" | AST kind for enum type extension nodes. |
INPUT_OBJECT_TYPE_EXTENSION | "InputObjectTypeExtension" | AST kind for input object type extension nodes. |
TYPE_COORDINATE | "TypeCoordinate" | AST kind for type coordinate nodes. |
MEMBER_COORDINATE | "MemberCoordinate" | AST kind for member coordinate nodes. |
ARGUMENT_COORDINATE | "ArgumentCoordinate" | AST kind for argument coordinate nodes. |
DIRECTIVE_COORDINATE | "DirectiveCoordinate" | AST kind for directive coordinate nodes. |
DIRECTIVE_ARGUMENT_COORDINATE | "DirectiveArgumentCoordinate" | AST kind for directive argument coordinate nodes. |
Category: Lexing
Classes
Lexer
Given a Source object, creates a Lexer for that source. A Lexer is a stateful stream generator in that every time it is advanced, it returns the next token in the Source. Assuming the source lexes, the final Token emitted by the lexer will be of kind EOF, after which the lexer will repeatedly return the same EOF token whenever called.
Constructor
Creates a Lexer instance.
Signature:
new Lexer(source: Source);
| Name | Type | Description |
|---|---|---|
| source | Source | Source document used to derive error locations. |
Members
| Name | Type | Description |
|---|---|---|
| source | Source | Source document used to derive error locations. |
| lastToken | Token | Most recent non-ignored token returned by the lexer. |
| token | Token | Current non-ignored token at the lexer cursor. |
| line | number | The (1-indexed) line containing the current token. |
| lineStart | number | Character offset where the current line starts. |
advance()
Advances the token stream to the next non-ignored token.
Signature:
advance(): Token;
| Type | Description |
|---|---|
Token | The next non-ignored token. |
import { Lexer, Source } from 'graphql/language';
const lexer = new Lexer(new Source('{ hello }'));
const token = lexer.advance();
token.kind; // => '{'
lexer.token; // => tokenlookahead()
Looks ahead and returns the next non-ignored token, but does not change the state of Lexer.
Signature:
lookahead(): Token;
| Type | Description |
|---|---|
Token | The next non-ignored token without advancing the lexer. |
import { Lexer, Source } from 'graphql/language';
const lexer = new Lexer(new Source('{ hello }'));
const token = lexer.lookahead();
token.kind; // => '{'
lexer.token.kind; // => '<SOF>'Enumerations
TokenKind
Enumeration. An exported enum describing the different kinds of tokens that the lexer emits.
This is not a TypeScript
enum. GraphQL.js exportsTokenKindas both a runtime const object of literal values and a TypeScript type alias for those values.
| Name | Value |
|---|---|
SOF | "<SOF>" |
EOF | "<EOF>" |
BANG | "!" |
DOLLAR | "$" |
AMP | "&" |
PAREN_L | "(" |
PAREN_R | ")" |
DOT | "." |
SPREAD | "..." |
COLON | ":" |
EQUALS | "=" |
AT | "@" |
BRACKET_L | "[" |
BRACKET_R | "]" |
BRACE_L | "{" |
PIPE | "|" |
BRACE_R | "}" |
NAME | "Name" |
INT | "Int" |
FLOAT | "Float" |
STRING | "String" |
BLOCK_STRING | "BlockString" |
COMMENT | "Comment" |
Category: Source
Classes
Source
A representation of source input to GraphQL. The name and locationOffset parameters are
optional, but they are useful for clients who store GraphQL documents in source files.
For example, if the GraphQL input starts at line 40 in a file named Foo.graphql, it might
be useful for name to be "Foo.graphql" and location to be { line: 40, column: 1 }.
The line and column properties in locationOffset are 1-indexed.
Constructor
Creates a Source instance.
Signature:
new Source(
body: string,
name: string = 'GraphQL request',
locationOffset: {
line: number;
column: number;
} = { line: 1, column: 1 },
);
| Name | Type | Default | Description |
|---|---|---|---|
| body | string | The GraphQL source text. | |
| name | string | 'GraphQL request' | Name used in diagnostics for this source. |
| locationOffset | { line: number; column: number } | { line: 1, column: 1 } | One-indexed line and column where this source begins. |
Members
| Name | Type | Description |
|---|---|---|
| body | string | The GraphQL source text. |
| name | string | Name used in diagnostics for this source, such as a file path or request name. |
| locationOffset | { line: number; column: number } | One-indexed line and column where this source begins. |
Functions
getLocation()
Takes a Source and a UTF-8 character offset, and returns the corresponding line and column as a SourceLocation.
Signature:
getLocation(
source: Source,
position: number,
): SourceLocation;
| Name | Type | Description |
|---|---|---|
| source | Source | The source document that contains the position. |
| position | number | The UTF-8 character offset in the source body. |
| Type | Description |
|---|---|
SourceLocation | The 1-indexed line and column for the given source position. |
import { Source, getLocation } from 'graphql/language';
const source = new Source('type Query { hello: String }');
const location = getLocation(source, 13);
location; // => { line: 1, column: 14 }printLocation()
Render a helpful description of the location in the GraphQL Source document.
Signature:
printLocation(location: Location): string;
| Name | Type | Description |
|---|---|---|
| location | Location | The AST location to print. |
| Type | Description |
|---|---|
string | A formatted source excerpt with line and column information. |
import { parse, printLocation } from 'graphql/language';
const document = parse('type Query { hello: String }');
const location = document.definitions[0].loc;
if (location) {
const printed = printLocation(location);
printed; // => 'GraphQL request:1:1\n1 | type Query { hello: String }\n | ^'
}printSourceLocation()
Render a helpful description of the location in the GraphQL Source document.
Signature:
printSourceLocation(
source: Source,
sourceLocation: SourceLocation,
): string;
| Name | Type | Description |
|---|---|---|
| source | Source | The source document that contains the location. |
| sourceLocation | SourceLocation | The 1-indexed line and column to print. |
| Type | Description |
|---|---|
string | A formatted source excerpt with line and column information. |
import { Source, printSourceLocation } from 'graphql/language';
const source = new Source('type Query { hello: String }');
const printed = printSourceLocation(source, { line: 1, column: 14 });
printed; // => 'GraphQL request:1:14\n1 | type Query { hello: String }\n | ^'Types
SourceLocation
Interface. Represents a location in a Source.
| Name | Type | Description |
|---|---|---|
| line | number | One-indexed line number in the source document. |
| column | number | One-indexed column number in the source document. |
Category: Parsing
Functions
parse()
Given a GraphQL source, parses it into a Document. Throws GraphQLError if a syntax error is encountered.
Signature:
parse(
source: string | Source,
options?: ParseOptions,
): DocumentNode;
| Name | Type | Description |
|---|---|---|
| source | string | Source | A GraphQL source string or source object. |
| options? | ParseOptions | Optional parser configuration. |
| Type | Description |
|---|---|
DocumentNode | The parsed GraphQL document AST. |
// Parse a GraphQL document with the default parser options.
import { parse } from 'graphql/language';
const document = parse('{ hero { name } }');
document.kind; // => 'Document'// This variant enables parser options and provides an explicit lexer.
import { Lexer, Source, parse } from 'graphql/language';
const document = parse(`
{
t { ...A(var: true) }
}
fragment A($var: Boolean = false) on T {
name
}
`, {
experimentalFragmentArguments: true,
maxTokens: 80,
noLocation: true,
});
const directiveDocument = parse('directive @foo @bar on FIELD', {
experimentalDirectivesOnDirectiveDefinitions: true,
});
const source = new Source('{ hero }');
const lexerDocument = parse(source, { lexer: new Lexer(source) });
document.definitions[0].kind; // => 'OperationDefinition'
document.definitions[1].kind; // => 'FragmentDefinition'
document.loc; // => undefined
directiveDocument.definitions[0].kind; // => 'DirectiveDefinition'
lexerDocument.definitions[0].kind; // => 'OperationDefinition'parseValue()
Given a string containing a GraphQL value (ex. [42]), parse the AST for
that value.
Throws GraphQLError 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: valueFromAST().
Signature:
parseValue(
source: string | Source,
options?: ParseOptions,
): ValueNode;
| Name | Type | Description |
|---|---|---|
| source | string | Source | A GraphQL source string or source object containing a value. |
| options? | ParseOptions | Optional parser configuration. |
| Type | Description |
|---|---|
ValueNode | The parsed GraphQL value AST. |
import { parseValue } from 'graphql/language';
const value = parseValue('[42]');
value.kind; // => 'ListValue'parseConstValue()
Similar to parseValue(), but raises a parse error if it encounters a variable. The return type will be a constant value.
Signature:
parseConstValue(
source: string | Source,
options?: ParseOptions,
): ConstValueNode;
| Name | Type | Description |
|---|---|---|
| source | string | Source | A GraphQL source string or source object containing a constant value. |
| options? | ParseOptions | Optional parser configuration. |
| Type | Description |
|---|---|
ConstValueNode | The parsed GraphQL constant value AST. |
import { parseConstValue } from 'graphql/language';
const value = parseConstValue('{ enabled: true }');
value.kind; // => 'ObjectValue'
parseConstValue('$variable'); // throws an errorparseType()
Given a string containing a GraphQL Type (ex. [Int!]), parse the AST for
that type.
Throws GraphQLError 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: typeFromAST().
Signature:
parseType(
source: string | Source,
options?: ParseOptions,
): TypeNode;
| Name | Type | Description |
|---|---|---|
| source | string | Source | A GraphQL source string or source object containing a type reference. |
| options? | ParseOptions | Optional parser configuration. |
| Type | Description |
|---|---|
TypeNode | The parsed GraphQL type AST. |
import { parseType } from 'graphql/language';
const type = parseType('[String!]');
type.kind; // => 'ListType'parseSchemaCoordinate()
Given a string containing a GraphQL Schema Coordinate (ex. Type.field),
parse the AST for that schema coordinate.
Throws GraphQLError if a syntax error is encountered.
Consider providing the results to the utility function: resolveASTSchemaCoordinate(). Or calling resolveSchemaCoordinate() directly with an unparsed source.
Signature:
parseSchemaCoordinate(
source: string | Source,
): SchemaCoordinateNode;
| Name | Type | Description |
|---|---|---|
| source | string | Source | A GraphQL source string or source object containing a schema coordinate. |
| Type | Description |
|---|---|
SchemaCoordinateNode | The parsed GraphQL schema coordinate AST. |
import { parseSchemaCoordinate } from 'graphql/language';
const coordinate = parseSchemaCoordinate('Query.hero');
coordinate.kind; // => 'MemberCoordinate'Types
ParseOptions
Interface. Configuration options to control parser behavior
| Name | Type | Description |
|---|---|---|
| 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. |
| maxTokens? | number | Parser CPU and memory usage is linear to the number of tokens in a document however in extreme cases it becomes quadratic due to memory exhaustion. Parsing happens before validation so even invalid queries can burn lots of CPU time and memory. To prevent this you can set a maximum number of tokens allowed within a document. |
| experimentalFragmentArguments? | boolean | EXPERIMENTAL: If enabled, the parser will understand and parse fragment variable definitions and arguments on fragment spreads. Fragment variable definitions will be represented in the variableDefinitions field of the FragmentDefinitionNode.Fragment spread arguments will be represented in the arguments field of FragmentSpreadNode. |
| experimentalDirectivesOnDirectiveDefinitions? | boolean | EXPERIMENTAL: If enabled, the parser will parse directives on directive definitions. This syntax is not part of the GraphQL specification and may change. |
{
t { ...A(var: true) }
}
fragment A($var: Boolean = false) on T {
...B(x: $var)
}directive @foo @bar on FIELDCategory: AST Predicates
Functions:
isDefinitionNode()isExecutableDefinitionNode()isSubscriptionOperationDefinitionNode()isSelectionNode()isValueNode()isConstValueNode()isTypeNode()isTypeSystemDefinitionNode()isTypeDefinitionNode()isTypeSystemExtensionNode()isTypeExtensionNode()isSchemaCoordinateNode()
Functions
isDefinitionNode()
Returns true when the AST node is a definition node.
Signature:
isDefinitionNode(
node: ASTNode,
): node is DefinitionNode;
| Name | Type | Description |
|---|---|---|
| node | ASTNode | The AST node to test. |
| Type | Description |
|---|---|
node is DefinitionNode | True when the AST node is a definition node. |
import { parse, isDefinitionNode } from 'graphql/language';
const document = parse('{ hello }');
isDefinitionNode(document.definitions[0]); // => true
isDefinitionNode(document); // => falseisExecutableDefinitionNode()
Returns true when the AST node is an executable definition node.
Signature:
isExecutableDefinitionNode(
node: ASTNode,
): node is ExecutableDefinitionNode;
| Name | Type | Description |
|---|---|---|
| node | ASTNode | The AST node to test. |
| Type | Description |
|---|---|
node is ExecutableDefinitionNode | True when the AST node is an executable definition node. |
import { parse, isExecutableDefinitionNode } from 'graphql/language';
const query = parse('{ hello }');
const schema = parse('type Query { hello: String }');
isExecutableDefinitionNode(query.definitions[0]); // => true
isExecutableDefinitionNode(schema.definitions[0]); // => falseisSubscriptionOperationDefinitionNode()
A type predicate for SubscriptionOperationDefinitionNode. Useful anywhere that must distinguish subscription operations from queries and mutations, such as the subscription execution pipeline which routes events through a different code path.
Signature:
isSubscriptionOperationDefinitionNode(
node: OperationDefinitionNode,
): node is SubscriptionOperationDefinitionNode;
| Name | Type | Description |
|---|---|---|
| node | OperationDefinitionNode | Operation definition node to test. |
| Type | Description |
|---|---|
node is SubscriptionOperationDefinitionNode | True when the operation definition is a subscription. |
import { parse, isSubscriptionOperationDefinitionNode } from 'graphql/language';
const subscription = parse('subscription { greeting }').definitions[0];
const query = parse('{ greeting }').definitions[0];
isSubscriptionOperationDefinitionNode(subscription); // => true
isSubscriptionOperationDefinitionNode(query); // => falseisSelectionNode()
Returns true when the AST node is a selection node.
Signature:
isSelectionNode(
node: ASTNode,
): node is SelectionNode;
| Name | Type | Description |
|---|---|---|
| node | ASTNode | The AST node to test. |
| Type | Description |
|---|---|
node is SelectionNode | True when the AST node is a selection node. |
import { Kind, isSelectionNode } from 'graphql/language';
const field = { kind: Kind.FIELD, name: { kind: Kind.NAME, value: 'hello' } };
const document = { kind: Kind.DOCUMENT, definitions: [] };
isSelectionNode(field); // => true
isSelectionNode(document); // => falseisValueNode()
Returns true when the AST node is a value node.
Signature:
isValueNode(
node: ASTNode,
): node is ValueNode;
| Name | Type | Description |
|---|---|---|
| node | ASTNode | The AST node to test. |
| Type | Description |
|---|---|
node is ValueNode | True when the AST node is a value node. |
import { parseType, parseValue, isValueNode } from 'graphql/language';
const value = parseValue('[42]');
const type = parseType('[String!]');
isValueNode(value); // => true
isValueNode(type); // => falseisConstValueNode()
Returns true when the AST node is a constant value node.
Signature:
isConstValueNode(
node: ASTNode,
): node is ConstValueNode;
| Name | Type | Description |
|---|---|---|
| node | ASTNode | The AST node to test. |
| Type | Description |
|---|---|
node is ConstValueNode | True when the AST node is a constant value node. |
import { parseConstValue, parseValue, isConstValueNode } from 'graphql/language';
const value = parseConstValue('[42]');
const variable = parseValue('$id');
isConstValueNode(value); // => true
isConstValueNode(variable); // => falseisTypeNode()
Returns true when the AST node is a type node.
Signature:
isTypeNode(
node: ASTNode,
): node is TypeNode;
| Name | Type | Description |
|---|---|---|
| node | ASTNode | The AST node to test. |
| Type | Description |
|---|---|
node is TypeNode | True when the AST node is a type node. |
import { parseType, parseValue, isTypeNode } from 'graphql/language';
const type = parseType('[String!]');
const value = parseValue('[42]');
isTypeNode(type); // => true
isTypeNode(value); // => falseisTypeSystemDefinitionNode()
Returns true when the AST node is a type system definition node.
Signature:
isTypeSystemDefinitionNode(
node: ASTNode,
): node is TypeSystemDefinitionNode;
| Name | Type | Description |
|---|---|---|
| node | ASTNode | The AST node to test. |
| Type | Description |
|---|---|
node is TypeSystemDefinitionNode | True when the AST node is a type system definition node. |
import { parse, isTypeSystemDefinitionNode } from 'graphql/language';
const schema = parse('type Query { hello: String }');
const query = parse('{ hello }');
isTypeSystemDefinitionNode(schema.definitions[0]); // => true
isTypeSystemDefinitionNode(query.definitions[0]); // => falseisTypeDefinitionNode()
Returns true when the AST node is a type definition node.
Signature:
isTypeDefinitionNode(
node: ASTNode,
): node is TypeDefinitionNode;
| Name | Type | Description |
|---|---|---|
| node | ASTNode | The AST node to test. |
| Type | Description |
|---|---|
node is TypeDefinitionNode | True when the AST node is a type definition node. |
import { parse, isTypeDefinitionNode } from 'graphql/language';
const typeDefinition = parse('type Query { hello: String }');
const directiveDefinition = parse('directive @cache on FIELD');
isTypeDefinitionNode(typeDefinition.definitions[0]); // => true
isTypeDefinitionNode(directiveDefinition.definitions[0]); // => falseisTypeSystemExtensionNode()
Returns true when the AST node is a type system extension node.
Signature:
isTypeSystemExtensionNode(
node: ASTNode,
): node is TypeSystemExtensionNode;
| Name | Type | Description |
|---|---|---|
| node | ASTNode | The AST node to test. |
| Type | Description |
|---|---|
node is TypeSystemExtensionNode | True when the AST node is a type system extension node. |
import { parse, isTypeSystemExtensionNode } from 'graphql/language';
const extension = parse('extend type Query { hello: String }');
const definition = parse('type Query { hello: String }');
isTypeSystemExtensionNode(extension.definitions[0]); // => true
isTypeSystemExtensionNode(definition.definitions[0]); // => falseisTypeExtensionNode()
Returns true when the AST node is a type extension node.
Signature:
isTypeExtensionNode(
node: ASTNode,
): node is TypeExtensionNode;
| Name | Type | Description |
|---|---|---|
| node | ASTNode | The AST node to test. |
| Type | Description |
|---|---|
node is TypeExtensionNode | True when the AST node is a type extension node. |
import { parse, isTypeExtensionNode } from 'graphql/language';
const extension = parse('extend type Query { hello: String }');
const schemaExtension = parse('extend schema { query: Query }');
isTypeExtensionNode(extension.definitions[0]); // => true
isTypeExtensionNode(schemaExtension.definitions[0]); // => falseisSchemaCoordinateNode()
Returns true when the AST node is a schema coordinate node.
Signature:
isSchemaCoordinateNode(
node: ASTNode,
): node is SchemaCoordinateNode;
| Name | Type | Description |
|---|---|---|
| node | ASTNode | The AST node to test. |
| Type | Description |
|---|---|
node is SchemaCoordinateNode | True when the AST node is a schema coordinate node. |
import {
parse,
parseSchemaCoordinate,
isSchemaCoordinateNode,
} from 'graphql/language';
const coordinate = parseSchemaCoordinate('Query.hero');
const document = parse('{ hero }');
isSchemaCoordinateNode(coordinate); // => true
isSchemaCoordinateNode(document); // => falseCategory: Printing
Functions:
print()
Functions
print()
Converts an AST into a string, using one set of reasonable formatting rules.
Signature:
print(ast: ASTNode): string;
| Name | Type | Description |
|---|---|---|
| ast | ASTNode | The GraphQL AST node to print. |
| Type | Description |
|---|---|
string | A stable string representation of the AST. |
import { parse, print } from 'graphql';
const ast = parse('{ hero { name } }');
const text = print(ast);
text; // => '{\n hero {\n name\n }\n}'Category: Visiting
Functions:
visitInParallel()getEnterLeaveForKind()
Constants:
BREAK
Functions
visitInParallel()
Creates a new visitor instance which delegates to many visitors to run in parallel. Each visitor will be visited for each node before moving on.
If a prior visitor edits a node, no following visitors will see that node.
Signature:
visitInParallel(
visitors: readonly ASTVisitor[],
): ASTVisitor;
| Name | Type | Description |
|---|---|---|
| visitors | readonly ASTVisitor[] | The visitors to merge into one parallel visitor. |
| Type | Description |
|---|---|
ASTVisitor | A visitor that delegates traversal to each provided visitor. |
import { parse, visit, visitInParallel } from 'graphql/language';
const document = parse('{ hero { name } }');
const events = [];
visit(
document,
visitInParallel([
{ Field: (node) => { events.push(`field:${node.name.value}`); } },
{ Name: (node) => { events.push(`name:${node.value}`); } },
]),
);
events; // => ['field:hero', 'name:hero', 'field:name', 'name:name']getEnterLeaveForKind()
Given a visitor instance and a node kind, return EnterLeaveVisitor for that kind.
Signature:
getEnterLeaveForKind(
visitor: ASTVisitor,
kind: Kind,
): {
enter?: ASTVisitFn<ASTNode> | undefined;
leave?: ASTVisitFn<ASTNode> | undefined;
};
| Name | Type | Description |
|---|---|---|
| visitor | ASTVisitor | The visitor object to inspect. |
| kind | Kind | The AST node kind to resolve handlers for. |
| Type | Description |
|---|---|
{
enter?: ASTVisitFn<ASTNode> | undefined;
leave?: ASTVisitFn<ASTNode> | undefined;
} | The enter and leave handlers that apply for the given node kind. |
import { Kind, getEnterLeaveForKind } from 'graphql/language';
const handlers = getEnterLeaveForKind({ Field: () => {} }, Kind.FIELD);
typeof handlers.enter; // => 'function'
handlers.leave; // => undefinedConstants
BREAK
A value that can be returned from a visitor function to stop traversal.
unknown
Types
ASTVisitor
Type alias. A visitor defines the callbacks called during AST traversal.
type ASTVisitor =
| {
enter?:
| ASTVisitFn<ASTNode>
| undefined;
leave?:
| ASTVisitFn<ASTNode>
| undefined;
}
| {
readonly [NodeT in ASTNode as NodeT['kind']]?:
| ASTVisitFn<NodeT>
| EnterLeaveVisitor<NodeT>;
};
ASTVisitFn
Type alias. A visitor is composed of visit functions called for each node during traversal.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TVisitedNode | ASTNode | AST node type handled by this visitor function. |
type ASTVisitFn<TVisitedNode extends ASTNode> = (
node: TVisitedNode,
key: string | number | undefined,
parent:
| ASTNode
| ReadonlyArray<ASTNode>
| undefined,
path: ReadonlyArray<string | number>,
ancestors: ReadonlyArray<
ASTNode | ReadonlyArray<ASTNode>
>,
) => any;
ASTVisitorKeyMap
Type alias. A visitor key map describes the traversable child properties for each node kind.
type ASTVisitorKeyMap = mapped object;