Skip to content

Directive default enum value is not mapped to internal representation #6922

@DanTsk

Description

@DanTsk

Issue workflow progress

Progress of the issue based on the
Contributor Workflow

  • 1. The issue provides a reproduction available on Github, Stackblitz or CodeSandbox

    Make sure to fork this template and run yarn generate in the terminal.

    Please make sure the GraphQL Tools package versions under package.json matches yours.

  • 2. A failing test has been provided
  • 3. A local solution has been provided
  • 4. A pull request is pending review

Describe the bug
Let's assume we have such a schema:

  enum UserRole {
    ADMIN
    MODERATOR
    USER
  }

  directive @auth(role: UserRole = USER) on FIELD_DEFINITION 

  type Query {
    usersList: [ID!]!       @auth 
    moderatorsList: [ID!]!  @auth(role: MODERATOR)
    adminsList: [ID!]!      @auth(role: ADMIN)
  }

And the following resolvers:

const resolvers = {
  Query: {
    usersList: () => ["u1", "u2", "u3"],
    moderatorsList: () => ["m1", "m2"],
    adminsList: () => ["a1"],
  },
  UserRole: {
    ADMIN: "0admin",
    MODERATOR: "1moderator",
    USER: "3user",
  },
};

The default role specified in the @auth directive is not being correctly mapped to the internal value of the UserRole enum in the schema. Specifically, when the @auth directive is used without specifying a role, the default value (USER) is expected to be mapped to its corresponding internal value (3user). However, this mapping does not occur, leading to an incorrect role in the directive transformer.

const schemaWithDirective = mapSchema(schema, {
  [MapperKind.OBJECT_FIELD]: (fieldConfig) => {
    const authDirective = getDirective(schema, fieldConfig, "auth")?.at(0);

    const fieldName = fieldConfig.astNode.name.value;
    const role = authDirective.role;

    console.log({ fieldName, role });

    return fieldConfig;
  },
});

Current Output:

{ fieldName: 'usersList', role: 'USER' }
{ fieldName: 'moderatorsList', role: '2moderator' }
{ fieldName: 'adminsList', role: '1admin' }

To Reproduce
Please, visit the codesandbox below

https://codesandbox.io/p/devbox/fjt76v

Expected behavior

The default role (USER) should be correctly mapped to the internal value (3user) defined in the UserRole enum.
Expected Output:

{ fieldName: 'usersList', role: '3user' }
{ fieldName: 'moderatorsList', role: '2moderator' }
{ fieldName: 'adminsList', role: '1admin' }

Environment:

  • OS: All
  • @graphql-tools/schema: 10.0.16
  • @graphql-tools/utils: 10.7.2
  • NodeJS: v20+

Additional context

Also, the following error is raised while executing IntrospectionQuery.

"errors": [
        {
            "message": "Enum \"UserRole\" cannot represent value: \"USER\"",
            "locations": [
                {
                    "line": 62,
                    "column": 7
                }
            ],
            "path": [
                "__schema",
                "directives",
                "0",
                "args",
                "0",
                "defaultValue"
            ]
        }
    ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions