Add integration tests against a gqlgen server (#50)

## Summary:
We have lots of tests covering codegen, but not a lot that actually run
the code.  For things where all we do is generate types, that's (mostly)
fine (especially now that we actually build the code), but as we
generate more nontrivial non-type code we need to actually run it.

So I wrote some integration tests that spin up a little gqlgen
server, and make calls to it; we can add more over time especially as
the JSON marshalling logic gets complex (to support fragments).
They're more work to write than the snapshot tests, but of course they
can test a lot more.

In addition to gqlgen, I pulled in testify assert/require, because I
really wanted to be able to use assert.Equal and such for these.  I
didn't bother converting existing tests, although I assume they will
become useful elsewhere in time.  Both gqlgen and testify are of course
only used in tests.

Fixes #21 and #24.
Issue: https://github.com/Khan/genqlient/issues/21
Issue: https://github.com/Khan/genqlient/issues/24

## Test plan:
make check

Author: benjaminjkraft

Reviewers: aberkan, dnerdy, benjaminjkraft, csilvers, MiguelCastillo

Required Reviewers: 

Approved by: aberkan, dnerdy

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

Pull request URL: https://github.com/Khan/genqlient/pull/50
This commit is contained in:
Ben Kraft
2021-08-20 10:54:41 -07:00
committed by GitHub
parent 700392315a
commit 3746ecd063
15 changed files with 2773 additions and 52 deletions
+10
View File
@@ -0,0 +1,10 @@
schema:
- ../schema.graphql
exec:
filename: gqlgen_exec.go
package: server
model:
filename: gqlgen_models.go
package: server
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,9 @@
// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.
package server
type User struct {
ID string `json:"id"`
Name string `json:"name"`
LuckyNumber *int `json:"luckyNumber"`
}
+48
View File
@@ -0,0 +1,48 @@
package server
import (
"context"
"net/http/httptest"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/transport"
)
func intptr(v int) *int { return &v }
var users = []*User{
{ID: "1", Name: "Yours Truly", LuckyNumber: intptr(17)},
{ID: "2", Name: "Raven", LuckyNumber: intptr(-1)},
}
func userByID(id string) *User {
for _, user := range users {
if id == user.ID {
return user
}
}
return nil
}
func (r *queryResolver) Me(ctx context.Context) (*User, error) {
return userByID("1"), nil
}
func (r *queryResolver) User(ctx context.Context, id string) (*User, error) {
return userByID(id), nil
}
func RunServer() *httptest.Server {
gqlgenServer := handler.New(NewExecutableSchema(Config{Resolvers: &resolver{}}))
gqlgenServer.AddTransport(transport.POST{})
return httptest.NewServer(gqlgenServer)
}
type (
resolver struct{}
queryResolver struct{}
)
func (r *resolver) Query() QueryResolver { return &queryResolver{} }
//go:generate go run github.com/99designs/gqlgen