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
| Name | Type | Description |
|---|---|---|
| documents | readonly DocumentNode[] | The documents value. |
Returns
| Type | Description |
|---|---|
DocumentNode | A 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: 2separateOperations()
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
| Name | Type | Description |
|---|---|---|
| documentAST | DocumentNode | The parsed GraphQL document AST. |
Returns
| Type | Description |
|---|---|
object | A 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 valuestripIgnoredCharacters()
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
| Name | Type | Description |
|---|---|---|
| source | string | Source | The GraphQL source text or source object. |
Returns
| Type | Description |
|---|---|
string | A semantically equivalent GraphQL source string without ignored characters. |
Example
import { stripIgnoredCharacters } from 'graphql/utilities';
const source = stripIgnoredCharacters('query Example { name }');
// source: 'query Example{name}'