graphql/utilities/ast-utilities

Functions

concatAST()

Provided a collection of ASTs, presumably each from different files, concatenate the ASTs together into batched AST, useful for validating many GraphQL source files which together represent one conceptual application.

Signature:

concatAST(documents: readonly DocumentNode[]): DocumentNode

Arguments

NameTypeDescription
documentsreadonly DocumentNode[]The documents value.

Returns

TypeDescription
DocumentNodeA document containing the definitions from each provided document.

Example

import { parse } from 'graphql/language';
import { concatAST } from 'graphql/utilities';
 
const document = concatAST([parse('type Query { a: String }'), parse('type User { id: ID }')]);
 
// document.definitions.length: 2

separateOperations()

separateOperations accepts a single AST document which may contain many operations and fragments and returns a collection of AST documents each of which contains a single operation as well the fragment definitions it refers to.

Signature:

separateOperations(documentAST: DocumentNode): object

Arguments

NameTypeDescription
documentASTDocumentNodeThe parsed GraphQL document AST.

Returns

TypeDescription
objectA map of operation names to documents containing each operation and its referenced fragments.

Example

import { separateOperations } from 'graphql/utilities';
 
const result = separateOperations(documentAST);
 
// result contains the separateOperations return value

stripIgnoredCharacters()

Strips characters that are not significant to the validity or execution of a GraphQL document:

  • UnicodeBOM
  • WhiteSpace
  • LineTerminator
  • Comment
  • Comma
  • BlockString indentation

Note: It is required to have a delimiter character between neighboring non-punctuator tokens and this function always uses single space as delimiter.

It is guaranteed that both input and output documents if parsed would result in the exact same AST except for nodes location.

Warning: It is guaranteed that this function will always produce stable results. However, it’s not guaranteed that it will stay the same between different releases due to bugfixes or changes in the GraphQL specification.

Query example:

query SomeQuery($foo: String!, $bar: String) {
  someField(foo: $foo, bar: $bar) {
    a
    b {
      c
      d
    }
  }
}

Becomes:

query SomeQuery($foo:String!$bar:String){someField(foo:$foo bar:$bar){a b{c d}}}

SDL example:

"""
Type description
"""
type Foo {
  """
  Field description
  """
  bar: String
}

Becomes:

"""Type description""" type Foo{"""Field description""" bar:String}

Signature:

stripIgnoredCharacters(source: string | Source): string

Arguments

NameTypeDescription
sourcestring | SourceThe GraphQL source text or source object.

Returns

TypeDescription
stringA semantically equivalent GraphQL source string without ignored characters.

Example

import { stripIgnoredCharacters } from 'graphql/utilities';
 
const source = stripIgnoredCharacters('query Example { name }');
 
// source: 'query Example{name}'