Skip to content

Commit

Permalink
Merge pull request #1 from peterdeme/master
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinwyszynski authored Feb 2, 2023
2 parents 18c5c31 + 8b46104 commit a77b50e
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 3 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: CI

on:
pull_request:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- uses: actions/setup-go@main
- run: go test -v ./...
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/shurcooL/graphql

go 1.19

require (
github.com/graph-gophers/graphql-go v1.5.0
golang.org/x/net v0.5.0
)
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/graph-gophers/graphql-go v1.5.0 h1:fDqblo50TEpD0LY7RXk/LFVYEVqo3+tXMNMPSVXA1yc=
github.com/graph-gophers/graphql-go v1.5.0/go.mod h1:YtmJZDLbF1YYNrlNAuiO5zAStUWc3XZT07iGsVqe1Os=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.opentelemetry.io/otel v1.6.3/go.mod h1:7BgNga5fNlF/iZjG06hM3yofffp0ofKCDwSXx1GC4dI=
go.opentelemetry.io/otel/trace v1.6.3/go.mod h1:GNJQusJlUgZl9/TQBPKU/Y/ty+0iVB5fjhKeJGZPGFs=
golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw=
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
31 changes: 28 additions & 3 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab
body, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("non-200 OK status code: %v body: %q", resp.Status, body)
}

var out struct {
Data *json.RawMessage
Errors errors
//Extensions interface{} // Unused.
Data *json.RawMessage
Errors errors
Extensions interface{}
}

err = json.NewDecoder(resp.Body).Decode(&out)
if err != nil {
// TODO: Consider including response body in returned error, if deemed helpful.
Expand All @@ -91,9 +93,15 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab
return err
}
}

if len(out.Errors) > 0 {
if out.Extensions != nil {
return newErrorsWithExtensions(out.Errors, out.Extensions)
}

return out.Errors
}

return nil
}

Expand Down Expand Up @@ -121,3 +129,20 @@ const (
mutationOperation
//subscriptionOperation // Unused.
)

type ErrorsWithExtensions struct {
errors errors
extensions interface{}
}

func newErrorsWithExtensions(err errors, extensions interface{}) ErrorsWithExtensions {
return ErrorsWithExtensions{errors: err, extensions: extensions}
}

func (e ErrorsWithExtensions) Error() string {
return e.errors[0].Message
}

func (e ErrorsWithExtensions) Extensions() interface{} {
return e.extensions
}
60 changes: 60 additions & 0 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package graphql_test

import (
"context"
"errors"
"io"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -101,6 +102,65 @@ func TestClient_Query_noDataWithErrorResponse(t *testing.T) {
}
}

func TestClient_Query_noDataWithErrorAndExtensionsResponse(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/graphql", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
mustWrite(w, `{
"errors": [
{
"message": "Field 'user' is missing required arguments: login",
"locations": [
{
"line": 7,
"column": 3
}
]
}
],
"extensions": {
"code": "MISSING_ARGUMENTS"
}
}`)
})
client := graphql.NewClient("/graphql", &http.Client{Transport: localRoundTripper{handler: mux}})

var q struct {
User struct {
Name graphql.String
}
}
err := client.Query(context.Background(), &q, nil)

if err == nil {
t.Fatal("got error: nil, want: non-nil")
}

if got, want := err.Error(), "Field 'user' is missing required arguments: login"; got != want {
t.Errorf("got error: %v, want: %v", got, want)
}

var graphErr graphql.ErrorsWithExtensions
if !errors.As(err, &graphErr) {
t.Fatalf("got error: %T, want: %T", err, graphErr)
}

extensions := graphErr.Extensions()
asMap, ok := extensions.(map[string]interface{})

if !ok {
t.Fatalf("got error: %T, want: %T", graphErr.Extensions(), asMap)
}

if got, want := asMap["code"], "MISSING_ARGUMENTS"; got != want {
t.Errorf("got error: %v, want: %v", got, want)
}

if q.User.Name != "" {
t.Errorf("got non-empty q.User.Name: %v", q.User.Name)
}
}

func TestClient_Query_errorStatusCode(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/graphql", func(w http.ResponseWriter, req *http.Request) {
Expand Down

0 comments on commit a77b50e

Please sign in to comment.