Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

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

Closed
dmitrydrynov opened this issue Jul 19, 2021 · 1 comment
Closed

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

dmitrydrynov opened this issue Jul 19, 2021 · 1 comment
Labels

Comments

@dmitrydrynov
Copy link

dmitrydrynov commented Jul 19, 2021

Hi guys,
How I can set custom directives to the current field through Javascript code?

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!

@IvanGoncharov
Copy link
Member

@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.

@graphql graphql locked and limited conversation to collaborators Aug 13, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

2 participants