1.0.3 • Published 7 years ago
graphql-resolvers-chain v1.0.3
DEPRECATED
This project has been renamed to graphql-resolved.
Install using npm install graphql-resolved instead.
graphql-resolvers-chain
graphql-resolvers-chain is a framework/server agnostic resolvers creation tool for GraphQL
This project provide a middleware-like approach to GraphQL servers enabling the composition of resolvers chains through a simple and expressive API.
Installing
npm
npm i graphql-resolvers-chain --saveyarn
yarn add graphql-resolvers-chainUsage
Create your first resolver using the chain, protect or apply api
chain api
Create a new resolver chaining the ones in the supplied array. The order matters from left to right.
The last resolver being the one return the final expect value.
chain([1, 2, 3, 4])
import { chain } from 'graphql-resolvers-chain';
import { isAuthenticated, isAdmin, isTopContributer } from './auth/resolvers';
import * as UserResolvers from './user/resolvers';
const getMe = chain([isAuthenticated, UserResolvers.getMe])
const Query = {
getMe
}
const resolvers = {
Query,
}apply api
Applies all the resolvers to all of those in the to object.
resolvers(The order matters from left to right)
const allUserResolvers = apply({ resolvers: [isAuthenticated], to: UserResolvers })
const Query = {
...allUserResolvers
}protect api
The protect key work take three arguments:
it(Optional) a resolver function, if suppliedprotectreturns a new resolver functionall(Optional) an object with resolvers, if suppliedprotectreturns an object with the exact same resolvers names as keysusinga list of resolvers to apply(The order matters from left to right).
const getMe = protect({
it: UserResolvers.getMe,
using: [isAuthenticated]
});
const adminResolvers = protect({
all: { PostResolvers.deletePost, PostResolvers.correctPost },
using: [isAuthenticated, isAdmin]
});
const Query = {
getMe
...adminResolvers
}