Skip to content

Commit

Permalink
Fix issues with resolver implementation resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathew Byrne committed Sep 25, 2018
1 parent 621d248 commit f366877
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 15 deletions.
5 changes: 5 additions & 0 deletions codegen/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ func (cfg *Config) bind() (*Build, error) {
namedTypes := cfg.buildNamedTypes()

progLoader := newLoader(namedTypes, true)
for _, i := range cfg.Directives.Implementations() {
pkg, _ := pkgAndType(i)
progLoader.Import(pkg)
}

prog, err := progLoader.Load()
if err != nil {
return nil, errors.Wrap(err, "loading failed")
Expand Down
8 changes: 5 additions & 3 deletions codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/pkg/errors"
"github.com/vektah/gqlparser"
"github.com/vektah/gqlparser/ast"
"github.com/vektah/gqlparser/gqlerror"
)

func Generate(cfg Config) error {
Expand Down Expand Up @@ -160,9 +161,10 @@ func (cfg *Config) normalize() error {
}
srcs = append(srcs, pluginSrcs...)

cfg.schema, err = gqlparser.LoadSchema(srcs...)
if err != nil {
return err
var gqlerr *gqlerror.Error
cfg.schema, gqlerr = gqlparser.LoadSchema(srcs...)
if gqlerr != nil {
return gqlerr
}
return nil
}
Expand Down
10 changes: 10 additions & 0 deletions codegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ func (dm DirectiveMap) ImplementationFor(name string) string {
return d.Implementation
}

func (dm DirectiveMap) Implementations() []string {
impls := []string{}
for _, d := range dm {
if d.Implementation != "" {
impls = append(impls, d.Implementation)
}
}
return impls
}

// findCfg searches for the config file in this directory and all parents up the tree
// looking for the closest match
func findCfg() (string, error) {
Expand Down
21 changes: 13 additions & 8 deletions codegen/plugin.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
package codegen

import "github.com/vektah/gqlparser/ast"
import (
"github.com/vektah/gqlparser/ast"
)

type Plugin interface {
Execute(c *Config, schema *ast.Schema) error
Schema(cfg *Config) (*ast.Source, error)
}

type PluginRegistry []Plugin
type PluginRegistry struct {
plugins []Plugin
}

var DefaultPluginRegistry = PluginRegistry{}

// RegisterPlugin registers a plugin in the default plugin register
func RegisterPlugin(p Plugin) {

DefaultPluginRegistry.Register(p)
}

func (r PluginRegistry) Register(p Plugin) {
r = append(r, p)
func (r *PluginRegistry) Register(p Plugin) {
r.plugins = append(r.plugins, p)
}

func (r PluginRegistry) Schemas(c *Config) (srcs []*ast.Source, err error) {
for _, p := range r {
func (r *PluginRegistry) Schemas(c *Config) (srcs []*ast.Source, err error) {
for _, p := range r.plugins {
src, err := p.Schema(c)
if err != nil {
return nil, err
Expand All @@ -33,8 +38,8 @@ func (r PluginRegistry) Schemas(c *Config) (srcs []*ast.Source, err error) {
return srcs, err
}

func (r PluginRegistry) Execute(cfg *Config, schema *ast.Schema) error {
for _, p := range r {
func (r *PluginRegistry) Execute(cfg *Config, schema *ast.Schema) error {
for _, p := range r.plugins {
if err := p.Execute(cfg, schema); err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions codegen/plugins/resolver/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func (p *Plugin) Execute(cfg *codegen.Config, schema *ast.Schema) error {
if _, ok := cfg.Models[typ.Name]; !ok {
cfg.Models[typ.Name] = codegen.TypeMapEntry{}
}
if cfg.Models[typ.Name].Fields == nil {
cfg.Models[typ.Name] = codegen.TypeMapEntry{
Model: typ.Name,
Fields: make(map[string]codegen.TypeMapField),
}
}
if tmf, ok := cfg.Models[typ.Name].Fields[f.Name]; ok {
cfg.Models[typ.Name].Fields[f.Name] = codegen.TypeMapField{
ForceResolver: true,
Expand Down
8 changes: 7 additions & 1 deletion codegen/testserver/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions codegen/testserver/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ models:
model: "github.com/99designs/gqlgen/codegen/testserver.Rectangle"
ForcedResolver:
model: "github.com/99designs/gqlgen/codegen/testserver.ForcedResolver"
fields:
field: { resolver: true }
Error:
model: "github.com/99designs/gqlgen/codegen/testserver.Error"

Expand Down
2 changes: 1 addition & 1 deletion codegen/testserver/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ type Rectangle implements Shape {
union ShapeUnion = Circle | Rectangle

type ForcedResolver {
field: Circle
field: Circle @resolver
}

input ComplexInput {
Expand Down

0 comments on commit f366877

Please sign in to comment.