SyntaxTree

SyntaxTree

new SyntaxTree()

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

Source:

Members

(static, constant) SUBSCRIPTION :string

A runtime constant denoting a subscription type.

Source:
Type:
  • string

(static, constant) ⬇︎⠀MUTATION :string

A runtime constant denoting a mutation type.

Source:
Type:
  • string

(static, constant) ⬇︎⠀QUERY :string

A runtime constant denoting a query type.

Source:
Type:
  • string

Methods

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

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

Source:
Parameters:
Name Type Description
schemaOrASTOrST string | Object | SyntaxTree

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

Returns:
Type:
SyntaxTree

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

(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.

Source:
Returns:
Type:
SyntaxTree

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

(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.

Source:
Returns:
Type:
SyntaxTree

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

(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.

Source:
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:
Type:
Object | null

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

(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.

Since:
  • 2.7.0
Source:
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:
Type:
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.

(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.

Since:
  • 2.7.0
Source:
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:
Type:
Object | null

the AST leaf if one matches or null otherwise.

(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.

Source:
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:
Type:
SyntaxTree

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

(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.

Source:
Parameters:
Name Type Description
ast object

a valid GraphQL AST object.

Returns:
Type:
SyntaxTree

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

(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.

Source:
Parameters:
Name Type Description
schema string

a valid GraphQL IDL schema string.

Returns:
Type:
SyntaxTree

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

(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.

Source:
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.

⌾⠀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).

Source:
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:
Type:
SyntaxTree

this for inlining

⌾⠀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.

Source:
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:
Type:
SyntaxTree

returns this for inlining

⌾⠀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.

Source:
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:
Type:
Object | null

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

⌾⠀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.

Since:
  • 2.7.0
Source:
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:
Type:
Object | null

the desired AST node or null if one does not exist

⌾⠀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.

Source:
Parameters:
Name Type Description
schemaOrAST string | Object

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

Returns:
Type:
SyntaxTree

this for inlining.

⌾⠀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.

Source:
Returns:
Type:
string

the AST for the tree parsed back into a string

⌾⠀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.

Source:
Parameters:
Name Type Description
ast Object

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

Returns:
Type:
SyntaxTree

this for inlining.

⬆︎⠀ast(value)

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

Source:
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

⬇︎⠀ast() → {Object}

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

Source:
Returns:
Type:
Object

a GraphQL AST object