Skip to content

Commit

Permalink
Support path indices in GraphQLError.Path
Browse files Browse the repository at this point in the history
Fixes the following error in flyctl:

Error: decoding response: json: cannot unmarshal number into Go struct field GraphQLError.Errors.Path of type string
  • Loading branch information
saleemrashid committed Jan 4, 2025
1 parent 6a434ad commit 8938f9b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ type File struct {
type GraphQLError struct {
err error
Message string
Path []string
Path []interface{}
Extensions GraphQLErrorExtensions
}

Expand Down
34 changes: 34 additions & 0 deletions graphql_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package graphql

import (
"context"
"errors"
"io"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -95,6 +96,39 @@ func TestDoJSONBadRequestErr(t *testing.T) {
is.Equal(err.Error(), "miscellaneous message as to why the the request was bad")
}

func TestDoJSONErrorWithPath(t *testing.T) {
is := is.New(t)
var calls int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
is.Equal(r.Method, http.MethodPost)
b, err := ioutil.ReadAll(r.Body)
is.NoErr(err)
is.Equal(string(b), `{"query":"query {}","variables":null}`+"\n")
w.WriteHeader(http.StatusOK)
io.WriteString(w, `{
"errors": [{
"message": "miscellaneous message as to why the the request was bad",
"path": ["nodes", 1, "name"]
}]
}`)
}))
defer srv.Close()

ctx := context.Background()
client := NewClient(srv.URL)

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
var responseData map[string]interface{}
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
is.Equal(calls, 1) // calls

gqlErr := &GraphQLError{}
is.True(errors.As(err, &gqlErr))
is.Equal(gqlErr.Message, "miscellaneous message as to why the the request was bad")
}

func TestQueryJSON(t *testing.T) {
is := is.New(t)

Expand Down

0 comments on commit 8938f9b

Please sign in to comment.