{"id":17671,"library":"graphql-add-middleware","title":"GraphQL Resolver Middleware Utility","description":"The `graphql-add-middleware` package, currently at version 0.3.7, offers a dedicated solution for injecting custom middleware logic directly into GraphQL schema resolvers. It enables developers to apply a function before and after resolver execution, allowing for operations such as logging, authentication checks, argument manipulation, or result transformation. The utility provides granular control, supporting global application to all resolvers, scoped application to all resolvers of a specific GraphQL type (e.g., `Query`, `Mutation`), or precise targeting of a single field within a type (e.g., `Query.posts`). Released in 2017 and last updated around that time, the package is effectively abandoned, which means it has no active development, bug fixes, or compatibility updates for newer GraphQL versions. Its primary differentiator is its singular focus on direct schema modification for middleware injection, rather than being integrated into a larger GraphQL server framework, requiring users to explicitly manage the `GraphQLSchema` object. This narrow scope can be beneficial for those who need a lightweight solution to augment an existing schema without adopting an entire framework.","status":"abandoned","version":"0.3.7","language":"javascript","source_language":"en","source_url":"https://github.com/alekbarszczewski/graphql-add-middleware","tags":["javascript","graphql","middleware","resolver"],"install":[{"cmd":"npm install graphql-add-middleware","lang":"bash","label":"npm"},{"cmd":"yarn add graphql-add-middleware","lang":"bash","label":"yarn"},{"cmd":"pnpm add graphql-add-middleware","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for defining and working with GraphQL schemas, especially for the `GraphQLSchema` object and `buildSchema` function.","package":"graphql","optional":false}],"imports":[{"note":"While README examples show ESM syntax, the package's age (2017) implies CommonJS `require` was also a primary consumption method. Named import is correct.","wrong":"const { addMiddleware } = require('graphql-add-middleware');","symbol":"addMiddleware","correct":"import { addMiddleware } from 'graphql-add-middleware';"}],"quickstart":{"code":"import { buildSchema } from 'graphql';\nimport { addMiddleware } from 'graphql-add-middleware';\n\n// Define a basic GraphQL schema\nconst schemaString = `\n  type User {\n    id: ID!\n    name: String!\n  }\n\n  type Post {\n    id: ID!\n    title: String!\n    author: User\n  }\n\n  type Query {\n    users: [User!]!\n    posts: [Post!]!\n  }\n\n  type Mutation {\n    createUser(name: String!): User!\n  }\n`;\n\nconst schema = buildSchema(schemaString);\n\n// Add a global middleware for logging all resolver calls\naddMiddleware(schema, async (root, args, context, info, next) => {\n  console.log(`[Global Middleware] Entering resolver: ${info.parentType}.${info.fieldName}`);\n  const result = await next(); // Proceed to the next middleware or original resolver\n  console.log(`[Global Middleware] Exiting resolver: ${info.parentType}.${info.fieldName}`);\n  return result; // Ensure the result is returned\n});\n\n// Add middleware specifically to 'Query' type resolvers\naddMiddleware(schema, 'Query', async (root, args, context, info, next) => {\n  console.log(`[Query Middleware] Processing Query.${info.fieldName} with args:`, args);\n  const result = await next();\n  return result;\n});\n\n// Add middleware to a specific field: Mutation.createUser\naddMiddleware(schema, 'Mutation.createUser', async (root, args, context, info, next) => {\n  // Example of authentication check\n  if (!context || !context.userId) {\n    throw new Error('Unauthorized: You must be logged in to create a user.');\n  }\n  console.log(`[CreateUser Middleware] Attempting to create user: ${args.name}`);\n  const newUser = await next();\n  console.log(`[CreateUser Middleware] User created with ID: ${newUser.id}`);\n  return newUser;\n});\n\nconsole.log('GraphQL schema with middleware applied successfully.');\n// The 'schema' object can now be passed to a GraphQL server (e.g., Apollo Server, express-graphql)\n","lang":"javascript","description":"This example demonstrates how to create a basic GraphQL schema and then apply middleware globally, to a specific type (Query), and to a specific field (Mutation.createUser) using `graphql-add-middleware`. It includes logging and a hypothetical authorization check within the middleware functions, showcasing their execution flow."},"warnings":[{"fix":"Consider migrating to a more actively maintained GraphQL middleware solution or a GraphQL server framework with built-in middleware capabilities (e.g., Apollo Server, GraphQL Yoga). If absolutely necessary, pin your `graphql` dependency to a compatible older version and thoroughly test.","message":"The package's peer dependency for `graphql` is `^0.13.2`. Modern `graphql` versions (e.g., v15, v16, v17) have undergone significant internal changes. It is highly probable that `graphql-add-middleware` is incompatible with current `graphql` versions, leading to runtime errors or unexpected behavior due to schema and resolver structure differences.","severity":"breaking","affected_versions":">=0.4.0"},{"fix":"Evaluate alternatives like `graphql-middleware`, `graphql-shield`, or leveraging the middleware features of comprehensive GraphQL server frameworks. If continuing to use, thoroughly test across your stack and be prepared to fork or patch the library yourself.","message":"The package is abandoned, with its last update 7 years ago. This means there will be no future bug fixes, security patches, or compatibility updates for newer Node.js versions, GraphQL specifications, or ecosystem changes. Using it in new projects or upgrading existing ones carries significant risk.","severity":"gotcha","affected_versions":">=0.3.7"},{"fix":"Always use `const result = await next();` and `return result;` within your middleware to correctly pass control and return values. Middleware functions are asynchronous by design.","message":"Middleware functions must explicitly call `await next()` and `return result` to ensure the resolver chain continues and values are propagated. Forgetting either can lead to resolvers not being called, hangs, or `undefined` results.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Ensure you are passing a valid `GraphQLSchema` instance, typically created using `buildSchema` or `makeExecutableSchema`, as the first argument to `addMiddleware`.","cause":"`addMiddleware` was called with an invalid or uninitialized GraphQL schema object.","error":"Error: The \"schema\" argument must be a GraphQLSchema instance."},{"fix":"Verify that your middleware function correctly accepts `(root, args, context, info, next)` as arguments and that `next()` is explicitly called (and `awaited`) to pass control to the next middleware or resolver.","cause":"The `next` function in the middleware signature was either not called, or the middleware function itself was invoked incorrectly outside the `graphql-add-middleware` context.","error":"TypeError: next is not a function"},{"fix":"This often points to a fundamental incompatibility between `graphql-add-middleware` (v0.3.7) and the `graphql` version your server is using. Downgrade `graphql` to `^0.13.2` or migrate away from `graphql-add-middleware` to a modern alternative.","cause":"This error typically originates from a GraphQL server trying to execute a query, indicating that the schema passed to the server is corrupted or incorrectly modified by an incompatible version of `graphql-add-middleware` or `graphql` itself.","error":"Error: Must provide document (query) or AST. Cannot execute request."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}