ardatan / ardatan/graphql-tools
Directive default enum value is not mapped to internal representation
- Dominant language
- TypeScript
- Stars
- 5.4k
- Forks
- 830
- Avg merge
- 10h 59m
- Merged PRs (30d)
- 45
Description
### Issue workflow progress
_Progress of the issue based on the
[Contributor Workflow](https://github.com/the-guild-org/Stack/blob/master/CONTRIBUTING.md#a-typical-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:
```gql
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:
```javascript
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.
```javascript
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"
]
}
]
}
```
Contributor guide
Research direction
Start with the linked CodeSandbox reproduction and inspect the mapSchema/getDirective path for directive default arguments, along with the IntrospectionQuery failure. Done means the omitted USER role is mapped to 3user like explicit enum values, and introspection no longer reports that UserRole cannot represent USER.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql, typescript
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100