Middleware

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:

import { tensei, graphQlQuery } from '@tensei/core'

tensei()
    .graphQlQueries([
        graphQlQuery('Get Purchases')
            .query()
            .path('get_customer_purchases')
            .middleware(async (resolve, parent, args, ctx, info) => {
                console.time('resolver_execution_time')
                const result = await resolve(parent, args, ctx, info)
                console.timeEnd('resolver_execution_time')

                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:

import { tensei } from '@tensei/core'

tensei()
    .register(({ getQuery }) => {
        getQuery('createComments')
            ?.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:

import { tensei, plugin } from '@tensei/core'

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

tensei()
    .plugins([
        plugin('Add Global Middleware')
            .register(({ extendGraphQlMiddleware }) => {
                extendGraphQlMiddleware(graphqlPinoMiddleware({
                    logger: pino()
                }))
            })
    ])