Cached parsing and validation
Query parsing and validation results are cached, so repeated operations skip straight to execution.
Mercurius is a GraphQL adapter for Fastify. Register the plugin, hand it a schema and resolvers, and you get cached parsing, automatic loaders, a JIT executor, subscriptions and federation out of the box.
npm i fastify mercurius graphql'use strict'
const Fastify = require('fastify')
const mercurius = require('mercurius')
const app = Fastify()
const schema = `
type Query {
add(x: Int, y: Int): Int
}
`
const resolvers = {
Query: {
add: async (_, { x, y }) => x + y
}
}
app.register(mercurius, {
schema,
resolvers
})
app.get('/', async function (req, reply) {
const query = '{ add(x: 2, y: 2) }'
return reply.graphql(query)
})
app.listen({ port: 3000 })Run it, then open http://localhost:3000/graphiql.
// core features
Mercurius sits on the Fastify plugin system, so hooks, decorators and encapsulation work exactly the way they do in the rest of your app.
Query parsing and validation results are cached, so repeated operations skip straight to execution.
Automatic loader integration batches nested resolvers and avoids the 1 + N query problem without extra wiring.
Resolvers are compiled with graphql-jit for execution that is much faster than the reference graphql-js executor.
First-class subscriptions with graphql-ws and subscriptions-transport-ws, including custom pub/sub backends.
Build Apollo-compatible federated services with @mercuriusjs/federation, and compose them with @mercuriusjs/gateway, subscriptions included.
Accept batched operations in a single request and configure persisted queries to cut payload size on the wire.
// ecosystem
Start with a single server, split it into federated services later. Type-safety, testing and tracing are covered by the companion packages and the documented integrations.
Browse all integrations// acknowledgements
The project is kindly sponsored by
Start from the introduction, browse the examples, or open a good first issue if you'd rather contribute.