GQLBase

GQLBase

new GQLBase()

All GraphQL Type objects used in this system are assumed to have extended
from this class. An instance of this class can be used to wrap an existing
structure if you have one.

Source:

Classes

⎆⠀constructor

Members

(static, constant) ⬇︎⠀[_PROXY_HANDLER] :Object

If ES6 Proxies are supported in your execution environment, all GQLBase
extended classes are also proxies. By default the internal proxy handler
provides backwards compatibility with the removal of the default getters
and setters for the 'model' property as long as you do not define a
top level 'model' property of your own.

Since:
  • 2.5.0
Source:
Type:
  • Object

(static, constant) ⬇︎⠀ADJACENT_FILE

Source:
See:
  • GQLBase#SCHEMA

(static, constant) ⬇︎⠀DOC_CLASS :string

A constant key used to identify a comment for a class description

Source:
Type:
  • string

(static, constant) ⬇︎⠀DOC_FIELDS :string

A constant key used to identify a comment for a type field description

Source:
Type:
  • string

(static, constant) ⬇︎⠀DOC_MUTATION :string

A constant key used to identify a comment for the top level mutation
description

Source:
Type:
  • string

(static, constant) ⬇︎⠀DOC_MUTATORS :string

A constant key used to identify a comment for a mutator description

Source:
Type:
  • string

(static, constant) ⬇︎⠀DOC_MUTATORS :string

A constant key used to identify a comment for a mutator description

Deprecated:
  • Use `DOC_MUTATIONS` instead
Source:
Type:
  • string

(static, constant) ⬇︎⠀DOC_QUERIES :string

A constant key used to identify a comment for a query description

Source:
Type:
  • string

(static, constant) ⬇︎⠀DOC_QUERY :string

A constant key used to identify a comment for the top level query
description

Source:
Type:
  • string

(static, constant) ⬇︎⠀DOC_SUBSCRIPTION :string

A constant key used to identify a comment for the top level subscription
description

Source:
Type:
  • string

(static, constant) ⬇︎⠀DOC_SUBSCRIPTIONS :string

A constant key used to identify a comment for a subscription description

Source:
Type:
  • string

(static, constant) ⬇︎⠀EVENT_MODEL_HAS_BEEN_SET :string

A constant used to register an event listener for when the internal
model object is assigned a new value. This event fires after the model
is set.

Source:
Type:
  • string

(static, constant) ⬇︎⠀EVENT_MODEL_PROP_CHANGE :string

A constant used to register an event listener for when a property of the
internal model object is set to a new or intial value.

Source:
Type:
  • string

(static, constant) ⬇︎⠀EVENT_MODEL_PROP_DELETE :string

A constant used to register an event listener for when a property of the
internal model object has been deleted. This event fires after the value
has been deleted.

Source:
Type:
  • string

(static, constant) ⬇︎⠀EVENT_MODEL_WILL_BE_SET :string

A constant used to register an event listener for when the internal
model object is assigned a new value. This event fires before the model
is set. Changes to the model value at this point will affect the contents
before the value assignment takes place.

Source:
Type:
  • string

(static, constant) ⬇︎⠀GQL_TYPE

Determines the default type targeted by this GQLBase class. Any
type will technically be valid but only will trigger special behavior

Source:

(static, constant) ⬇︎⠀joinLines :function

A shortcut to the utils/joinLines function to make it easier to get
the tools to write docs for your types in a friendly fashion.

Source:
Type:
  • function

(static, constant) ⬇︎⠀module

Returns the module object where your class is created. This needs to be
defined on your class, as a static getter, in the FILE where you are
defining your Class definition.

Source:
See:

Methods

(static) apiDocs() → {Object}

Until such time as the reference implementation of Facebook's GraphQL
SDL AST parser supports comments, or until we take advantage of Apollo's
AST parser, this is how comments will be applied to a built schema.

Several constants are defined on the GQLBase object itself, and thereby
all its subclasses. They pertain to how to define description fields
for various parts of your GQL implementation.

// To define a description on the top level class
[this.DOC_CLASS]: string

// To define a description on a field (getter, function or async function)
[this.DOC_FIELDS]: {
  fieldName: string
}

// To define a description on a query, mutation or subscription field
[this.DOC_QUERIES || this.DOC_MUTATORS || this.DOC_SUBSCRIPTIONS]: {
  fieldName: string
}

To make writing code easier, the joinLines() template function is
available so your source code can look nice and neat and your descriptions
won't get annoying line breaks and spaces as part of that process.

Source:
Returns:
Type:
Object

an object with various keys and values denoting
description fields that should be applied to the final schema object

(static) ⌾⠀getMergedRoot(requestData) → {Object}

An simple pass-thru method for fetching a types merged root object.

Source:
Parameters:
Name Type Description
requestData Object

an object containing the request data such as
request, response or graphql context info that should be passed along to
each of the resolver creators

