nuwave / lighthouse

A framework for serving GraphQL from Laravel
https://lighthouse-php.com
MIT License
3.37k stars 437 forks source link

why use can't directive @convertEmptyStringsToNullvalue with enums (empty string) ? #2625

Closed baonguyen212002 closed 3 weeks ago

baonguyen212002 commented 3 weeks ago

Describe the bug

Expected behavior/Solution

Steps to reproduce

1.

 type Query {
    userAttendanceStatistics(
        dateRange: DateRange @whereBetween
        userId: ID @where
        blockType: AttendanceBlockType @convertEmptyStringsToNull
    ): [UserAttendanceStatistics]
}

2.

query userAttendanceStatistics($dateRange: DateRange, $userId: ID, $blockType: AttendanceBlockType) {
  userAttendanceStatistics(
    dateRange: $dateRange
    userId: $userId
    blockType: $blockType
  ) {
    user {
      id
      name
      avatar
      position
      name
      nickname
      email
      phone
      __typename
    }
    checkin
    checkout
    totalWorkingHours
    status
    __typename
  }
}

3.

{
  "dateRange": {
    "from": "2024-10-25",
    "to": "2024-10-25"
  },
  "userId": "9",
  "blockType": ""
}

Output/Logs

{
  "errors": [
    {
      "message": "Variable \"$blockType\" got invalid value (empty string); Expected type AttendanceBlockType.",
      "extensions": {
        "category": "graphql"
      },
      "locations": [
        {
          "line": 1,
          "column": 68
        }
      ]
    }
  ]
}
Click to expand ``` # Add in log output/error messages here ```

**Lighthouse Version**
spawnia commented 3 weeks ago

Clients are still expected to call GraphQL operations with valid values. Whereas empty strings are valid values for scalars of type String and only handled differently internally, they are not valid enum values.

baonguyen212002 commented 3 weeks ago

Clients are still expected to call GraphQL operations with valid values. Whereas empty strings are valid values for scalars of type String and only handled differently internally, they are not valid enum values.

how to fix

spawnia commented 3 weeks ago

Do not pass empty strings where only enum values or null are allowed. This is a client issue.