How to extend graphql query functionality with middleware

Middleware provide an efficient way to manage additional functionality accross your query resolvers. Tensei makes use of the popular graphql-middleware library to support middleware.

Registering middleware

To add middleware to a specific graphql query, you may use the .middleware(...) function on the GraphqlQuery instance:

const { tensei, graphQlQuery } = require('@tensei/core')

        graphQlQuery('Get Purchases')
            .middleware(async (resolve, parent, args, ctx, info) => {
                const result = await resolve(parent, args, ctx, info)

                return result
            .handle(async (parent, args, ctx, info) => ctx.user.purchases)

You may also add middleware to already existing queries by using the getQuery() method in the tensei.register() hook:

const { tensei } = require('@tensei/core')

    .register(({ getQuery }) => {
            ?.middleware(async (resolve, ...rest) => {
                return resolve(...rest)

Global middleware

The .middleware() method registers middleware only for a specific query. If you want to register a global middleware, you can use a simple plugin. Plugins have access to the .extendGraphQlMiddleware() method. For example, this is how you would register the graphql pino middleware in your tensei application:

const { tensei, plugin } = require('@tensei/core')

const pino = require('pino')
const graphqlPinoMiddleware = require('graphql-pino-middleware')

        plugin('Add Global Middleware')
            .register(({ extendGraphQlMiddleware }) => {
                    logger: pino()