graphql-middleware is a schema wrapper designed to allow developers to compose reusable middleware functions around GraphQL resolvers. This utility enables the execution of arbitrary code both before and after a resolver is invoked, facilitating tasks such as argument modification, result transformation, logging, authentication, and error handling. The current stable version, 6.1.35, demonstrates ongoing maintenance with recent updates focused on bug fixes and dependency compatibility, particularly with newer GraphQL versions. Its release cadence is primarily driven by these maintenance needs rather than frequent new feature introductions. Key differentiators include its intuitive API, which offers complete control over the resolver lifecycle, and its broad compatibility with any standard GraphQL schema, integrating seamlessly with popular GraphQL server implementations like Apollo Server. This library promotes a clear separation of concerns, improving code structure by centralizing cross-cutting logic that would otherwise be duplicated across multiple resolvers. Developers can define middleware at various levels, from global application to specific fields, following an an "onion"-like execution principle.
npm install graphql-middlewareVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to apply both function-based and object-based middleware to an executable GraphQL schema using Apollo Server, showcasing the 'onion' execution principle and resolver modification capabilities.
If introspection query interception is required, custom logic must be implemented outside graphql-middleware, or an older version (pre-3.0.0) must be used, though this is not recommended due to other potential issues.
For GraphQL Yoga users, refer to Yoga's documentation on schema wrapping or custom plugin integration to apply graphql-middleware manually. Older versions might retain direct Yoga support but are not recommended for new projects.
Carefully consider the order of your middleware functions. Middleware that needs to execute before others (e.g., authentication) should be placed earlier in the array passed to `applyMiddleware`.
When modifying `args` or `context`, consider immutability patterns where possible (e.g., creating new objects with spread syntax). Test thoroughly to ensure changes don't unintentionally impact other parts of your GraphQL execution flow.
Ensure that the first argument passed to `applyMiddleware` is the result of `makeExecutableSchema` or `buildSchema`, or another function that produces a `GraphQLSchema` instance.
Verify that object-based middleware matches the schema structure (e.g., `Query`, `Mutation`, `Type.field`). For function-based middleware, always include `await resolve(root, args, context, info)` to ensure the next layer or the actual resolver is called.
Run `npm install graphql-middleware` or `yarn add graphql-middleware`. For TypeScript, ensure `tsconfig.json` includes `node_modules/@types` in its `typeRoots` (though usually default) and that the package is correctly installed.