A plugin for mercurius federation
npm install @mercuriusjs/federationA module to add Apollo Federation v1 metadata info to a schema.
``bash`
npm i fastify @mercuriusjs/federation
`js
const Fastify = require('fastify')
const { mercuriusFederationPlugin } = require('@mercuriusjs/federation')
const users = {
1: {
id: '1',
name: 'John',
username: '@john'
},
2: {
id: '2',
name: 'Jane',
username: '@jane'
}
}
const app = Fastify()
const schema =
extend type Query {
me: User
}
type User @key(fields: "id") {
id: ID!
name: String
username: String
}
const resolvers = {
Query: {
me: () => {
return users['1']
}
},
User: {
__resolveReference: (source, args, context, info) => {
return users[source.id]
}
}
}
app.register(mercuriusFederationPlugin, {
schema,
resolvers
})
app.get('/', async function (req, reply) {
const query = '{ _service { sdl } }'
return app.graphql(query)
})
app.listen({ port: 3000 })
`
Instead of using the plugin, the federation schema can be built using the buildFederationSchema function and passing the schema generated to mercurius.
`javascript
const Fastify = require('fastify')
const mercurius = require('mercurius')
const { buildFederationSchema } = require('../')
...
app.register(mercurius, {
schema: buildFederationSchema(schema),
resolvers,
graphiql: true
})
...
`
A fastify plugin to create a mercurius server that expose the federation directives.
`javascript
const { mercuriusFederationPlugin } = require('@mercuriusjs/federation')
const schema = ...
const resolvers = ...
const app = Fastify()
app.register(mercuriusFederationPlugin, {
schema,
resolvers
})
`
#### options
Uses the same options of mercurius butstring
it requires a , DocumentNode or an Array of DocumentNode for schema attribute.
Create a schema object that can be used in a federated environment
(schema, opts) => GraphQLSchema
- schema string | DocumentNode | Arrayopts
- object:isGateway
- boolean: If enabled create a schema compatible with the gateway`, Default 'false'