@esco/graphql-add-middleware

0.1.5 • Public • Published

graphql-add-middleware Build Status

Easily add middleware to GraphQL schema resolvers

Installation

$ npm install --save graphql-add-middleware

Features

  • Add middleware to all schema resolvers
  • Add middleware to resolvers of given type
  • Add middleware to resolver of given field of given type

Usage

type User {
  name: String!
}

type Post {
  title: String!
  author: User
}

type Query {
  posts: [Post!]!
  user: User
}

type Mutation {
  createUser: User!
}

schema {
  query: Query
  mutation: Mutation
}
import { addMiddleware } from 'graphql-add-middleware';

// add middleware to ALL resolvers (also to nested resolver if they are defined in schema like Post.author)
addMiddleware(schema, async function (root, args, context, info, next) {
  // you can modify root, args, context, info
  const result = await next();
  // you can modify result
  return result; // you must return value
});

// add middleware only to given type
addMiddleware(schema, 'Query', async function (root, args, context, info, next) { ... }); // will add middleware to Query.posts and Query.user
addMiddleware(schema, 'Mutation', async function (root, args, context, info, next) { ... }); // will add middleware to Mutation.createUser
addMiddleware(schema, 'Post', async function (root, args, context, info, next) { ... }); // will add middleware to Post.author (Post.*)

// add middleware only to given type/field
addMiddleware(schema, 'Query.posts', async function (root, args, context, info, next) { ... }); // will add middleware to Query.posts
addMiddleware(schema, 'Post.author', async function (root, args, context, info, next) { ... }); // will add middleware to Post.author

Package Sidebar

Install

npm i @esco/graphql-add-middleware

Weekly Downloads

0

Version

0.1.5

License

ISC

Unpacked Size

7.01 kB

Total Files

4

Last publish

Collaborators

  • esco