SyntaxTree

SyntaxTree

new SyntaxTree()

A parser and processor of GraphQL IDL Abstract Syntax Trees. Used to combine
a set of GQLBase class instances.

Source:
SyntaxTree.js, line 61

Members

(static, constant) ⬇︎⠀MUTATION :string

A runtime constant denoting a mutation type.

Type:
  • string
Source:
SyntaxTree.js, line 823

(static, constant) ⬇︎⠀QUERY :string

A runtime constant denoting a query type.

Type:
  • string
Source:
SyntaxTree.js, line 441

Methods

(static) ⌾⠀EmptyDocument(schemaOrASTOrST) → {SyntaxTree}

The starting point for a SyntaxTree that will be built up programmatically.

Parameters:
Name Type Description
schemaOrASTOrST string | Object | SyntaxTree

any valid type taken by
SyntaxTree.from() used to further populate the new empty document

Returns:

an instance of SyntaxTree with no definitions and a
kind set to 'Document'

Source:
SyntaxTree.js, line 783

(static) ⌾⠀EmptyMutation() → {SyntaxTree}

Mutation types in GraphQL are an ObjectTypeDefinition of importance for
placement on the root object. There is utility in creating an empty
one that can be injected with the fields of other GraphQL object mutation
entries.

Returns:

an instance of SyntaxTree with a base AST generated
by parsing the graph query, "type Mutation {}"

Source:
SyntaxTree.js, line 763

(static) ⌾⠀EmptyQuery() → {SyntaxTree}

Query types in GraphQL are an ObjectTypeDefinition of importance for
placement on the root object. There is utility in creating an empty
one that can be injected with the fields of other GraphQL object query
entries.

Returns:

an instance of SyntaxTree with a base AST generated
by parsing the graph query, "type Query {}"

Source:
SyntaxTree.js, line 743

(static) ⌾⠀findDefinition(ast, definitionName) → {Object|null}

Iterate through the definitions of the AST if there are any. For each
definition the name property's value field is compared to the supplied
definitionName. The definitionName can be a string or a regular
expression if finer granularity is desired.

Parameters:
Name Type Description
ast Object

an abstract syntax tree object created from a GQL SDL

definitionName string | RegExp

a string or regular expression used
to match against the definition name field in a given AST.

Returns:
( Object | null )

a reference to the internal definition field or
null if one with a matching name could not be found.

Source:
SyntaxTree.js, line 600

(static) ⌾⠀findField(ast, definitionName, fieldName) → {Object|null}

Iterate through the fields of a definition AST if there are any. For each
field, the name property's value field is compared to the supplied
fieldName. The fieldName can be a string or a regular expression if
finer granularity is desired.

Before iterating over the fields, however, the definition is found using
SyntaxTree#findDefinition. If either the field or definition are not
found, null is returned.

Parameters:
Name Type Description
ast Object

an abstract syntax tree object created from a GQL SDL

definitionName string | RegExp

a string or regular expression used
to match against the definition name field in a given AST.

fieldName string | RegExp

a string or regular expression used
to match against the field name field in a given AST.

Returns:
( Object | null )

an object containing two keys, the first being
field which points to the requested AST definition field. The second
being meta which contains three commonly requested bits of data; name,
type and nullable. Non-nullable fields have their actual type wrapped
in a NonNullType GraphQL construct. The actual field type is contained
within. The meta object surfaces those values for easy use.

Since:
2.7.0
Source:
SyntaxTree.js, line 623

(static) ⌾⠀findInASTArrayByNameValue(array, name) → {Object|null}

A lot of searching in ASTs is filtering through arrays and matching on
subobject properties on each iteration. A common theme is find something
by its .name.value. This method simplifies that by taking an array of
AST nodes and searching them for a .name.value property that exists
within.

Parameters:
Name Type Description
array Array

of mixed AST object nodes containing name.values

name string | RegExp

a string or regular expression used
to match against the node name value

Returns:
( Object | null )

the AST leaf if one matches or null otherwise.

Since:
2.7.0
Source:
SyntaxTree.js, line 708

(static) ⌾⠀from(mixed) → {SyntaxTree}

Given one of, a valid GraphQL IDL schema string, a valid GraphQL AST or
an instance of SyntaxTree, the static from() method will create a new
instance of the SyntaxTree with the values you provide.

Parameters:
Name Type Description
mixed String | Object | SyntaxTree

an instance of one of the valid
types specified above. Everything else will result in a null value.

Returns:

a newly created and populated instance of SyntaxTree
or null if an invalid type was supplied for mixed.

Source:
SyntaxTree.js, line 507

(static) ⌾⠀fromAST(ast) → {SyntaxTree}

Generates a new instance of SyntaxTree from the supplied, valid, GraphQL
schema. This method does not perform try/catch validation and if an
invalid GraphQL schema is supplied an error will be thrown.

Parameters:
Name Type Description
ast object

a valid GraphQL AST object.

Returns:

a new instance of SyntaxTree initialized with a
supplied abstract syntax tree generated by require('graphql').parse() or
other compatible method.

Source:
SyntaxTree.js, line 576

(static) ⌾⠀fromSchema(schema) → {SyntaxTree}

Generates a new instance of SyntaxTree from the supplied, valid, GraphQL
schema. This method does not perform try/catch validation and if an
invalid GraphQL schema is supplied an error will be thrown.

