graph-gophers / graphql-go

GraphQL server with a focus on ease of use
BSD 2-Clause "Simplified" License
4.65k stars 492 forks source link

refactored execFieldSelection to return multiple errors on the same path if err instance of `interface { Unwrap() []error }` #625

Closed savaki closed 1 year ago

savaki commented 1 year ago

I opened this discussion, but thou, but thought it might be useful to provide the code change being proposed. This allows joined errors to be returned as:

{
  "errors": [
    {
      "message": "You cannot do this because of X.",
      "path": [
        "field"
      ],
      "extensions": {
        "code": "failed_x"
      }
    },
    {
      "message": "You cannot do this because of Y.",
      "path": [
        "field"
      ],
      "extensions": {
        "code": "failed_y"
      }
    },
    {
      "message": "You cannot do this because of Z.",
      "path": [
        "field"
      ],
      "extensions": {
        "code": "failed_z"
      }
    }
  ],
  "data": {
    "field": null
  }
}

rather than being using extensions and a more nested:

{
  "errors": [
    {
      "message": "failed_x: You cannot do this because of X., failed_y: You cannot do this because of Y., failed_z: You cannot do this because of Z.",
      "path": [
        "field"
      ],
      "extensions": {
        "details": [
          {
            "code": "failed_x",
            "message": "You cannot do this because of X."
          },
          {
            "code": "failed_y",
            "message": "You cannot do this because of Y."
          },
          {
            "code": "failed_z",
            "message": "You cannot do this because of Z."
          }
        ]
      }
    }
  ],
  "data": {
    "field": null
  }
}
pavelnikolov commented 1 year ago

This is against the GraphQL spec, which clearly states:

If the field returns null because of a field error which has already been added to the "errors" list in the response, the "errors" list must not be further affected. That is, only one error should be added to the errors list per field.

https://spec.graphql.org/October2021/#sel-EANRNDLAACNAn7V