v17 APIgraphql/language

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

ExportKindDescription
SourceClassRepresents GraphQL source text with an optional name and source location offset.
LocationClassRepresents the character span and token range for an AST node.
TokenClassRepresents one lexical token.
getLocation()FunctionConverts a character offset in a Source to { line, column }.
printLocation()FunctionPrints a source location with surrounding source text.
printSourceLocation()FunctionPrints a { line, column } location in a source.
SourceLocationType{ 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

ExportKindDescription
LexerClassPull-based GraphQL lexer over a Source.
TokenKind Changed in v17Const object and typeToken kind constants such as TokenKind.NAME and TokenKind.EOF; use it as both value and type.
parse() Changed in v17FunctionParses a document into a DocumentNode. v17 includes experimental parser options and omits many empty AST child arrays.
parseValue()FunctionParses a GraphQL value literal.
parseConstValue()FunctionParses a constant GraphQL value literal.
parseType()FunctionParses a GraphQL type reference.
parseSchemaCoordinate()FunctionParses a schema coordinate.
ParseOptions Changed in v17TypeParser options, including noLocation and experimental syntax flags.
print()FunctionPrints 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

ExportKindDescription
visit()FunctionDepth-first AST traversal and editing utility.
visitInParallel()FunctionCombines multiple visitors into one visitor.
getEnterLeaveForKind() Changed in v17FunctionReads the enter and leave callbacks for one AST kind from a visitor. Use it instead of the removed getVisitFn().
getVisitFn()FunctionReads the callback for one AST kind from a visitor.
BREAKConstantSentinel value that stops a visitor traversal.
ASTVisitorTypeVisitor object shape.
ASTVisitFnTypeVisitor callback shape.
ASTVisitorKeyMapTypeMap of AST kind to child keys used for traversal.

Language Constant Exports

ExportKindDescription
Kind Changed in v17Const object and typeAST node kind constants such as Kind.FIELD; use it as both value and type.
DirectiveLocation Changed in v17Const object and typeDirective location constants such as DirectiveLocation.FIELD. v17 adds FRAGMENT_VARIABLE_DEFINITION for experimental fragment arguments.
OperationTypeNodeConst objectOperation 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

ExportDescription
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

GroupExports
Base ASTASTNode, ASTKindToNode, NameNode, DocumentNode, DefinitionNode, ExecutableDefinitionNode
OperationsOperationDefinitionNode, VariableDefinitionNode, VariableNode, SelectionSetNode, SelectionNode, FieldNode, ArgumentNode, ConstArgumentNode
FragmentsFragmentSpreadNode, InlineFragmentNode, FragmentDefinitionNode
ValuesValueNode, ConstValueNode, IntValueNode, FloatValueNode, StringValueNode, BooleanValueNode, NullValueNode, EnumValueNode, ListValueNode, ConstListValueNode, ObjectValueNode, ConstObjectValueNode, ObjectFieldNode, ConstObjectFieldNode
DirectivesDirectiveNode, ConstDirectiveNode, DirectiveDefinitionNode, DirectiveExtensionNode
Type referencesTypeNode, NamedTypeNode, ListTypeNode, NonNullTypeNode
Type system definitionsTypeSystemDefinitionNode, SchemaDefinitionNode, OperationTypeDefinitionNode, TypeDefinitionNode, ScalarTypeDefinitionNode, ObjectTypeDefinitionNode, FieldDefinitionNode, InputValueDefinitionNode, InterfaceTypeDefinitionNode, UnionTypeDefinitionNode, EnumTypeDefinitionNode, EnumValueDefinitionNode, InputObjectTypeDefinitionNode
Type system extensionsTypeSystemExtensionNode, SchemaExtensionNode, TypeExtensionNode, ScalarTypeExtensionNode, ObjectTypeExtensionNode, InterfaceTypeExtensionNode, UnionTypeExtensionNode, EnumTypeExtensionNode, InputObjectTypeExtensionNode
Schema coordinatesSchemaCoordinateNode, 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.