Skip to content

Instantly share code, notes, and snippets.

@kirkness
Last active August 2, 2017 21:46
Show Gist options
  • Save kirkness/4ac64659a62925c0693d99caf8fbe187 to your computer and use it in GitHub Desktop.
Save kirkness/4ac64659a62925c0693d99caf8fbe187 to your computer and use it in GitHub Desktop.
Authenticate a GraphQL Schema
// @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