Files
genqlient/internal/integration/server/server.go
T
Ben Kraft c6d087c29b Add support for "flattening" fragment-spreads (#121)
## Summary:
One common use of fragment spreads is as the entirety of a field's
selection, e.g.
```graphql
query MyQuery {
  myField {
    ...MyFragment
  }
}
```
In this case, by default, genqlient generates a wrapper type
`MyQueryMyFieldMyType`, which just embeds `MyFragment`.  This makes
sense if you later want to add more fields in addition to the fragment
spread.  But if you don't -- and you did the fragment because you want
to share types, it's an extra layer of indirection.  (Which becomes
especially onerous if `myField` has list type (`[MyType!]`), such that
it's not just an extra attribute-access to get to `MyFragment`.)

The new option `# @genqlient(flatten: true)` simplifies this situation:
if applied to `myField` is skips the wrapper type;
`MyQueryResponse.MyField` will simply have type `MyFragment` (or
`[]MyFragment`, or whatever).  This should hopefully make the `typename`
option, which has more limitations, less necessary.

Note that in #30 the initial idea was to support this for fields as
well.  This would require significant additional complexity in the
JSON-(un)marshaling code, and has proven less necessary, so I
implemented this option only for fragment-spreads for now.  With that
restriction, it was shockingly simple; we have to hook into a bunch of
different places, but they're all quite simple, since the structure of
the Go types still matches the structure in GraphQL.

Issue: https://github.com/Khan/genqlient/issues/30

## Test plan:
make check


Author: benjaminjkraft

Reviewers: csilvers, dnerdy, aberkan, jvoll, mahtabsabet, MiguelCastillo, StevenACoffman

Required Reviewers: 

Approved By: csilvers, dnerdy

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/121
2021-09-29 17:52:06 -07:00

145 lines
3.3 KiB
Go

package server
import (
"context"
"fmt"
"net/http/httptest"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/transport"
)
func strptr(v string) *string { return &v }
func intptr(v int) *int { return &v }
var users = []*User{
{
ID: "1", Name: "Yours Truly", LuckyNumber: intptr(17),
Birthdate: strptr("2025-01-01"),
Hair: &Hair{Color: strptr("Black")},
},
{ID: "2", Name: "Raven", LuckyNumber: intptr(-1), Hair: nil},
}
func init() {
users[0].Friends = []*User{users[1]} // (obviously a lie, but)
users[1].Friends = users // try to crash the system
}
var animals = []*Animal{
{
ID: "3", Name: "Fido", Species: SpeciesDog, Owner: userByID("1"),
Hair: &BeingsHair{HasHair: true},
},
{
ID: "4", Name: "Old One", Species: SpeciesCoelacanth, Owner: nil,
Hair: &BeingsHair{HasHair: false},
},
}
func userByID(id string) *User {
for _, user := range users {
if id == user.ID {
return user
}
}
return nil
}
func usersByBirthdates(dates []string) []*User {
var retval []*User
for _, date := range dates {
for _, user := range users {
if user.Birthdate != nil && *user.Birthdate == date {
retval = append(retval, user)
}
}
}
return retval
}
func beingByID(id string) Being {
for _, user := range users {
if id == user.ID {
return user
}
}
for _, animal := range animals {
if id == animal.ID {
return animal
}
}
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) {
if id == nil {
return userByID("1"), nil
}
return userByID(*id), nil
}
func (r *queryResolver) Being(ctx context.Context, id string) (Being, error) {
return beingByID(id), nil
}
func (r *queryResolver) Beings(ctx context.Context, ids []string) ([]Being, error) {
ret := make([]Being, len(ids))
for i, id := range ids {
ret[i] = beingByID(id)
}
return ret, nil
}
func (r *queryResolver) LotteryWinner(ctx context.Context, number int) (Lucky, error) {
for _, user := range users {
if user.LuckyNumber != nil && *user.LuckyNumber == number {
return user, nil
}
}
return nil, nil
}
func (r *queryResolver) UsersBornOn(ctx context.Context, date string) ([]*User, error) {
return usersByBirthdates([]string{date}), nil
}
func (r *queryResolver) UsersBornOnDates(ctx context.Context, dates []string) ([]*User, error) {
return usersByBirthdates(dates), nil
}
func (r *queryResolver) UserSearch(ctx context.Context, birthdate *string, id *string) ([]*User, error) {
switch {
case birthdate == nil && id != nil:
return []*User{userByID(*id)}, nil
case birthdate != nil && id == nil:
return usersByBirthdates([]string{*birthdate}), nil
default:
return nil, fmt.Errorf("need exactly one of birthdate or id")
}
}
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{})
return httptest.NewServer(gqlgenServer)
}
type (
resolver struct{}
queryResolver struct{}
)
func (r *resolver) Query() QueryResolver { return &queryResolver{} }
//go:generate go run github.com/99designs/gqlgen