Skip to content

Commit

Permalink
Merge pull request 99designs#1016 from 99designs/federation-entity-type
Browse files Browse the repository at this point in the history
Create a non generated federation _Entity type
  • Loading branch information
vektah authored Feb 7, 2020
2 parents a1de04e + c23164e commit 5290844
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
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

0 comments on commit 5290844

Please sign in to comment.