-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auth check before call original resolver ? #2
Comments
@giautm I am thinking about this actually, having a way to specify easily the resolvers chain order. |
@alexisgahon can you elaborate a bit more regarding the chain order? |
@chenkie The way What it does: What I try to achieve Maybe use a prefix on directive name to know how to chain resolvers. |
@alexisgahon I think you have it reversed, it should say: What it does: What I try to achieve |
Hi, i edited // Credit: agonbina https://github.com/apollographql/graphql-tools/issues/212
export const attachDirectives = (resolvers, schema: GraphQLSchema) => {
forEachField(schema, (field: GraphQLField<any, any>) => {
const directives = field.astNode.directives;
directives.forEach((directive: DirectiveNode) => {
const directiveName = directive.name.value;
const resolver = resolvers[directiveName];
if (resolver) {
const originalResolver = field.resolve || defaultFieldResolver;
const Directive = schema.getDirective(directiveName);
const directiveArgs = getArgumentValues(Directive, directive);
field.resolve = (...args) => {
const [source, _, context, info] = args;
return resolver(() => {
const promise = originalResolver.call(field, ...args);
if (promise instanceof Promise) {
return promise;
}
return Promise.resolve(promise);
}, source, directiveArgs, context, info);
};
}
});
});
};
const directiveResolvers = {
// UPPER CASE a string result
upperCase(next: () => Promise<any>) {
return next().then((r) => {
if (typeof(r) === 'string') {
return r.toUpperCase();
}
return r;
});
},
isAuthenticated(nextResolver, source, args, context) {
const token = context.headers.authorization;
if (!token) {
throw new AuthorizationError({
message: 'You must supply a JWT for authorization!'
});
}
try {
const decoded = jwt.verify(
token.replace('Bearer ', ''),
process.env.JWT_SECRET
);
return nextResolver(); // <---- dont forget it
} catch (err) {
throw new AuthorizationError({
message: 'You are not authorized.'
});
}
},
}; |
Thank you, great! now this is typescript and not compatible with the current project. But I too use TS, thank you |
What would the graphql declaration for upperCase look like? directive @isAuthenticated on QUERY | FIELD |
directive @uppercase on FIELD Current solution is work for directive on field only. Other locations like QUERY | FRAGMENT | INLINE_FRAGMENT,... is not supported. |
I have a PR right here: ardatan/graphql-tools#518 to bring it to graphql-tools, so typescript is not a problem for any project. |
yep, thank you |
Any more thoughts on the schema directives pattern over the last year @chenkie ? I just had a play developing your approach using the updated code: https://github.com/dan-kwiat/graphql-auth Let me know if you'd like to merge. |
I think we should check Auth before call original resolver? in some case like mutation. or resolver access user info in context object.
The text was updated successfully, but these errors were encountered: