@autotelic/mercurius-remote-schema

A plugin for fetching and stitching remote schemas with mercurius

Usage no npm install needed!

<script type="module">
  import autotelicMercuriusRemoteSchema from 'https://cdn.skypack.dev/@autotelic/mercurius-remote-schema';
</script>

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

  • stitchSchemaOpts

  • 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.
  • localSubschemaOpts

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.