Skip to content
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 to set directives for fields when creating GraphQLObjectType? #1262

Closed
nodkz opened this issue Feb 23, 2018 · 2 comments
Closed

How to set directives for fields when creating GraphQLObjectType? #1262

nodkz opened this issue Feb 23, 2018 · 2 comments

Comments

@nodkz
Copy link
Contributor

nodkz commented Feb 23, 2018

Can somebody share proper way how to pass directives for fields when creating type via GraphQLObjectType? I want to integrate graphql-cost-analysis to our GraphQL Schema.

A astNode: FieldDefinitionNode in GraphQLFieldConfig is ReadOnly and there is now way to write directives via object config. Or i something missing?

Desired way to declare directive will be such:

const UserType = new GraphQLObjectType({
  name: 'User',
  fields: {
    name: {
      type: GraphQLString,
      description: '...',
      resolve: () => ...,
      directives: [
        { name: 'cost',  args: { useMultipliers: false, complexity: 2 } },
        { name: 'unique' },
      ],
      // or maybe simpler if graphql spec does not allow duplicated names
      directives: {
        cost: { useMultipliers: false, complexity: 2 },
        unique: true,
      },
    },
  },
});

cc @IvanGoncharov @leebyron

@IvanGoncharov
Copy link
Member

@nodkz It was already discussed in my PR that added astNode feature and @leebyron had the following position: #746 (comment)

Right now directives are purely a feature of the GraphQL language and IDL, so a schema not created using IDL definitionally won't contain directives. When building a schema in code, you have the full power of the host programming environment, so directives shouldn't be necessary. Directives are a tool for supplying additional intent to be later interpreted by the host programming environment.

Also, I think this question is connected to the topic discussed on the last WG: Exposing schema metadata. So I think we should decide what we expose through introspection(directives or something else) and then add the same thing to GraphQL*Type classes.

@leebyron
Copy link
Contributor

Yeah - I stand by that comment.

You can include any arbitrary data on a GraphQLObjectType that you like - it's just a JS object after all. Directives are a language-level feature, not a structural property of a type.

Typically tools which use directives in the SDL are adding properties to the created underlying type objects to track within their runtimes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants