dist

graphql-middleware

Actions Status codecov npm version

Split up your GraphQL resolvers in middleware functions.

Overview

GraphQL Middleware is a schema wrapper which allows you to manage additional functionality across multiple resolvers efficiently.

  • 💡 Easy to use: An intuitive, yet familiar API that you will pick up in a second.

  • 💪 Powerful: Allows complete control over your resolvers (Before, After).

  • 🌈 Compatible: Works with any GraphQL Schema.

NOTE: As of 3.0.0 graphql-middleware no longer wraps introspection queries.

NOTE: As of 5.0.0 graphql-middleware no longer supports GraphQL Yoga out of the box. We might bring back the support if the library becomes maintained again. We are keeping the docs as the reference for older versions.

Install

How does it work

GraphQL Middleware lets you run arbitrary code before or after a resolver is invoked. It improves your code structure by enabling code reuse and a clear separation of concerns.

Execution of the middleware and resolver functions follow the "onion"-principle, meaning each middleware function adds a layer before and after the actual resolver invocation.

The order of the middleware functions in the middlewares array is important. The first resolver is the "most-outer" layer, so it gets executed first and last. The second resolver is the "second-outer" layer, so it gets executed second and second to last... And so forth.

You can read more about GraphQL Middleware in this fantastic article.

Standalone usage

Usage with graphql-yoga

graphql-yoga has built-in support for graphql-middleware!

Awesome Middlewares Awesome

API

A middleware is a resolver function that wraps another resolver function.

Middleware Generator

In some cases, your middleware could depend on how your schema looks. In such situations, you can turn your middleware into a middleware generator. Middleware generators are denoted with function middleware and receive schema as the first argument.

Middleware Fragments

Fragments are a way of expressing what information your resolver requires to make sure it can execute correctly. They are primarily used in schema forwarding when the client might not always request all the fields your resolver demands. Because of that, we need to provide a way of telling what other information we need from a remote schema and that's why we use fragments.

You can read more about fragments in the graphql-binding repository and on graphql-tools documentation website.

GraphQL Middleware provides a convenient way to quickly and easily add fragments to your middleware. This might turn out particularly useful when your middleware depends on resolver data.

We've made fragments extremely flexible by using the general API which, if you have ever run over fragments, you probably already know.

graphql-middleware automatically merges fragments from multiple middlewares if possible. Otherwise, validation function throws an error.

GraphQL Middleware Use Cases

  • Logging

  • Metrics

  • Input sanitisation

  • Performance measurement

  • Authorization

  • Caching

  • Tracing

FAQ

Can I use GraphQL Middleware without GraphQL Yoga?

Yes. Nevertheless, we encourage you to use it in combination with Yoga. Combining the power of middlewares that GraphQL Middleware offers, with documentMiddleware which Yoga exposes, gives you unparalleled control over the execution of your queries.

How does GraphQL Middleware compare to directives?

GraphQL Middleware and directives tackle the same problem in a completely different way. GraphQL Middleware allows you to implement all your middleware logic in your code, whereas directives encourage you to mix schema with your functionality.

Should I modify the context using GraphQL Middleware?

GraphQL Middleware allows you to modify the context of your resolvers, but we encourage you to use GraphQL Yoga's documentMiddleware for this functionality instead.

Thank you

Thanks to everyone who supported the development of this project. It's an honor to lead a project that helps so many people.

  • Prisma - for sponsoring the project,

  • Johannes Schickling - for guiding the project development, and

  • everyone else who personally contributed to the project in one way or another.

Thank you! ❤️

Last updated