Skip to content

Instantly share code, notes, and snippets.

@grydstedt
Created September 16, 2015 02:59
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save grydstedt/03ae4806e0af41677f5d to your computer and use it in GitHub Desktop.
Save grydstedt/03ae4806e0af41677f5d to your computer and use it in GitHub Desktop.
/*
export default new GraphQLObjectType({
name: 'User',
description: 'A user',
fields: () => ({
first_name: {
type: GraphQLString,
description: 'The first name of the user.'
},
email: {
type: GraphQLString,
description: 'The email of the user',
resolve: when(or(isAdmin, isSelf))
}
})
});
*/
import P from 'bluebird';
export function when(...conditions) {
return (rootValue, params, info) => {
var results = conditions.map(f => {
let authed = f(rootValue, params, info);
return authed === false ? P.reject() : authed;
});
return P.all(results)
.then(() => rootValue[info.fieldName])
.catch(() => null);
};
}
export function or(...conditions) {
return (rootValue, params, info) => {
var results = conditions.map(f => {
let authed = f(rootValue, params, info);
return authed === false ? P.reject() : authed;
});
return P.any(results);
};
}
export function isAdmin(rootValue, params, info) {
const authedUser = info.rootValue.authedUser;
if (!authedUser) return false;
return authedUser.isAdmin;
};
export function isSelf(rootValue, params, info) {
const authedUser = info.rootValue.authedUser;
if (!authedUser) return false;
return authedUser._id.toString() === rootValue._id.toString();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment