Is it possible to pass custom directive args to actual field resolver method #834
Answered
by
MichalLytek
abhijeetgaonkar
asked this question in
Q&A
-
class CustomDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
const { resovle = defaultFieldResolver } = field;
field.resolve = async (...args) => {
const fieldName = field.name
args[1] = Object.assign({ field: fieldName }, args[1]); // add fieldName
return resolve.apply(this, args);
};
}
}
@ObjectType()
class Foo {
@Field()
field: string;
}
@Resolver(of => Foo)
class FooBarResolver {
@Directive("@auth(requires: ANY)")
@Query()
foobar(@Arg("baz") baz: string): string {
return getDetails(baz);
}
getDetails(baz: string){
// how to read directive field name in this method
return baz
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
MichalLytek
Mar 27, 2021
Replies: 2 comments
-
I think GraphQL extensions should fit you better in that use case: |
Beta Was this translation helpful? Give feedback.
0 replies
-
No, directives are an SDL-side feature and are intended to use decoupled from the resolvers code. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
abhijeetgaonkar
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No, directives are an SDL-side feature and are intended to use decoupled from the resolvers code.