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 🙏

© 2025 – Pkg Stats / Ryan Hefner

twobyfour

v0.0.16

Published

graphql wrapper for making validation, generation and permissions easier

Readme

twobyfour

npm version Build Status

Being agnostic of all business logic, graphql expects you to put everything inside the resolve functions. This means validation, permissions, analytics, business logic, etc. We wanted to put these things right next to the appropriate fields, so they are easier to understand and act on. So twobyfour is a graphql schema wrapper that allows you to decorate any field with functions that will be wrapped around your primary resolve (including managing arguments). It automatically walks the schema tree, looking for the appropriate decorators and updates the resolve functions automagically. There is an example below.

Installation

npm install twobyfour --save

Usage

twobyfour(schema, config)
/*
    schema - A regular graphql schema, with fields decorated according to the config
    config - config objects specifying decorator names and when they apply
*/

config

There is a sample config in the example below, but basically, the config is just an object that contains an array of decorator names for three possible types: - args: promise chains that are run before all others for any given arguments. - pre: promise chains that are run before the primary resolve function. - post: promise chains that are run after the primary resolve function (Note: these resolvers are run asynchronously to the return of the primary resolve. This was so that post resolvers don't delay any reply to a client).

resolvers

Say you added the key validators to the args decorator list for the twobyfour config. To add promise chains to be run on arguments, simply add a single function or an array of functions using that decorator key on any argument. For example:

args: {
  my_arg: {
    type: GraphQLString,
    validators: [isLength(1, 50), matches(/^[\w\d]+(?:-[\w\d]+)*$/)]

In the example above, isLength and matches are both higher order functions that return functions that match the regular graphql resolve pattern resolve(root, args, context, info). NOTE: Argument resolvers are slightly different in that they also include the current argument name as the key arg on the info object parameter.

Example

Imagine you had the following basic type/query in graphql:

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      hello: {
        args: {
          myArg: {
            type: GraphQLString
          }
        },
        type: GraphQLString,
        resolve() {
          // ... return some string
          return 'I am the result'
        }
      }
    }
  })
})

Now let's say you want to put validation on the argument, like a regex match or length check, and put permissions on the query itself. Normally you would need to put all this work in the resolve function of the query, but now you can do the following in twobyfour:

const schema = twobyfour(new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      hello: {
        args: {
          myArg: {
            type: GraphQLString,
            validators: [isLength(1, 50), matches(/^[\w\d]+(?:-[\w\d]+)*$/)]
          }
        }
        type: GraphQLString,
        permissions: isAdmin,
        resolve() {
          // ... return some string
          return 'I am the result'
        }
      }
    }
  })
}), {
  args: ['validators'],
  pre: ['permissions']
})

This might look a little magic, but here is how it works.

  1. Define your schema as per usual, but decorate any field with a function or array of functions that match the same interface as the regular graphql resolve function. For example:
const isLength = (min, max) => (root, args, context, info) => {
  // arguments get their name put on the info object
  const arg = args[info.arg]
  if(arg.length < min || arg.length > max){
    return Promise.reject(new Error('String is an invalid length'))
  }
}

// on an argument field
hello: {
  args: {
    myArg: {
      type: GraphQLString,
      validators: isLength(5, 10)
    }
  }
}
  1. Wrap your graphql schema in twobyfour(), passing in a config object detailing to twobyfour which words represent decorators to be run at which time in the resolve lifecycle. For example:
twobyfour(mySchema, {
  args: ['validators'] // tell twobyfour to look for the 'validators' decorator on any arguments
})

Extras

Included in twobyfour are promise chain helpers. For now it is just or(). This allows you to allow a logical or in your promise chain. For example:

import { or } from twobyfour
...
// inside your type/query/mutation
args: {
  test: {
    validators: [or(isAdmin, someOtherPermissionFunction)]

This causes the promise chain to fulfill if any of the given promises are resolved.