Last active
August 2, 2017 21:46
-
-
Save kirkness/4ac64659a62925c0693d99caf8fbe187 to your computer and use it in GitHub Desktop.
Authenticate a GraphQL Schema
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
// @flow | |
import { forEachField } from 'graphql-tools'; | |
import { get } from 'lodash/fp'; | |
export default (schema: Object) => { | |
forEachField(schema, (field: Object) => { | |
if (field.authenticate) { | |
// eslint-disable-next-line no-param-reassign | |
field.resolve = (...args) => { | |
const [,, context] = args; | |
if (get('locals.user', context)) { | |
return field.resolve(...args); | |
} | |
throw new Error('Unauthorized'); | |
}; | |
} | |
}); | |
return schema; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment