## Summary: In this commit I remove one of the limitations of our support for interfaces, from #52, by adding support for list-of-interface fields. This was surprisingly complex! The issue is that, as before, it's the containing type that has to do all the glue work -- and it's that glue work that is complicated by list-of-interface fields. All in all, it's not that much new code, and by far the hard part is just 20 lines in the UnmarshalJSON template (which come with almost twice as many lines of comments to explain them). It may be easiest to start by reading some of the generated code, and then read the template. I also added support for such fields with `pointer: true` specified, such that the type is `[][]...[]*MyInterface`, although I don't know why you would want that. This does *not* allow e.g. `*[]*[][]*MyInterface`; that would require a way to specify it (see #16) but also add some extra complexity (as we'd have to actually walk the type-unwrap chain properly, instead of just counting the number of slices and whether there's a pointer). Issue: https://github.com/Khan/genqlient/issues/8 ## Test plan: make check Author: benjaminjkraft Reviewers: dnerdy, benjaminjkraft, aberkan, csilvers, MiguelCastillo Required Reviewers: Approved by: dnerdy Checks: ⌛ Test (1.17), ⌛ Test (1.16), ⌛ Test (1.15), ⌛ Test (1.14), ⌛ Test (1.13), ⌛ Lint, ⌛ Lint, ⌛ Test (1.17), ⌛ Test (1.16), ⌛ Test (1.15), ⌛ Test (1.14), ⌛ Test (1.13) Pull request URL: https://github.com/Khan/genqlient/pull/54
173 lines
4.9 KiB
Go
173 lines
4.9 KiB
Go
// Package integration contains genqlient's integration tests, which run
|
|
// against a real server (defined in internal/integration/server/server.go).
|
|
//
|
|
// These are especially important for cases where we generate nontrivial logic,
|
|
// such as JSON-unmarshaling.
|
|
package integration
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/Khan/genqlient/graphql"
|
|
"github.com/Khan/genqlient/internal/integration/server"
|
|
)
|
|
|
|
func TestSimpleQuery(t *testing.T) {
|
|
_ = `# @genqlient
|
|
query simpleQuery { me { id name luckyNumber } }`
|
|
|
|
ctx := context.Background()
|
|
server := server.RunServer()
|
|
defer server.Close()
|
|
client := graphql.NewClient(server.URL, http.DefaultClient)
|
|
|
|
resp, err := simpleQuery(ctx, client)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "1", resp.Me.Id)
|
|
assert.Equal(t, "Yours Truly", resp.Me.Name)
|
|
assert.Equal(t, 17, resp.Me.LuckyNumber)
|
|
}
|
|
|
|
func TestVariables(t *testing.T) {
|
|
_ = `# @genqlient
|
|
query queryWithVariables($id: ID!) { user(id: $id) { id name luckyNumber } }`
|
|
|
|
ctx := context.Background()
|
|
server := server.RunServer()
|
|
defer server.Close()
|
|
client := graphql.NewClient(server.URL, http.DefaultClient)
|
|
|
|
resp, err := queryWithVariables(ctx, client, "2")
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "2", resp.User.Id)
|
|
assert.Equal(t, "Raven", resp.User.Name)
|
|
assert.Equal(t, -1, resp.User.LuckyNumber)
|
|
|
|
resp, err = queryWithVariables(ctx, client, "374892379482379")
|
|
require.NoError(t, err)
|
|
|
|
assert.Zero(t, resp.User)
|
|
}
|
|
|
|
func TestInterfaceNoFragments(t *testing.T) {
|
|
_ = `# @genqlient
|
|
query queryWithInterfaceNoFragments($id: ID!) {
|
|
being(id: $id) { __typename id name }
|
|
me { id name }
|
|
}`
|
|
|
|
ctx := context.Background()
|
|
server := server.RunServer()
|
|
defer server.Close()
|
|
client := graphql.NewClient(server.URL, http.DefaultClient)
|
|
|
|
resp, err := queryWithInterfaceNoFragments(ctx, client, "1")
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "1", resp.Me.Id)
|
|
assert.Equal(t, "Yours Truly", resp.Me.Name)
|
|
|
|
user, ok := resp.Being.(*queryWithInterfaceNoFragmentsBeingUser)
|
|
require.Truef(t, ok, "got %T, not User", resp.Being)
|
|
assert.Equal(t, "1", user.Id)
|
|
assert.Equal(t, "Yours Truly", user.Name)
|
|
|
|
resp, err = queryWithInterfaceNoFragments(ctx, client, "3")
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "1", resp.Me.Id)
|
|
assert.Equal(t, "Yours Truly", resp.Me.Name)
|
|
|
|
animal, ok := resp.Being.(*queryWithInterfaceNoFragmentsBeingAnimal)
|
|
require.Truef(t, ok, "got %T, not Animal", resp.Being)
|
|
assert.Equal(t, "3", animal.Id)
|
|
assert.Equal(t, "Fido", animal.Name)
|
|
|
|
resp, err = queryWithInterfaceNoFragments(ctx, client, "4757233945723")
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "1", resp.Me.Id)
|
|
assert.Equal(t, "Yours Truly", resp.Me.Name)
|
|
|
|
assert.Nil(t, resp.Being)
|
|
}
|
|
|
|
func TestInterfaceListField(t *testing.T) {
|
|
_ = `# @genqlient
|
|
query queryWithInterfaceListField($ids: [ID!]!) {
|
|
beings(ids: $ids) { __typename id name }
|
|
}`
|
|
|
|
ctx := context.Background()
|
|
server := server.RunServer()
|
|
defer server.Close()
|
|
client := graphql.NewClient(server.URL, http.DefaultClient)
|
|
|
|
resp, err := queryWithInterfaceListField(ctx, client,
|
|
[]string{"1", "3", "12847394823"})
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, resp.Beings, 3)
|
|
|
|
user, ok := resp.Beings[0].(*queryWithInterfaceListFieldBeingsUser)
|
|
require.Truef(t, ok, "got %T, not User", resp.Beings[0])
|
|
assert.Equal(t, "1", user.Id)
|
|
assert.Equal(t, "Yours Truly", user.Name)
|
|
|
|
animal, ok := resp.Beings[1].(*queryWithInterfaceListFieldBeingsAnimal)
|
|
require.Truef(t, ok, "got %T, not Animal", resp.Beings[1])
|
|
assert.Equal(t, "3", animal.Id)
|
|
assert.Equal(t, "Fido", animal.Name)
|
|
|
|
assert.Nil(t, resp.Beings[2])
|
|
}
|
|
|
|
func TestInterfaceListPointerField(t *testing.T) {
|
|
_ = `# @genqlient
|
|
query queryWithInterfaceListPointerField($ids: [ID!]!) {
|
|
# @genqlient(pointer: true)
|
|
beings(ids: $ids) {
|
|
__typename id name
|
|
}
|
|
}`
|
|
|
|
ctx := context.Background()
|
|
server := server.RunServer()
|
|
defer server.Close()
|
|
client := graphql.NewClient(server.URL, http.DefaultClient)
|
|
|
|
resp, err := queryWithInterfaceListPointerField(ctx, client,
|
|
[]string{"1", "3", "12847394823"})
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, resp.Beings, 3)
|
|
|
|
user, ok := (*resp.Beings[0]).(*queryWithInterfaceListPointerFieldBeingsUser)
|
|
require.Truef(t, ok, "got %T, not User", resp.Beings[0])
|
|
assert.Equal(t, "1", user.Id)
|
|
assert.Equal(t, "Yours Truly", user.Name)
|
|
|
|
animal, ok := (*resp.Beings[1]).(*queryWithInterfaceListPointerFieldBeingsAnimal)
|
|
require.Truef(t, ok, "got %T, not Animal", resp.Beings[1])
|
|
assert.Equal(t, "3", animal.Id)
|
|
assert.Equal(t, "Fido", animal.Name)
|
|
|
|
assert.Nil(t, *resp.Beings[2])
|
|
}
|
|
|
|
func TestGeneratedCode(t *testing.T) {
|
|
// TODO(benkraft): Check that gqlgen is up to date too. In practice that's
|
|
// less likely to be a problem, since it should only change if you update
|
|
// the schema, likely too add something new, in which case you'll notice.
|
|
RunGenerateTest(t, "internal/integration/genqlient.yaml")
|
|
}
|
|
|
|
//go:generate go run github.com/Khan/genqlient genqlient.yaml
|