Parameters:
Name Type Description
schema string

a valid GraphQL IDL schema string.

Returns:

a new instance of SyntaxTree initialized with a
parsed response from require('graphql').parse().

Source:
SyntaxTree.js, line 553

(static) ⎆⠀constructor(schemaOrASTOrST)

Constructs a new SyntaxTree object. If a string schema is supplied or
an already parsed AST object, either of which is valid GraphQL IDL, then
its parsed AST will be the internals of this object.

Parameters:
Name Type Description
schemaOrASTOrST string | Object | SyntaxTree

if supplied the tree
will be constructed with the contents of the data. If a string of IDL is
given, it will be parsed. If an AST is given, it will be verified. If a
SyntaxTree is supplied, it will be copied.

Source:
SyntaxTree.js, line 69

⌾⠀appendDefinitions(schemaOrASTOrST) → {SyntaxTree}

Appends all definitions from another AST to this one. The method will
actually create a copy using SyntaxTree.from() so the input types can
be any one of a valid GraphQL IDL schema string, a GraphQL IDL AST or
another SyntaxTree object instance.

Definitions of the same name but different kinds will be replaced by the
new copy. Those of the same kind and name will be merged (TODO handle more
than ObjectTypeDefinition kinds when merging; currently other types are
overwritten).

Parameters:
Name Type Description
schemaOrASTOrST string | Object | SyntaxTree

an instance of one of
the valid types for SyntaxTree.from() that can be used to create or
duplicate the source from which to copy definitions.

Returns:

this for inlining

Source:
SyntaxTree.js, line 193

⌾⠀consumeDefinition(astOrSyntaxTree, definitionType) → {SyntaxTree}

This method finds the Query type definitions in the supplied AST or
SyntaxTree objects, takes its defined fields and adds it to the current
instances. If this instance does not have a Query type defined but the
supplied object does, then the supplied one is moved over. If neither
has a query handler, then nothing happens.

NOTE this removes the Query type definition from the supplied AST or
SyntaxTree object.

Parameters:
Name Type Description
astOrSyntaxTree Object | SyntaxTree

a valid GraphQL IDL AST or
an instance of SyntaxTree that represents one.

definitionType string | RegExp

a valid search input as would be
accepted for the #find() method of this object.

Returns:

returns this for inlining

Source:
SyntaxTree.js, line 287

⌾⠀find(definitionName) → {Object|null}

Iterate through the definitions of the AST if there are any. For each
definition the name property's value field is compared to the supplied
definitionName. The definitionName can be a string or a regular
expression if finer granularity is desired.

Parameters:
Name Type Description
definitionName string | RegExp

a string or regular expression used
to match against the definition name field in a given AST.

Returns:
( Object | null )

a reference to the internal definition field or
null if one with a matching name could not be found.

Source:
SyntaxTree.js, line 400

⌾⠀findEnumDefinition(ast, enumDefinitionName, enumValueName) → {Object|null}

Enum AST definitions operate differently than object type definitions
do. Namely, they do not have a fields array but instead have a values
array. This wrapper method, first finds the enum definition in the ast
and then searches the values for the named node desired and returns that
or null, if one could not be found.

Parameters:
Name Type Description
ast Object

the abstract syntax tree parsed by graphql

enumDefinitionName string | RegExp

a string or regular expression
used to locate the enum definition in the AST.

enumValueName string | RegExp

a string or regular expression used
to locate the value by name in the values of the enum definition.

Returns:
( Object | null )

the desired AST node or null if one does not exist

Since:
2.7.0
Source:
SyntaxTree.js, line 674

⌾⠀setAST(schemaOrAST) → {SyntaxTree}

Sets the underlying AST object with either schema which will be parsed
into a valid AST or an existing AST. Previous ast values will be erased.

Parameters:
Name Type Description
schemaOrAST string | Object

a valid GraphQL IDL schema or a
previosuly parsed or compatible GraphQL IDL AST object.

Returns:

this for inlining.

Source:
SyntaxTree.js, line 109

⌾⠀toString() → {string}

SyntaxTree instances that are toString()'ed will have the graphql method
print() called on them to convert their internal structures back to a
GraphQL IDL schema syntax. If the object is in an invalid state, it WILL
throw an error.

Returns:
( string )

the AST for the tree parsed back into a string

Source:
SyntaxTree.js, line 422

⌾⠀updateAST(ast) → {SyntaxTree}

As passthru update method that works on the internal AST object. If
an error occurs, the update is skipped. An error can occur if adding the
changes would make the AST invalid. In such a case, the error is logged
to the error console.

Parameters:
Name Type Description
ast Object

an existing GraphQL IDL AST object that will be
merged on top of the existing tree using Object.assign()

Returns:

this for inlining.

Source:
SyntaxTree.js, line 158

⬆︎⠀ast(value)

Setter that assigns the abstract syntax tree, typically created by
graphql.parse when given a valid string of IDL.

Parameters:
Name Type Description
value Object

a valid AST object. Other operations will act
in an undefined manner should this object not be a valid AST

Source:
SyntaxTree.js, line 458

⬇︎⠀ast() → {Object}

Getter that retrieves the abstract syntax tree created by graphql.parse
when it is presented with a valid string of IDL.

Returns:
( Object )

a GraphQL AST object

Source:
SyntaxTree.js, line 93