graphql icon indicating copy to clipboard operation
graphql copied to clipboard

Add support for error extensions

Open blaedj opened this issue 6 years ago • 0 comments

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"
      //}
   }
}

blaedj avatar Dec 10 '18 22:12 blaedj