A plugin for using remote schemas with Mercurius.
npm i @autotelic/mercurius-remote-schema
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 }])
mercurius-remote-schema accepts the following optional configuration:
-
-
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
-
-
- Options object to be passed to stitchSchemas
-
- 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
-
- A subschema configuration object. Note
schema
is pre-configured and will not be overridden.
- A subschema configuration object. Note
Note All decorators are added to the graphql
namespace.
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.
May be invoked to trigger a refetch of all registered remote schemas and re-stitch the service schema.
Used to enable automatic polling and refreshing of remote schemas.
It accepts the following arguments:
interval
: The polling interval (in milliseconds)
Used to stop the automated remote schema refresh behavior.