GithubHelp home page GithubHelp logo

graphql-chain's Introduction

GraphQL Chain

Create GraphQL middleware that resembles how Express middleware works.

Install

yarn add graphql-chain

How to use

Step 1

Create middleware

const validationMiddleware: MiddlewareResolver = (
  next,
  parent,
  args,
  context,
  info
) => {
  if (args.name.length > 10) {
    throw new Error("too long");
  }

  return next();
};

It has access to all the regular parameters a resolver gets plus a next parameter at the beginning which you call and return to call the next middleware or resolver.

You don't always have to call the next middleware or resolver though. This can be helpful if you want create a caching middleware:

const cachingMiddleware: MiddlewareResolver = async (next, _, args) => {
  const data = await redis.get("hello:cache");
  if (data) {
    // found data in the cache, so return early
    console.log("CACHE HIT");
    return data;
  }
  // did not find data, so call the next middleware
  console.log("CACHE MISS");
  const result = await next();
  // set cache for next call
  await redis.set("hello:cache", result);
  return result;
};

You can also change the value of the arguments and then use them later

const getUserMiddleware: MiddlewareResolver = async (next, _, __, context) => {
  if (validToken(context.authToken)) {
    // you can add properties to context that you can use later
    context.user = await getUser(context.authToken);
  }

  return next();
};

So you can then have a middleware that checks the user

const authorizationMiddleware: MiddlewareResolver = (next, _, __, context) => {
  if (!context.user || !context.user.admin) {
    throw new Error("not authorized");
  }

  return next();
};

Step 2

Chain as many middlewares together as you like

import { chain } from "graphql-chain";

const helloMiddleware = chain([
  getUserMiddleware,
  authorizationMiddleware,
  validationMiddleware
]);

Step 3

Wrap the resolver you want the middleware to run on

const resolvers: IResolvers = {
  Query: {
    hello: helloMiddleware((_, { name }) => `Hello ${name || "World"}`)
  }
};

The execution sequence will be getUserMiddleware -> authorizationMiddleware -> validationMiddleware -> hello

Checkout the examples directory for complete examples

graphql-chain's People

Contributors

benawad avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.