graphql/language
The graphql/language entry point contains the lexer, parser, printer, visitor
utilities, AST classes, AST TypeScript types, predicates, and GraphQL language
constants.
import { parse, print, visit, Kind } from 'graphql/language';The same exports are also available from the root graphql entry point.
Contents
- Source and Location Exports
- Lexer, Parser, and Printer Exports
- Visitor Exports
- Language Constant Exports
- Predicate Exports
- AST Type Exports
- Experimental Parser Options
Source and Location Exports
| Export | Kind | Description |
|---|---|---|
Source | Class | Represents GraphQL source text with an optional name and source location offset. |
Location | Class | Represents the character span and token range for an AST node. |
Token | Class | Represents one lexical token. |
getLocation() | Function | Converts a character offset in a Source to { line, column }. |
printLocation() | Function | Prints a source location with surrounding source text. |
printSourceLocation() | Function | Prints a { line, column } location in a source. |
SourceLocation | Type | { line, column } object returned by getLocation(). |
import { Source, getLocation } from 'graphql';
const source = new Source('query { viewer { name } }', 'Viewer.graphql');
const location = getLocation(source, 8);Lexer, Parser, and Printer Exports
| Export | Kind | Description |
|---|---|---|
Lexer | Class | Pull-based GraphQL lexer over a Source. |
TokenKind Changed in v17 | Const object and type | Token kind constants such as TokenKind.NAME and TokenKind.EOF; use it as both value and type. |
parse() Changed in v17 | Function | Parses a document into a DocumentNode. v17 includes experimental parser options and omits many empty AST child arrays. |
parseValue() | Function | Parses a GraphQL value literal. |
parseConstValue() | Function | Parses a constant GraphQL value literal. |
parseType() | Function | Parses a GraphQL type reference. |
parseSchemaCoordinate() | Function | Parses a schema coordinate. |
ParseOptions Changed in v17 | Type | Parser options, including noLocation and experimental syntax flags. |
print() | Function | Prints an AST back to GraphQL source text. |
import { parse, print, visit, Kind } from 'graphql';
const document = parse('query { viewer { name } }');
const renamed = visit(document, {
Field(node) {
if (node.name.value === 'viewer') {
return {
...node,
name: { kind: Kind.NAME, value: 'me' },
};
}
},
});
console.log(print(renamed));Visitor Exports
| Export | Kind | Description |
|---|---|---|
visit() | Function | Depth-first AST traversal and editing utility. |
visitInParallel() | Function | Combines multiple visitors into one visitor. |
getEnterLeaveForKind() Changed in v17 | Function | Reads the enter and leave callbacks for one AST kind from a visitor. Use it instead of the removed getVisitFn(). |
getVisitFn() | Function | Reads the callback for one AST kind from a visitor. |
BREAK | Constant | Sentinel value that stops a visitor traversal. |
ASTVisitor | Type | Visitor object shape. |
ASTVisitFn | Type | Visitor callback shape. |
ASTVisitorKeyMap | Type | Map of AST kind to child keys used for traversal. |
Language Constant Exports
| Export | Kind | Description |
|---|---|---|
Kind Changed in v17 | Const object and type | AST node kind constants such as Kind.FIELD; use it as both value and type. |
DirectiveLocation Changed in v17 | Const object and type | Directive location constants such as DirectiveLocation.FIELD. v17 adds FRAGMENT_VARIABLE_DEFINITION for experimental fragment arguments. |
OperationTypeNode | Const object | Operation type constants: query, mutation, and subscription. |
import { Kind } from 'graphql';
import type { Kind as KindType } from 'graphql';
function isFieldKind(kind: KindType): boolean {
return kind === Kind.FIELD;
}Predicate Exports
| Export | Description |
|---|---|
isDefinitionNode() | Checks any executable or type-system definition node. |
isExecutableDefinitionNode() | Checks operation and fragment definitions. |
isSelectionNode() | Checks field, fragment spread, and inline fragment selections. |
isValueNode() | Checks runtime value nodes. |
isConstValueNode() | Checks value nodes that can appear in constant locations. |
isTypeNode() | Checks named, list, and non-null type reference nodes. |
isTypeSystemDefinitionNode() | Checks schema, type, and directive definitions. |
isTypeDefinitionNode() | Checks named type definitions. |
isTypeSystemExtensionNode() | Checks schema, type, and directive extensions. |
isTypeExtensionNode() | Checks named type extensions. |
isSchemaCoordinateNode() | Checks schema coordinate AST nodes. |
AST Type Exports
| Group | Exports |
|---|---|
| Base AST | ASTNode, ASTKindToNode, NameNode, DocumentNode, DefinitionNode, ExecutableDefinitionNode |
| Operations | OperationDefinitionNode, VariableDefinitionNode, VariableNode, SelectionSetNode, SelectionNode, FieldNode, ArgumentNode, ConstArgumentNode |
| Fragments | FragmentSpreadNode, InlineFragmentNode, FragmentDefinitionNode |
| Values | ValueNode, ConstValueNode, IntValueNode, FloatValueNode, StringValueNode, BooleanValueNode, NullValueNode, EnumValueNode, ListValueNode, ConstListValueNode, ObjectValueNode, ConstObjectValueNode, ObjectFieldNode, ConstObjectFieldNode |
| Directives | DirectiveNode, ConstDirectiveNode, DirectiveDefinitionNode, DirectiveExtensionNode |
| Type references | TypeNode, NamedTypeNode, ListTypeNode, NonNullTypeNode |
| Type system definitions | TypeSystemDefinitionNode, SchemaDefinitionNode, OperationTypeDefinitionNode, TypeDefinitionNode, ScalarTypeDefinitionNode, ObjectTypeDefinitionNode, FieldDefinitionNode, InputValueDefinitionNode, InterfaceTypeDefinitionNode, UnionTypeDefinitionNode, EnumTypeDefinitionNode, EnumValueDefinitionNode, InputObjectTypeDefinitionNode |
| Type system extensions | TypeSystemExtensionNode, SchemaExtensionNode, TypeExtensionNode, ScalarTypeExtensionNode, ObjectTypeExtensionNode, InterfaceTypeExtensionNode, UnionTypeExtensionNode, EnumTypeExtensionNode, InputObjectTypeExtensionNode |
| Schema coordinates | SchemaCoordinateNode, TypeCoordinateNode, MemberCoordinateNode, ArgumentCoordinateNode, DirectiveCoordinateNode, DirectiveArgumentCoordinateNode |
Experimental Parser Options
const document = parse(source, {
experimentalFragmentArguments: true,
experimentalDirectivesOnDirectiveDefinitions: true,
});Use these flags only when both the producer and consumer of the document understand the proposal syntax. The resulting AST nodes are public, but the underlying GraphQL specification features remain experimental.