Skip to content

How I can set custom directives to the current field? #3234

Answered by IvanGoncharov
dmitrydrynov asked this question in Q&A
Discussion options

You must be logged in to vote

@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 extensions in addition to directives.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by saihaj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants
Converted from issue

This discussion was converted from issue #3213 on August 13, 2021 03:48.