Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphql-middleware

Package Overview
Dependencies
Maintainers
6
Versions
107
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-middleware

GraphQL Middleware done right!

  • 6.1.35
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
6
Created

What is graphql-middleware?

The graphql-middleware package allows you to apply middleware functions to your GraphQL resolvers, enabling you to add custom logic before or after the execution of a resolver. This can be useful for tasks such as authentication, logging, error handling, and more.

What are graphql-middleware's main functionalities?

Authentication Middleware

This example demonstrates how to use graphql-middleware to add an authentication check to a GraphQL resolver. If the user is not authenticated, an error is thrown.

const { makeExecutableSchema } = require('@graphql-tools/schema');
const { applyMiddleware } = require('graphql-middleware');

const typeDefs = `
  type Query {
    secret: String
  }
`;

const resolvers = {
  Query: {
    secret: () => 'This is a secret message'
  }
};

const schema = makeExecutableSchema({ typeDefs, resolvers });

const isAuthenticated = async (resolve, parent, args, context, info) => {
  if (!context.user) {
    throw new Error('Not authenticated');
  }
  return resolve(parent, args, context, info);
};

const schemaWithMiddleware = applyMiddleware(schema, isAuthenticated);

Logging Middleware

This example shows how to use graphql-middleware to log messages before and after a resolver is executed. This can be useful for debugging and monitoring purposes.

const { makeExecutableSchema } = require('@graphql-tools/schema');
const { applyMiddleware } = require('graphql-middleware');

const typeDefs = `
  type Query {
    hello: String
  }
`;

const resolvers = {
  Query: {
    hello: () => 'Hello world'
  }
};

const schema = makeExecutableSchema({ typeDefs, resolvers });

const logger = async (resolve, parent, args, context, info) => {
  console.log('Request started');
  const result = await resolve(parent, args, context, info);
  console.log('Request finished');
  return result;
};

const schemaWithMiddleware = applyMiddleware(schema, logger);

Error Handling Middleware

This example demonstrates how to use graphql-middleware to catch and handle errors that occur during the execution of a resolver. The error is logged, and a generic error message is returned to the client.

const { makeExecutableSchema } = require('@graphql-tools/schema');
const { applyMiddleware } = require('graphql-middleware');

const typeDefs = `
  type Query {
    throwError: String
  }
`;

const resolvers = {
  Query: {
    throwError: () => { throw new Error('An error occurred'); }
  }
};

const schema = makeExecutableSchema({ typeDefs, resolvers });

const errorHandler = async (resolve, parent, args, context, info) => {
  try {
    return await resolve(parent, args, context, info);
  } catch (error) {
    console.error('Error:', error);
    throw new Error('Internal server error');
  }
};

const schemaWithMiddleware = applyMiddleware(schema, errorHandler);

Other packages similar to graphql-middleware

Keywords

FAQs

Package last updated on 07 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc