Created
September 16, 2015 02:59
-
-
Save grydstedt/03ae4806e0af41677f5d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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