How I can set custom directives to the current field? #3234
-
Hi guys, I created a custom Directive: const authDirective = new GraphQLDirective({
name: 'auth',
locations: [DirectiveLocation.OBJECT, DirectiveLocation.FIELD_DEFINITION],
args: ...
}) Added to the schema: const schema = new GraphQLSchema({
directives: [authDirective],
query: new GraphQLObjectType({
fields: {
secretData: ...
}
})
}) How I can use my directive only for the secretData field? I tried to add a directives parameter for field definition but it doesn't work: {
type: validateWidgetQLType,
args: validateWidgetQLArgs,
directives: [
{ name: 'auth', arguments: { requires: 0 }}
],
} Please help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@dmitrydrynov Directives are intended to be used only in GraphQL Documents (SDL or query documents) so if you want to attach some data to a field please use extensions, like so: const schema = new GraphQLSchema({
directives: [authDirective],
query: new GraphQLObjectType({
fields: {
secretData: {
type: validateWidgetQLType,
args: validateWidgetQLArgs,
extensions: {
auth: { requires: 0 }
}
}
}
})
}); If you need to pass directives to some 3rd-party library please ask them to add support for |
Beta Was this translation helpful? Give feedback.
@dmitrydrynov Directives are intended to be used only in GraphQL Documents (SDL or query documents) so if you want to attach some data to a field please use extensions, like so:
If you need to pass directives to some 3rd-party library please ask them to add support for
extensions
in addition to directives.