The graphql/language
module is responsible for parsing and operating on the GraphQL language. You can import either from the graphql/language
module, or from the root graphql
module. For example:
import { Source } from "graphql" // ES6var { Source } = require("graphql") // CommonJS
Source
Lexer
Parser
Visitor
Printer
export class Source { constructor(body: string, name?: string)}
A representation of source input to GraphQL. The name is optional, but is mostly useful for clients who store GraphQL documents in source files; for example, if the GraphQL input is in a file Foo.graphql, it might be useful for name to be "Foo.graphql".
function getLocation(source: Source, position: number): SourceLocation
type SourceLocation = { line: number; column: number;}
Takes a Source and a UTF-8 character offset, and returns the corresponding line and column as a SourceLocation.
function lex(source: Source): Lexer;
type Lexer = (resetPosition?: number) => Token;
export type Token = { kind: number; start: number; end: number; value: ?string;};
Given a Source object, this returns a Lexer for that source. A Lexer is a function that acts like a generator in that every time it is called, 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 EOF tokens whenever called.
The argument to the lexer function is optional, and can be used to rewind or fast forward the lexer to a new position in the source.
export function parse( source: Source | string, options?: ParseOptions): Document
Given a GraphQL source, parses it into a Document.
Throws GraphQLError if a syntax error is encountered.
export function parseValue( source: Source | string, options?: ParseOptions): Value
Given a string containing a GraphQL value, 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.
An enum that describes the different kinds of AST nodes.
function visit(root, visitor, keyMap)
visit() will walk through an AST using a depth first traversal, calling the visitor's enter function at each node in the traversal, and calling the leave function after visiting that node and all of its child nodes.
By returning different values from the enter and leave functions, the behavior of the visitor can be altered, including skipping over a sub-tree of the AST (by returning false), editing the AST by returning a value or null to remove the value, or to stop the whole traversal by returning BREAK.
When using visit() to edit an AST, the original AST will not be modified, and a new version of the AST with the changes applied will be returned from the visit function.
var editedAST = visit(ast, { enter(node, key, parent, path, ancestors) { // @return // undefined: no action // false: skip visiting this node // visitor.BREAK: stop visiting altogether // null: delete this node // any value: replace this node with the returned value }, leave(node, key, parent, path, ancestors) { // @return // undefined: no action // false: no action // visitor.BREAK: stop visiting altogether // null: delete this node // any value: replace this node with the returned value },})
Alternatively to providing enter() and leave() functions, a visitor can instead provide functions named the same as the kinds of AST nodes, or enter/leave visitors at a named key, leading to four permutations of visitor API:
visit(ast, { Kind(node) { // enter the "Kind" node },})
visit(ast, { Kind: { enter(node) { // enter the "Kind" node } leave(node) { // leave the "Kind" node } }})
visit(ast, { enter(node) { // enter any node }, leave(node) { // leave any node },})
visit(ast, { enter: { Kind(node) { // enter the "Kind" node }, }, leave: { Kind(node) { // leave the "Kind" node }, },})
The sentinel BREAK
value described in the documentation of visitor
.
function print(ast): string
Converts an AST into a string, using one set of reasonable formatting rules.