A little utility allowing you to write rules for your graphql resolvers.
import GraphqlRules from 'graphql-rules';
const userRules = new GraphqlRules();
userRules.addRule({
name: 'is-logged',
resolver: (obj, args, context) => {
if (!context.userId) {
throw new Error('Forbidden');
}
},
});
userRules.addRule({
name: 'is-admin',
resolver: (obj, args, context) => {
if (!context.user.isAdmin()) {
throw new Error('Forbidden');
}
},
});
const resolverMap = {
Query: {
users: userRules.check({
rules: ['is-admin', 'other-rules', 'is-logged'],
resolver: (obj, args, context) => {
return context.Users.find();
},
}),
},
Mutation: {
addSomeDbObject: userRules.check({
rules: 'is-logged',
resolver: (obj, args, context) => {
return MyModel.insert(context.obj);
},
}),
},
};