我可以在 laravel-lighthouse 中抛出自定义错误吗

can I throw custom error in laravel-lighthouse

GraphQL\Error\Error 抛出错误的任何方式,除消息外没有其他数据。 当前return个数据是

{
  "errors": [
    {
      "message": "Some Errors",
      "extensions": {
        "reason": "",
        "category": "custom"
      },
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "to the path"
      ],
      "trace": [{},{},{},{}]
    }
  ],
  "data": {
    "testQuery": null
  }
}

它包含不必要的数据

但我想要这样的东西:

{
  "errors": [
    {
      "message": "Some Errors",
      "extensions": {
        "reason": "",
        "category": "custom"
      },
      
  ],
  "data": {
    "testQuery": null
  }
}

您可以抛出自己的异常,这将允许您这样做。检查 https://lighthouse-php.com/5/digging-deeper/error-handling.html#registering-error-handlers.

对于简单的错误,您可以使用此代码,以防您不想创建错误处理程序 class

use GraphQL\Error\Error;

...

return Error::createLocatedError('Some Errors');