Graphql errors: Difference between revisions

From wikinotes
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Documentation =
<blockquote>
{| class="wikitable"
|-
| spec(2018): error format || https://spec.graphql.org/June2018/#sec-Errors
|-
|}
</blockquote><!-- Documentation -->
= Basics =
<blockquote>
There are two possible replies for a graphql request.
There are two possible replies for a graphql request.


Line 6: Line 18:


// failure (a json list with the key 'errors')
// failure (a json list with the key 'errors')
{"errors": []}
{
  "errors": [
    {
      "message": "Name for character with ID 1002 could not be fetched.",  // <--- graphql spec keys
      "locations": [ { "line": 6, "column": 7 } ],
      "path": [ "hero", "heroFriends", 1, "name" ],
      "extensions": {                                                      // <-- library/application specific keys
        "code": "CAN_NOT_FETCH_BY_ID",
        "timestamp": "Fri Feb 9 14:33:09 UTC 2018"
      }
    }
  ]
}
</syntaxhighlight>
</syntaxhighlight>


<code>data</code> should never be present if there was an error, and vice-versa.<br>
Note that the <code>extensions</code> key was introduced to the spec in 2018.
'''Beyond this top-level requirement, the actual content of errors appears to depend on the library used to create your API.'''
</blockquote><!-- Basics -->

Latest revision as of 14:51, 4 September 2021

Documentation

spec(2018): error format https://spec.graphql.org/June2018/#sec-Errors

Basics

There are two possible replies for a graphql request.

// success  (a json object with the key 'data')
{"data": {"name": "foo", "id": 1}

// failure (a json list with the key 'errors')
{
  "errors": [
    {
      "message": "Name for character with ID 1002 could not be fetched.",  // <--- graphql spec keys
      "locations": [ { "line": 6, "column": 7 } ],
      "path": [ "hero", "heroFriends", 1, "name" ],
      "extensions": {                                                      // <-- library/application specific keys
        "code": "CAN_NOT_FETCH_BY_ID",
        "timestamp": "Fri Feb 9 14:33:09 UTC 2018"
      }
    }
  ]
}

Note that the extensions key was introduced to the spec in 2018.