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

Entity resolver tests #1697

Merged
merged 2 commits into from
Nov 9, 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
102 changes: 102 additions & 0 deletions plugin/federation/federation_entityresolver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package federation

import (
"strings"
"testing"

"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/stretchr/testify/require"

"github.com/99designs/gqlgen/plugin/federation/testdata/entityresolver"
"github.com/99designs/gqlgen/plugin/federation/testdata/entityresolver/generated"
)

func TestEntityResolver(t *testing.T) {
c := client.New(handler.NewDefaultServer(
generated.NewExecutableSchema(generated.Config{
Resolvers: &entityresolver.Resolver{}}),
))

t.Run("Hello entities - single federation key", func(t *testing.T) {
representations := []map[string]interface{}{
{
"__typename": "Hello",
"name": "first name - 1",
}, {
"__typename": "Hello",
"name": "first name - 2",
},
}

var resp struct {
Entities []struct {
Name string `json:"name"`
} `json:"_entities"`
}

err := c.Post(
entityQuery([]string{
"Hello {name}",
}),
&resp,
client.Var("representations", representations),
)

require.NoError(t, err)
require.Equal(t, resp.Entities[0].Name, "first name - 1")
require.Equal(t, resp.Entities[1].Name, "first name - 2")
})

t.Run("World entity with nested key", func(t *testing.T) {
representations := []map[string]interface{}{
{
"__typename": "World",
"hello": map[string]interface{}{
"name": "world name - 1",
},
"foo": "foo 1",
}, {
"__typename": "World",
"hello": map[string]interface{}{
"name": "world name - 2",
},
"foo": "foo 2",
},
}

var resp struct {
Entities []struct {
Foo string `json:"foo"`
Hello struct {
Name string `json:"name"`
} `json:"hello"`
} `json:"_entities"`
}

err := c.Post(
entityQuery([]string{
"World {foo hello {name}}",
}),
&resp,
client.Var("representations", representations),
)

require.NoError(t, err)
require.Equal(t, resp.Entities[0].Foo, "foo 1")
require.Equal(t, resp.Entities[0].Hello.Name, "world name - 1")
require.Equal(t, resp.Entities[1].Foo, "foo 2")
require.Equal(t, resp.Entities[1].Hello.Name, "world name - 2")
})
}

func entityQuery(queries []string) string {
// What we want!
//query($representations:[_Any!]!){_entities(representations:$representations){ ...on Hello{secondary} }}
entityQueries := make([]string, len(queries))
for i, query := range queries {
entityQueries[i] = " ... on " + query
}

return "query($representations:[_Any!]!){_entities(representations:$representations){" + strings.Join(entityQueries, "") + "}}"
}
5 changes: 3 additions & 2 deletions plugin/federation/federation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestWithEntities(t *testing.T) {
f, cfg := load(t, "test_data/gqlgen.yml")
f, cfg := load(t, "testdata/allthethings/gqlgen.yml")

require.Equal(t, []string{"ExternalExtension", "Hello", "MoreNesting", "NestedKey", "VeryNestedKey", "World"}, cfg.Schema.Types["_Entity"].Types)

Expand Down Expand Up @@ -70,10 +70,11 @@ func TestWithEntities(t *testing.T) {
}

func TestNoEntities(t *testing.T) {
f, cfg := load(t, "test_data/nokey.yml")
f, cfg := load(t, "testdata/entities/nokey.yml")

err := f.MutateConfig(cfg)
require.NoError(t, err)
require.Len(t, f.Entities, 0)
}

func load(t *testing.T, name string) (*federation, *config.Config) {
Expand Down
17 changes: 17 additions & 0 deletions plugin/federation/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Federation plugin

Add support for graphql federation in your graphql Go server!

TODO(miguel): add details.

# Tests
There are several different tests. Some will process the configuration file directly. You can see those in the `federation_test.go`. There are also tests for entity resolvers, which will simulate requests from a federation server like Apollo Federation.

Running entity resolver tests.
1. Go to `plugin/federation`
2. Run the command `go run github.com/99designs/gqlgen --config testdata/entityresolver/gqlgen.yml`
3. Run the tests with `go test ./...`.

# Architecture

TODO(miguel): add details.
6 changes: 0 additions & 6 deletions plugin/federation/test_data/gqlgen.yml

This file was deleted.

6 changes: 0 additions & 6 deletions plugin/federation/test_data/nokey.yml

This file was deleted.

9 changes: 9 additions & 0 deletions plugin/federation/testdata/allthethings/gqlgen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
schema:
- "testdata/allthethings/schema.graphql"
exec:
filename: testdata/allthethings/generated/exec.go
federation:
filename: testdata/allthethings/generated/federation.go

autobind:
- "github.com/99designs/gqlgen/plugin/federation/testdata/allthethings/model"
48 changes: 48 additions & 0 deletions plugin/federation/testdata/allthethings/model/federation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package model

type _FieldSet string //nolint:deadcode,unused

type Hello struct {
Name string
Secondary string
}

func (Hello) IsEntity() {}

type World struct {
Foo string
Bar int
}

func (World) IsEntity() {}

type ExternalExtension struct {
UPC string
Reviews []*World
}

func (ExternalExtension) IsEntity() {}

type NestedKey struct {
ID string
Hello *Hello
}

func (NestedKey) IsEntity() {}

type MoreNesting struct {
ID string
World *World
}

func (MoreNesting) IsEntity() {}

type VeryNestedKey struct {
ID string
Hello *Hello
World *World
Nested *NestedKey
More *MoreNesting
}

func (VeryNestedKey) IsEntity() {}
6 changes: 6 additions & 0 deletions plugin/federation/testdata/entities/nokey.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
schema:
- "testdata/entities/nokey.graphql"
exec:
filename: testdata/entities/generated/exec.go
federation:
filename: testdata/entities/generated/federation.go
30 changes: 30 additions & 0 deletions plugin/federation/testdata/entityresolver/entity.resolvers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package entityresolver

// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.

import (
"context"

"github.com/99designs/gqlgen/plugin/federation/testdata/entityresolver/generated"
)

func (r *entityResolver) FindHelloByName(ctx context.Context, name string) (*generated.Hello, error) {
return &generated.Hello{
Name: name,
}, nil
}

func (r *entityResolver) FindWorldByHelloNameAndFoo(ctx context.Context, helloName string, foo string) (*generated.World, error) {
return &generated.World{
Hello: &generated.Hello{
Name: helloName,
},
Foo: foo,
}, nil
}

// Entity returns generated.EntityResolver implementation.
func (r *Resolver) Entity() generated.EntityResolver { return &entityResolver{r} }

type entityResolver struct{ *Resolver }
Loading