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-directives-middlewares

v0.2.2

Published

GraphQL directives as middlewares

Downloads

7

Readme

graphql-directives-middlewares

GraphQL directives as middlewares

npm bundle size GitHub Workflow Status NPM Version Coveralls github

install

yarn add graphql-directives-middlewares

You need to have graphql and graphql-tools installed on your project.

why

We create this library because we needed our directives to be sorted at runtime. We were on the case we needed to execute our @auth directive BEFORE our @api directive take place.

With graphql-directives-middlewares you just have to declare, in your schema, the directives in order you want them to take place.

With this declaration:

type Query {
  users: [User] @auth(requires: ADMIN) @api(name: "users")
}

Your @auth directive will be called BEFORE the @api one.

If you invert @auth and @api, then @api directive will be called BEFORE the @auth, without changing your implementation!

API

createVisitFieldDefinition

createVisitFieldDefinition(name: string, impl: (params, next) -> (...args))

  • name: the directive name you use in your gql schema
  • impl: is a function that take params and next and return a function that is your graphql custom resolver
    • params are your directives arguments
    • next is the next resolver to call, like in a middleware engine

createVisitObject

createVisitObject(name: string, impl: (params, next) -> (...args))

  • name: the directive name you use in your gql schema
  • impl: is a function that take params and next and return a function that is your graphql custom resolver
    • params are your directives arguments
    • next is the next resolver to call, like in a middleware engine

usage

Example with a @auth directive

  1. define your directive in the schema
export default `
  enum Role {
    ADMIN
    USER
    VIEWER
  }

  directive @auth(requires: Role = ADMIN) on FIELD_DEFINITION
`
  1. create your directive implementation
import { GraphQLList } from 'graphql'
import { createVisitFieldDefinition } from 'graphql-directives-middlewares'

export default createVisitFieldDefinition(
  'auth',
  (params, next) => async (...args) => {
    const { requires: requiredRole } = params

    if (!requiredRole) {
      return next()
    }

    const [, , context, definition] = args
    const { role } = context

    if (!role.includes(requiredRole)) {
      if (definition.returnType instanceof GraphQLList) return []
      throw new Error('Unauthorized')
    }

    return next()
  },
)
  1. bind your directives implementation to your schema
import { makeExecutableSchema } from 'graphql-tools'
import typeDefs from './types'
import auth from './auth'

export default () => {
  const resolvers = {
    Query: {
      users: () => [
        {
          id: 'fabien-juif-1',
          fullName: 'Fabien JUIF',
        },
      ],
    },
  }

  return makeExecutableSchema({
    typeDefs,
    resolvers,
    schemaDirectives: {
      auth,
    },
  })
}
  1. use your directives
import { gql } from 'apollo-server'
import user from './user'

export default gql`
  ${user}

  type Query {
    users: [User] @auth(requires: ADMIN)
  }
`