Graphql: Language Module Cheat Sheet

Source

Source 

Represents the input string to the GraphQL server

export class Source {
  constructor(body: string, name?: string)
}

getLocation 

Converts a character offset to a row and column in the Source

function getLocation(source: Source, position: number): SourceLocation

type SourceLocation = {
  line: number;
  column: number;
}

Lexer

lex 

Lexes a GraphQL Source according to the GraphQL Gramma

function lex(source: Source): Lexer;

type Lexer = (resetPosition?: number) => Token;

export type Token = {
  kind: number;
  start: number;
  end: number;
  value: ?string;
};

Parser 

parse 

Parses a GraphQL Source according to the GraphQL Grammar

export function parse(
  source: Source | string,
  options?: ParseOptions
): Document

parseValue 

Parses a value according to the GraphQL Grammar

export function parseValue(
  source: Source | string,
  options?: ParseOptions
): Value

Visitor

visit 

A general-purpose visitor to traverse a parsed GraphQL AST

function visit(root, visitor, keyMap)

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:

  • Named visitors triggered when entering a node a specific kind.
visit(ast, {
  Kind(node) {
    // enter the "Kind" node
  }
})
  • Named visitors that trigger upon entering and leaving a node of a specific kind.
visit(ast, {
  Kind: {
    enter(node) {
      // enter the "Kind" node
    }
    leave(node) {
      // leave the "Kind" node
    }
  }
})
  • Generic visitors that trigger upon entering and leaving any node.
visit(ast, {
  enter(node) {
    // enter any node
  },
  leave(node) {
    // leave any node
  }
})
  • Parallel visitors for entering and leaving nodes of a specific kind.
visit(ast, {
  enter: {
    Kind(node) {
      // enter the "Kind" node
    }
  },
  leave: {
    Kind(node) {
      // leave the "Kind" node
    }
  }
})

Printer 

print 

Prints an AST in a standard format.

function print(ast): string

Leave a Reply

Your email address will not be published. Required fields are marked *