GraphQL Combine Resolvers
A library to simplify the development of GraphQL resolvers.

Installation
This package is available on npm as: graphql-resolvers
npm install graphql-resolvers
You should consider using yarn, though.
Motivation
Many times we end-up repeating lots of logic on our resolvers. Access control, for instance, is something that can be done in the resolver level but just tends to end up with repeated code, even when creating services for such a task. This package aims to make it easier to build smart resolvers with logic being reusable and split in small pieces.
How to use it
This library currently consists of single but well tested helper function for combining other functions in a first-result-returns manner. GraphQL resolvers are just one kind of functions to benefit from this helper. Here is an example usage with resolver maps and graphql.js:
import { graphql } from 'graphql'
import { makeExecutableSchema } from 'graphql-tools'
import { skip, combineResolvers } from 'graphql-resolvers'
const typeDefs = `
type Query {
sensitive: String!
}
schema {
query: Query
}
`
const isAuthenticated = (root, args, { user }) => user ? skip : new Error('Not authenticated')
const isAdmin = combineResolvers(
isAuthenticated,
(root, args, { user: { role } }) => role === 'admin' ? skip : new Error('Not authorized')
)
const sensitive = combineResolvers(
isAdmin,
(root, args, { user: { name } }) => 'shhhh!'
)
const resolvers = { Query: { sensitive } }
const schema = makeExecutableSchema({ typeDefs, resolvers })
graphql(schema, '{ sensitive }', null, { }).then(console.log)
graphql(schema, '{ sensitive }', null, { user: { role: 'some-role' } }).then(console.log)
graphql(schema, '{ sensitive }', null, { user: { role: 'admin' } }).then(console.log)