Skip to content

Commit

Permalink
Add test case for extension response
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathew Byrne committed Sep 17, 2018
1 parent 60196b8 commit 8fdf4fb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
44 changes: 28 additions & 16 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,49 +73,61 @@ func (p *Client) mkRequest(query string, options ...Option) Request {
return r
}

type ResponseData struct {
Data interface{}
Errors json.RawMessage
Extensions map[string]interface{}
}

func (p *Client) Post(query string, response interface{}, options ...Option) (resperr error) {
respDataRaw, resperr := p.RawPost(query, options...)
if resperr != nil {
return resperr
}

// we want to unpack even if there is an error, so we can see partial responses
unpackErr := unpack(respDataRaw.Data, response)

if respDataRaw.Errors != nil {
return RawJsonError{respDataRaw.Errors}
}
return unpackErr
}

func (p *Client) RawPost(query string, options ...Option) (*ResponseData, error) {
r := p.mkRequest(query, options...)
requestBody, err := json.Marshal(r)
if err != nil {
return fmt.Errorf("encode: %s", err.Error())
return nil, fmt.Errorf("encode: %s", err.Error())
}

rawResponse, err := p.client.Post(p.url, "application/json", bytes.NewBuffer(requestBody))
if err != nil {
return fmt.Errorf("post: %s", err.Error())
return nil, fmt.Errorf("post: %s", err.Error())
}
defer func() {
_ = rawResponse.Body.Close()
}()

if rawResponse.StatusCode >= http.StatusBadRequest {
responseBody, _ := ioutil.ReadAll(rawResponse.Body)
return fmt.Errorf("http %d: %s", rawResponse.StatusCode, responseBody)
return nil, fmt.Errorf("http %d: %s", rawResponse.StatusCode, responseBody)
}

responseBody, err := ioutil.ReadAll(rawResponse.Body)
if err != nil {
return fmt.Errorf("read: %s", err.Error())
return nil, fmt.Errorf("read: %s", err.Error())
}

// decode it into map string first, let mapstructure do the final decode
// because it can be much stricter about unknown fields.
respDataRaw := struct {
Data interface{}
Errors json.RawMessage
}{}
respDataRaw := &ResponseData{}
err = json.Unmarshal(responseBody, &respDataRaw)
if err != nil {
return fmt.Errorf("decode: %s", err.Error())
return nil, fmt.Errorf("decode: %s", err.Error())
}

// we want to unpack even if there is an error, so we can see partial responses
unpackErr := unpack(respDataRaw.Data, response)

if respDataRaw.Errors != nil {
return RawJsonError{respDataRaw.Errors}
}
return unpackErr
return respDataRaw, nil
}

type RawJsonError struct {
Expand Down
21 changes: 21 additions & 0 deletions codegen/testserver/generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"testing"
"time"

"github.com/99designs/gqlgen/graphql"

"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/handler"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -110,6 +112,25 @@ func TestGeneratedServer(t *testing.T) {
})
}

func TestResponseExtension(t *testing.T) {
srv := httptest.NewServer(handler.GraphQL(
NewExecutableSchema(Config{
Resolvers: &testResolver{},
}),
handler.RequestMiddleware(func(ctx context.Context, next func(ctx context.Context) []byte) []byte {
rctx := graphql.GetRequestContext(ctx)
if err := rctx.RegisterExtension("example", "value"); err != nil {
panic(err)
}
return next(ctx)
}),
))
c := client.New(srv.URL)

raw, _ := c.RawPost(`query { valid }`)
require.Equal(t, raw.Extensions["example"], "value")
}

type testResolver struct {
tick chan string
}
Expand Down

0 comments on commit 8fdf4fb

Please sign in to comment.