Skip to content

Conversation

blaedj
Copy link

@blaedj blaedj commented Dec 10, 2018

This commit:

  • adds an ErrorExtensions attr to the graphErr type
  • adds an Extensions method to the graphErr type

Why?

  • This adds support for error extensions as described in a
    recent(ish) update to the graphql spec.
    The interface changes are minimally invasive, and are in fact
    invisible unless the importing code defines an interface that
    includes the Extensions() method.

An example usage:

package foo

import "github.com/machinebox/graphql"

type extendedError interface {
	Extensions() map[string]interface{}
	Error() string
}

func bar() {
   client := graphql.NewClient(...)
   //...
   err := c.client.Run(ctx, req, resp)
   if ok := err.(extendedError); ok {
       extMsg, _ := json.Marshal(ee.Extensions())
       fmt.Println(extMsg)
      // prints out the extensions, e.g.:
      //{
      //  "message": "Name for character with ID 1002 could not be fetched.",
      //  "locations": [ { "line": 6, "column": 7 } ],
      //  "path": [ "hero", "heroFriends", 1, "name" ],
      //  "code": "CAN_NOT_FETCH_BY_ID",
      //  "timestamp": "Fri Feb 9 14:33:09 UTC 2018"
      //}
   }
}

This commit:
 - adds an ErrorExtensions attr to the graphErr type
 - adds an Extensions method to the graphErr type

Why?
 - This adds support for error extensions as described in a
 recent(ish)  update to the [graphql
 spec](https://facebook.github.io/graphql/June2018/#example-fce18).
 The interface changes are minimally invasive, and are in fact
 invisible unless the importing code defines an interface that
 includes the `Extensions()` method.

An example usage:

```go
package foo

import "github.com/machinebox/graphql"

...

type extendedError interface {
	Extensions() map[string]interface{}
	Error() string
}

func bar() {
...

	client := graphql.NewClient(...)
  ...
   err := c.client.Run(ctx, req, resp)
   if ok := err.(extendedError); ok {
		extMsg, _ := json.Marshal(ee.Extensions())
    fmt.Println(extMsg)
    // prints out the extensions, e.g.:
    //{
    //  "message": "Name for character with ID 1002 could not be fetched.",
    //  "locations": [ { "line": 6, "column": 7 } ],
    //  "path": [ "hero", "heroFriends", 1, "name" ],
    //  "code": "CAN_NOT_FETCH_BY_ID",
    //  "timestamp": "Fri Feb 9 14:33:09 UTC 2018"
    //}
   }
}

```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant