Skip to content

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

Closed
@nodkz

Description

@nodkz

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions