Kest

Search

Search the documentation

GraphQL

Field middleware

Wrap every resolver call: timing, logging, caching.

Middleware wraps every resolver method (@Query, @Mutation, @Subscription, @ResolveField). It sees the field and decides what happens around next():

useGraphQL {
    schema = SDL
    middleware { env, next ->
        val start = System.nanoTime()
        try { next() }
        finally { log.info("${env.field.name} took ${(System.nanoTime() - start) / 1_000_000} ms") }
    }
}

Several middleware { } blocks run in the order you add them, the first being outermost. A middleware may change the result, return a cached value without calling next(), or throw (the error reaches the client like any resolver error).

It does not run for plain property reads (a name field on a returned object); only for resolver methods. To affect specific fields only, use a directive.

Read more