Returns:
Type:
Object

the merged root object with all the query, mutation and
subscription resolvers defined and created within.

(static) ⌾⠀getProp(propName, bindGetters, bindTo) → {mixed}

The static version of getProp reads into the prototype to find the field
that is desired. If the field is either a getter or a initializer (see
class properties descriptors), then the option to bind that to either the
prototype object or one of your choosing is available.

Source:
Parameters:
Name Type Description
propName string | Symbol

a string or Symbol denoting the name of
the property or field you desire

bindGetters boolean

true if a resulting getter or initializer
should be bound to the prototype or other object

bindTo mixed

the object to which to bind the getter or
initializer functions to if other than the class prototype.

Returns:
Type:
mixed

a Function or other mixed value making up the property
name requested

(static) ⌾⠀getResolver(resolverName, requestData) → {function}

Resolvers are created in a number of different ways. OOP design
dictates that instances of a created class will handle field
resolvers, but query, mutation and subscription resolvers are
typically what creates these instances.

Since a resolver can be created using @mutator/@subscriptor/@resolver
or via method on a object returned from RESOLVERS(), MUTATORS() or
SUBSCRIPTIONS(), there should be an easy to use way to fetch a
resolver by name; if for nothing else, code reuse.

Pass the name of the resolver to the function and optionally pass a
requestData object. The getMergedRoot() method will build an object
containing all the root resolvers for the type, bound to the supplied
requestData object. It is from this object that resolverName will
be used to fetch the function in question. If one exists, it will be
returned, ready for use. Otherwise, null will be your answer.

Source:
Parameters:
Name Type Description
resolverName string

the name of the resolver as a string

requestData Object

the requestData used to build the
resolver methods from which to choose

Returns:
Type:
function

returns either a function representing the
resolver requested or null if there wasn't one to be found

(static) ⌾⠀IDLFilePath(path, extensionopt)

Creates an appropriate Symbol crafted with the right data for use by
the IDLFileHandler class below.

Source:
See:
  • GQLBase#SCHEMA
Parameters:
Name Type Attributes Default Description
path string

a path to the IDL containing file

extension string <optional>
'.graphql'

an extension, including the
prefixed period, that will be added to the supplied path should it not
already exist.

Returns:

Symbol

(static) ⌾⠀MUTATORS(requestData) → {Promise}

This method should return a promise that resolves to an object of
functions matching the names of the mutation operations. These are to be
injected into the root object when used by GQLExpressMiddleware.

Source:
Parameters:
Name Type Description
requestData Object

typically an object containing three
properties; {req, res, gql}

Returns:
Type:
Promise

a promise that resolves to an object; see above for more
information.

(static) ⌾⠀RESOLVERS(requestData) → {Promise}

This method should return a promise that resolves to an object of
functions matching the names of the query operations. These are to be
injected into the root object when used by GQLExpressMiddleware.

Source:
Parameters:
Name Type Description
requestData Object

typically an object containing three
properties; {req, res, gql}

Returns:
Type:
Promise

a promise that resolves to an object; see above for more
information.

(static) ⌾⠀setupModel(instance)

The internal data model has some custom EventEmitter code wrapped
it here. When the data model is set via setModel or by accessing it
via instance[MODEL_KEY], an event EVENT_MODEL_SET is emitted. Any
listener listening for this event receives an object with two keys

{
  model: The actual model being set; changes are persisted
  instance: The GQLBase instance the model is associated with
}

Subsequently, the events EVENT_MODEL_PROP_CHANGE and
EVENT_MODEL_PROP_DELETE can be listened to if your version of node
supports Proxy objects. They allow you to be notified whenever your
model has a property changed or deleted, respectively.

The callback for change receives an object with four properties

{
  model: The model object the value is being changed on
  old: The old value being replaced; undefined if it is the first time
  key: The property key for the value being changed
  value: The new value being set
}

The callback for delete receives an object with four properties

{
  model: The model object the value is deleted from
  key: The property key for the deleted value
  deleted: The deleted value
}
Source:
Parameters:
Name Type Description
instance GQLBase

typically this as passed in from a call in
the constructor

(static) ⬇︎⠀handler() → {IDLFileHandler}

A file handler for fetching the IDL schema string from the file system
for those GQLBase extended classes that have indicated to do so by
returning a Symbol for their SCHEMA property.

Source:
Returns:
Type:
IDLFileHandler

instance of IDLFileHandler, created if one does
not already exist, for fetching the contents from disk.

(static) ⬇︎⠀SCHEMA() → {string|Symbol}

Defined in a base class, this getter should return either a String
detailing the full IDL schema of a GraphQL handler or one of two
types of Symbols.

The first Symbol type is the constant ADJACENT_FILE. If this Symbol is
returned, the system assumes that next to the source file in question is
a file of the same name with a .graphql extension. This file should be
made of the GraphQL IDL schema definitions for the object types being
created.

