From 8850a527a89f9b880531dc7fdcfec1016882b777 Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 7 Feb 2020 08:33:36 +1100 Subject: [PATCH] Create a non generated federation _Entity type --- plugin/federation/federation.go | 14 ++++++-------- plugin/federation/federation.gotpl | 12 ++++++------ plugin/federation/fedruntime/runtime.go | 13 +++++++++++++ plugin/modelgen/models.go | 8 -------- 4 files changed, 25 insertions(+), 22 deletions(-) create mode 100644 plugin/federation/fedruntime/runtime.go diff --git a/plugin/federation/federation.go b/plugin/federation/federation.go index fe7bb2ebf3..cca7a358a6 100644 --- a/plugin/federation/federation.go +++ b/plugin/federation/federation.go @@ -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": { @@ -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"` -} diff --git a/plugin/federation/federation.gotpl b/plugin/federation/federation.gotpl index 3672b0b84c..9fab3f04f0 100644 --- a/plugin/federation/federation.gotpl +++ b/plugin/federation/federation.gotpl @@ -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 { diff --git a/plugin/federation/fedruntime/runtime.go b/plugin/federation/fedruntime/runtime.go new file mode 100644 index 0000000000..43a924404a --- /dev/null +++ b/plugin/federation/fedruntime/runtime.go @@ -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() +} diff --git a/plugin/modelgen/models.go b/plugin/modelgen/models.go index 4f86a52f91..afcf65d69a 100644 --- a/plugin/modelgen/models.go +++ b/plugin/modelgen/models.go @@ -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{ @@ -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()]