Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add why field to GraphQL Errors #893

Open
IvanGoncharov opened this issue Oct 7, 2021 · 3 comments
Open

Add why field to GraphQL Errors #893

IvanGoncharov opened this issue Oct 7, 2021 · 3 comments
Labels
💡 Proposal (RFC 1) RFC Stage 1 (See CONTRIBUTING.md)

Comments

@IvanGoncharov
Copy link
Member

IvanGoncharov commented Oct 7, 2021

TL;DR:

This RFC proposes adding the why field to GraphQL errors that contain a list of reasons why this error happened.

Problem Statement

Sometimes, during execution multiple errors happening at the same time (e.g. parallel execution) or to provide full context on what happens you need to chain errors.

Recent changes in JS, added two features AggregatedError and Error.cause to solve both problems but we don't have any mechanism to represent this additional error context in graphql.

But I don't think this proposal is JS specific and other languages have also mechanisms for aggregated errors and chains of errors.

** TBD: Examples in other languages **

🧑‍💻 Proposed Solution

Add ability to attach an array of sub-errors to graphql error under a property named why (the name is up to discussion).
With following limitations:

  1. why is an optional non-empty array of objects
  2. Each individual object should contain only message property (required) and extensions(optional)

🎬 Behavior

Error chaining:

{
  "errors": [
    {
      "message": "Expected value of type "DateTime", found "07/11/2021"",
      "locations": [ { "line": 6, "column": 7 } ],
      "why": [{
        message: "Invalid date-time please specify in 'yyyy-MM-dd'T'HH:mm:ss.SSSZ' format"
      }],
    }
  ]
}

Aggregated errors:

{
  "errors": [
    {
      "message": "Failed to fetch all necessary data",
      "why": [
        {
          message: "Failed to fetch from https://example.com/api/resource/1"
        },
        {
          message: "Failed to fetch from https://example.com/api/resource/2"
        },
      ],
    }
  ]
}
@leebyron
Copy link
Collaborator

leebyron commented Oct 7, 2021

Note, there is a very similar ECMAScript proposal here https://github.com/tc39/proposal-error-cause

@leebyron
Copy link
Collaborator

leebyron commented Oct 7, 2021

Bikeshed, could merge the two related proposals with a key name like "causes"

@IvanGoncharov IvanGoncharov added the 💡 Proposal (RFC 1) RFC Stage 1 (See CONTRIBUTING.md) label Oct 23, 2021
@IvanGoncharov
Copy link
Member Author

Moving to Stage1 per Oct2021 WG discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💡 Proposal (RFC 1) RFC Stage 1 (See CONTRIBUTING.md)
Projects
None yet
Development

No branches or pull requests

2 participants