Class: Stitcher

apollo-stitcher~Stitcher(options)

Class encapsulating interactions with a graphql data source.

Constructor

new Stitcher(options)

Parameters:
Name Type Description
options object options for interacting with the graphql data source.
Properties
Name Type Attributes Default Description
schema object an executable schema providing access to the graphql data source, possibly created via use of makeExecutableSchema or makeRemoteExecutableSchema from the graphql-tools package.
transforms Array.<object> <optional>
additional transforms to be added on schema delegation. Transforms can be added here on initial Stitcher creation or later on a per "stitch" basis.
preStitchFragmentName string <optional>
PreStitch a string representing the fragment name for the pseudo-fragment representing the pre-"stitch" selection set, to be used in conjunction with the selectionSet option within the "from" and "to" methods.
Examples
// execute query on targetSchema directly without working from current
// info object.
myStitcher.execute({operation, fieldName, selectionSet, args})
// stitch current query to targetSchema, i.e. pass the remaining
// selectionSet to the target schema
myStitcher.from(info).delegateTo({operation, fieldName, args})
// stitch current query to targetSchema, transform prior to delegation
myStitcher.from(info).transform({selectionSet, result}).delegateTo({operation, fieldName, args})
// For code re-use:
// 1. derive a custom Stitcher class from the base Stitcher class
// 2. add a custom delegator with any required transforms
myStitcher.from(info).delegateToFieldWithTransforms(args)

Methods

delegateTo(options) → {Promise}

Delegates to the configured target schema.
Parameters:
Name Type Description
options object options for schema delegation to the specified target field.
Properties
Name Type Attributes Description
operation string one of 'query', 'mutation', or 'subscription'.
fieldName string root field name on target schema.
args object <optional>
named arguments for the query.
transforms Array.<object> <optional>
additional transforms to be added for this "stitch."
Returns:
a promise that will resolve to the graphql result.
Type
Promise

execute(options) → {Promise}

Directly executes a query without stitching an info object.
Parameters:
Name Type Description
options object options for schema delegation to the specified target field.
Properties
Name Type Attributes Description
operation string one of 'query', 'mutation', or 'subscription'.
fieldName string root field name on target schema.
args object <optional>
named arguments for the query.
selectionSet string | object | function <optional>
a selection set specified as graphql SDL or as an AST.
result function <optional>
an optional function to transform the result.
transforms Array.<object> <optional>
additional transforms to be added for this "stitch."
Returns:
a promise that will resolve to the graphql result.
Type
Promise

from(infoopt, optionsopt) → {Stitcher}

Creates a new Stitcher object based on the original Stitcher object settings. This function is designed to allow adding the query information prior to stitching. It takes a single parameter, either an info object or an options object with the info property set. When specifying no other options, one can pass the info object itself instead of the options object.
Parameters:
Name Type Attributes Description
info object <optional>
a graphql info object providing information about the query execution.
options object <optional>
an options object with an info property set to the graphql info object.
Properties
Name Type Attributes Description
info object a graphql info object providing information about the query execution.
context object <optional>
a graphql context object to be passed to the executable schema. If the Stitcher class is used as a datasource, the context will be automatically provided on data source initialization, but can be overridden.
Returns:
a Stitcher object instance for chaining.
Type
Stitcher

transform(options) → {Stitcher}

Transform the selection set prior to delegation. Options can be specified to transform the requested selection set prior to delegation and/or to reverse the transformation on receipt of the result. Multiple transformations can be performed; selection set transformations are applied sequentially, while result tranformation reversals are processed in reverse order.
Parameters:
Name Type Description
options object options for selectionSet transformation.
Properties
Name Type Attributes Description
selectionSet string | object | function a selection set specified as graphql SDL or as an AST, in which references to a pseudo-fragment named PreStitch will be expanded with the pre-"stitch" selection set. Alternatively, selectionSet may represent a function that takes the pre-"stitch" selection set AST as an argument and returns a post-"stitch" selection set. For example, a selection set can be specified to wrap fields prior to delegation, or to add fields.
result function <optional>
an optional function to reverse the transformation. For example, if the selection set transformation involves wrapping the selection set prior to delegation, this option can be used to automatically unwrap the result.
Returns:
a Stitcher object instance for chaining.
Type
Stitcher
Example
myStitcher.from(info).transform({selectionSet, result}).delegateTo({operation, fieldName, args})
*