@autotelic/mercurius-remote-schema
v0.1.2
Published
A plugin for fetching and stitching remote schemas with mercurius
Downloads
7
Maintainers
Readme
mercurius-remote-schema
A plugin for using remote schemas with Mercurius.
Usage
npm i @autotelic/mercurius-remote-schema
Example
const remoteSchema = require('@autotelic/mercurius-remote-schema')
function myPlugin (fastify, opts) {
// Create an [executor](https://www.graphql-tools.com/docs/remote-schemas/#creating-an-executor) for interacting with the remote schema.
const createExecutor = (url) => async ({ document, variables }) => {
const query = print(document);
const { body } = await got.post(url, {
json: { query, variables },
responseType: 'json'
});
return body
};
const executor = createExecutor('http://example.com/graphql')
// Introspect the remote schema and stitch it together with the existing mercurius
// service schema.
fastify.register(remoteSchema, {
subschemas: [{ executor }]
})
// Once the remote schema plugin has been loaded, you may also use the graphql.
// addRemoteSchemas decorator.
fastify.graphql.addRemoteSchemas([{ someOtherRemoteSchemaExecutor }])
API
Plugin options
mercurius-remote-schema accepts the following optional configuration:
subschemas
An array of subschema configuration objects
| field | description | required | |-------|-------------|----------| | executor | A graphql remote schema executor | yes | |transforms| An array of graphql transforms | no | | subscriber | A graphql subscriber | no |
stitchSchemaOpts
- Options object to be passed to stitchSchemas
pollingInterval
- The interval (in milliseconds) in which service should poll the remote services to refresh its schema. If left undefined there will be no automated refresh behavior
configured unless the
autoRefreshRemoteSchemas
decorator is used.
- The interval (in milliseconds) in which service should poll the remote services to refresh its schema. If left undefined there will be no automated refresh behavior
configured unless the
localSubschemaOpts
- A subschema configuration object. Note
schema
is pre-configured and will not be overridden.
- A subschema configuration object. Note
Decorators
Note All decorators are added to the graphql
namespace.
addRemoteSchemas
mercurius-remote-schema
adds a fastify.graphql.addRemoteSchemas
decorator to allow
adding additional remote schemas after plugin initialization.
It accepts the following arguments:
subschemas
: An array of subschema configuration objects.
refreshRemoteSchemas
May be invoked to trigger a refetch of all registered remote schemas and re-stitch the service schema.
autoRefreshRemoteSchemas
Used to enable automatic polling and refreshing of remote schemas.
It accepts the following arguments:
interval
: The polling interval (in milliseconds)
stopAutoRefreshRemoteSchemas
Used to stop the automated remote schema refresh behavior.