npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-router-ware

v0.2.0

Published

GraphQL resolver for using router level middlewares at ease.

Downloads

12

Readme

Graphql-Router-Ware

GraphQL resolver for using router level middlewares at ease.

Why?

  • Easy to use.
  • Actively maintained.
  • Zero dependency.
  • Handles router-level middleware in graphql just like in expressJs.

Install

$ npm install graphql-router-ware

Usage

Import in ES6 or ESM

import Router from 'graphql-router-ware';

// your code here...

Require in CommonJS

const Router = require('graphql-router-ware');

// your code here...

Resolver

import Router from 'graphql-router-ware';
import { checkPermission } from '../helpers/userhalper';
import Controller from '../controllers/page';

const resolvers = {
    Query: {
        singlePage: Router(Controller.singlePage)
    },
    Mutation: {
        createPage: Router(checkPermission,Controller.create),
        updatePage: Router(checkPermission,Controller.update),
    }
}

export default resolvers;

Middleware

// ....
export checkPermission=({ ctx },next)=>{

    if(!ctx.user){
        return next(new Error('Not logged in'));
        // or throw new Error('Not logged in')
    }

    // some more permission checks....
    return next();
};
// ....

Controller

// ....
export create=({ args,ctx })=>{
    // Already all the permissions have been verified...
    
    const page = { ...args, userId: ctx.user }; 
    
    // some more operations....

    return page;
};
// ....

API

Router(...functions)

It can take any number of functions which are called one by one using the next parameter passed in all functions.

next(err?)

It is passed in each of your functions as the last parameter. To continue the chain you need to return this function call at the end of your function

Example

    next() // this will call the next function in chain
    next(err) // this will stop the chain and throws error to graphql

Router-functions({ root, args, ctx, info }, next)

You will recieve all the graphql parameters in the first argument and next function as second argument.

Note : You need to return the next() from your router function to continue the chain.

Contributors:

Credits goes to these people: ✨