GraphQL Middleware is an open-source library from Prisma that lets you run arbitrary code before or after GraphQL resolvers are invoked, using the familiar Express.js middleware pattern. It improves code organization by separating cross-cutting concerns like logging, authorization, and error handling from resolver logic. Middleware functions wrap resolvers, share the same input arguments, and can be applied globally to all resolvers or scoped to specific fields and types. The post also compares GraphQL Middleware to schema directives, noting that middleware is imperative and more flexible in scope. Several community-built libraries on top of it are highlighted, including graphql-shield for permissions and graphql-middleware-sentry for error reporting.

5m read timeFrom prisma.io
Post cover image
Table of contents
Middleware keeps resolvers cleanUnderstanding middleware functionsGraphQL Middleware vs Schema directivesGetting started with graphql-middlewareMade by the awesome GraphQL community