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

Create a non generated federation _Entity type #1016

Merged
merged 1 commit into from
Feb 7, 2020
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
14 changes: 6 additions & 8 deletions plugin/federation/federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ func (f *federation) MutateConfig(cfg *config.Config) error {
builtins := config.TypeMap{
"_Service": {
Model: config.StringList{
"github.com/99designs/gqlgen/plugin/federation.Service",
"github.com/99designs/gqlgen/plugin/federation/fedruntime.Service",
},
},
"_Entity": {
Model: config.StringList{
"github.com/99designs/gqlgen/plugin/federation/fedruntime.Entity",
},
},
"_Any": {
Expand Down Expand Up @@ -361,10 +366,3 @@ func (f *federation) getSDL(c *config.Config) (string, error) {
formatter.NewFormatter(&buf).FormatSchema(schema)
return buf.String(), nil
}

// Service is the service object that the
// generated.go file will return for the _service
// query
type Service struct {
SDL string `json:"sdl"`
}
12 changes: 6 additions & 6 deletions plugin/federation/federation.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
{{ reserveImport "errors" }}
{{ reserveImport "fmt" }}

{{ reserveImport "github.com/99designs/gqlgen/plugin/federation" }}
{{ reserveImport "github.com/99designs/gqlgen/plugin/federation/fedruntime" }}

func (ec *executionContext) __resolve__service(ctx context.Context) (federation.Service, error) {
func (ec *executionContext) __resolve__service(ctx context.Context) (fedruntime.Service, error) {
if ec.DisableIntrospection {
return federation.Service{}, errors.New("federated introspection disabled")
return fedruntime.Service{}, errors.New("federated introspection disabled")
}
return federation.Service{
return fedruntime.Service{
SDL: `{{.SDL}}`,
}, nil
}

{{if .Entities}}
func (ec *executionContext) __resolve_entities(ctx context.Context, representations []map[string]interface{}) ([]_Entity, error) {
list := []_Entity{}
func (ec *executionContext) __resolve_entities(ctx context.Context, representations []map[string]interface{}) ([]fedruntime.Entity, error) {
list := []fedruntime.Entity{}
for _, rep := range representations {
typeName, ok := rep["__typename"].(string)
if !ok {
Expand Down
13 changes: 13 additions & 0 deletions plugin/federation/fedruntime/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fedruntime

// Service is the service object that the
// generated.go file will return for the _service
// query
type Service struct {
SDL string `json:"sdl"`
}

// Everything with a @key implements this
type Entity interface {
Is_Entity()
}
8 changes: 0 additions & 8 deletions plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error {
if cfg.Models.UserDefined(schemaType.Name) {
continue
}
ent := false
if schemaType.Directives.ForName("key") != nil {
hasEntity = true
ent = true
}
switch schemaType.Kind {
case ast.Interface, ast.Union:
it := &Interface{
Expand All @@ -108,9 +103,6 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error {
it.Implements = append(it.Implements, implementor.Name)
}

if ent { // only when Object. Directive validation should have occurred on InputObject otherwise.
it.Implements = append(it.Implements, "_Entity")
}
for _, field := range schemaType.Fields {
var typ types.Type
fieldDef := cfg.Schema.Types[field.Type.Name()]
Expand Down