Add tests for behavior on error (#83)

## Summary:
We guarantee that we never return a nil response, so you can safely do
```
resp, err := myQuery(...)
return resp.Field.SubField, err
```
And furthermore, if the error was a GraphQL error, `resp` may even be
nonzero; other, non-failing fields may be set.  (This depends on the
server, of course.) But we weren't testing either of those.  Now we do.

## Test plan:
make check


Author: benjaminjkraft

Reviewers: jvoll, aberkan, dnerdy, MiguelCastillo, mahtabsabet

Required Reviewers: 

Approved By: jvoll

Checks:  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Lint,  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Lint

Pull Request URL: https://github.com/Khan/genqlient/pull/83
This commit is contained in:
Ben Kraft
2021-09-10 15:45:34 -07:00
committed by GitHub
parent e88305ecbd
commit 9e1c98488e
5 changed files with 128 additions and 0 deletions
+33
View File
@@ -34,6 +34,39 @@ func TestSimpleQuery(t *testing.T) {
assert.Equal(t, 17, resp.Me.LuckyNumber)
}
func TestServerError(t *testing.T) {
_ = `# @genqlient
query failingQuery { fail me { id } }`
ctx := context.Background()
server := server.RunServer()
defer server.Close()
client := graphql.NewClient(server.URL, http.DefaultClient)
resp, err := failingQuery(ctx, client)
// As long as we get some response back, we should still return a full
// response -- and indeed in this case it should even have another field
// (which didn't err) set.
assert.Error(t, err)
assert.NotNil(t, resp)
assert.Equal(t, "1", resp.Me.Id)
}
func TestNetworkError(t *testing.T) {
ctx := context.Background()
client := graphql.NewClient("https://nothing.invalid/graphql", http.DefaultClient)
resp, err := failingQuery(ctx, client)
// As we guarantee in the README, even on network error you always get a
// non-nil response; this is so you can write e.g.
// resp, err := failingQuery(ctx)
// return resp.Me.Id, err
// without a bunch of extra ceremony.
assert.Error(t, err)
assert.NotNil(t, resp)
assert.Equal(t, new(failingQueryResponse), resp)
}
func TestVariables(t *testing.T) {
_ = `# @genqlient
query queryWithVariables($id: ID!) { user(id: $id) { id name luckyNumber } }`