Example:

  static get SCHEMA(): string | Symbol {
    return GQLBase.ADJACENT_FILE
  }

The primary advantage of this approach is allowing an outside editor that
provides syntax highlighting rather than returning a string from the
SCHEMA getter.

Alternatively, the static method IDLFilePath can be used to point to an
alternate location where the GraphQL IDL file resides. The extension can
also be changed from .graphql to something else if need be using this
method.

Example:

  static get SCHEMA(): string | Symbol {
    return GQLBase.IDLFilePath('/path/to/file', '.idl')
  }
Source:
See:
  • GQLBase#ADJACENT_FILE
  • GQLBase#IDLFilePath
Returns:
Type:
string | Symbol

a valid IDL string or one of the Symbols
described above.

⌾⠀applyAutoProps()

Since reading the Schema for a given GraphQL Lattice type or
interface is simple enough, we should be able to automatically
apply one to one GraphQL:Model properties.

Source:

⌾⠀callProp(propName, args) → {mixed}

Properties defined for GraphQL types in Lattice can be defined as
a getter, a function or an async function. In the case of standard
functions, if they return a promise they will be handled as though
they were async. In addition to fetching the property, or field
resolver, its resulting function or getter will be invoked.

Given the variety of things a GraphQL type can actually be, obtaining
its value can annoying. This method tends to lessen that boilerplate.
Errors raised will be thrown.

Source:
Parameters:
Name Type Description
propName string

the name of the property in question

args Array.<mixed>

the arguments array that will be passed
to .apply() should the property evaluate to a function

Throws:

errors raised in awaiting results will be thrown

Type
Error
Returns:
Type:
mixed

the return value of any resulting function or
value returned by a getter; wrapped in a promise as all async
functions do.

⌾⠀extendModel(extensions) → {GQLBase}

Uses _.merge() to modify the internal backing data store for the
object instance. This is a shortcut for
_.merge()(instance[MODEL_KEY], ...extensions)

Since:
  • 2.5
Source:
Parameters:
Name Type Description
extensions mixed

n-number of valid _.merge() parameters

Returns:
Type:
GQLBase

this is returned

⌾⠀getModel(value)

Getter for the internally stored model data. The contents of this
object are abstracted away behind a Symbol key to prevent collision
between the underlying model and any GraphQL Object Definition properties.

Since:
  • 2.5
Source:
Parameters:
Name Type Description
value Object

any object you wish to use as a data store

⌾⠀getProp(propName, bindGetters, bindTo) → {mixed}

Properties defined for GraphQL types in Lattice can be defined as
a getter, a function or an async function. In the case of standard
functions, if they return a promise they will be handled as though
they were async

Given the variety of things a GraphQL type can actually be, obtaining
its value can annoying. This method tends to lessen that boilerplate.
Errors raised will be thrown.

Source:
Parameters:
Name Type Description
propName string | Symbol

the name of the property in question

bindGetters boolean

true, by default, if the get or
initializer descriptor values should be bound to the current instance
or an object of the programmers choice before returning

bindTo mixed

the this object to use for binding when
bindGetters is set to true.

Throws:

errors raised in awaiting results will be thrown

Type
Error
Returns:
Type:
mixed

the value of the propName as a Function or something
else when the requested property name exists

⌾⠀getResolver(resolverName, requestData) → {function}

A pass-thru method to the static function of the same name. The
difference being that if requestData is not specified, the
requestData object from this instance will be used to build the
resolvers in question.

Source:
Parameters:
Name Type Description
resolverName string

the name of the resolver as a string

requestData Object

the requestData used to build the
resolver methods from which to choose

Returns:
Type:
function

returns either a function representing the
resolver requested or null if there wasn't one to be found

⌾⠀setModel(value)

Setter for the internally stored model data. The contents of this
object are abstracted away behind a Symbol key to prevent collision
between the underlying model and any GraphQL Object Definition properties.

Since:
  • 2.5
Source:
Parameters:
Name Type Description
value Object

any object you wish to use as a data store

⬆︎⠀requestData(value)

A setter that assigns a value to the inner request data object. When
used with GQLExpressMiddleware, this is an object matching {req, res, gql}.

Source:
Parameters:
Name Type Description
value Object

an object, usually matching { req, res, gql }

⬇︎⠀requestData() → {Object}

A getter that retrieves the inner request data object. When used with
GQLExpressMiddleware, this is an object matching {req, res, gql}.

Source:
Returns:
Type:
Object

an object, usually matching { req, res, gql }

(inner) deleteProperty(target, key)

Proxy deleteProperty() handler. This is where the delete property
events are fired from

Source:
Parameters:
Name Type Description
target Object

the GQLBase model object

key string

the property name

(inner) set(target, key, value)

Proxy set() handler. This is where the change events are fired from

Source:
Parameters:
Name Type Description
target Object

the GQLBase model object

key string

the property name

value mixed

the new property value