Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for behavior on error #83

Merged
merged 1 commit into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions internal/integration/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions internal/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }`
Expand Down
1 change: 1 addition & 0 deletions internal/integration/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type Query {
being(id: ID!): Being
beings(ids: [ID!]!): [Being]!
lotteryWinner(number: Int!): Lucky
fail: Boolean
}

type User implements Being & Lucky {
Expand Down
53 changes: 53 additions & 0 deletions internal/integration/server/gqlgen_exec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/integration/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"context"
"fmt"
"net/http/httptest"

"github.com/99designs/gqlgen/graphql/handler"
Expand Down Expand Up @@ -82,6 +83,11 @@ func (r *queryResolver) LotteryWinner(ctx context.Context, number int) (Lucky, e
return nil, nil
}

func (r *queryResolver) Fail(ctx context.Context) (*bool, error) {
f := true
return &f, fmt.Errorf("oh no")
}

func RunServer() *httptest.Server {
gqlgenServer := handler.New(NewExecutableSchema(Config{Resolvers: &resolver{}}))
gqlgenServer.AddTransport(transport.POST{})
Expand Down