Skip to content

Instantly share code, notes, and snippets.

@mugli
Forked from grydstedt/resolveMiddleware.jsx
Created October 14, 2015 17:46
Show Gist options
  • Select an option

  • Save mugli/e2861c7b5dc66dcf25d9 to your computer and use it in GitHub Desktop.

Select an option

Save mugli/e2861c7b5dc66dcf25d9 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