Skip to content

Commit

Permalink
Merge pull request #173 from vvakame/feat-resolver-hint
Browse files Browse the repository at this point in the history
add resolver option support to field
  • Loading branch information
vektah committed Jul 8, 2018
2 parents 57b8279 + fcfceef commit ce65704
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 14 deletions.
1 change: 1 addition & 0 deletions codegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type TypeMapEntry struct {
}

type TypeMapField struct {
Resolver bool `yaml:"resolver"`
}

func (c *PackageConfig) normalize() error {
Expand Down
17 changes: 9 additions & 8 deletions codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ type Object struct {
type Field struct {
*Type

GQLName string // The name of the field in graphql
GoMethodName string // The name of the method in go, if any
GoVarName string // The name of the var in go, if any
Args []FieldArgument // A list of arguments to be passed to this field
NoErr bool // If this is bound to a go method, does that method have an error as the second argument
Object *Object // A link back to the parent object
Default interface{} // The default value
GQLName string // The name of the field in graphql
GoMethodName string // The name of the method in go, if any
GoVarName string // The name of the var in go, if any
Args []FieldArgument // A list of arguments to be passed to this field
ForceResolver bool // Should be emit Resolver method
NoErr bool // If this is bound to a go method, does that method have an error as the second argument
Object *Object // A link back to the parent object
Default interface{} // The default value
}

type FieldArgument struct {
Expand Down Expand Up @@ -60,7 +61,7 @@ func (o *Object) HasResolvers() bool {
}

func (f *Field) IsResolver() bool {
return f.GoMethodName == "" && f.GoVarName == ""
return f.ForceResolver || f.GoMethodName == "" && f.GoVarName == ""
}

func (f *Field) IsConcurrent() bool {
Expand Down
18 changes: 14 additions & 4 deletions codegen/object_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,21 @@ func sanitizeGoName(name string) string {

func (cfg *Config) buildObject(types NamedTypes, typ *schema.Object) (*Object, error) {
obj := &Object{NamedType: types[typ.TypeName()]}
typeEntry, entryExists := cfg.Models[typ.TypeName()]

for _, i := range typ.Interfaces {
obj.Satisfies = append(obj.Satisfies, i.Name)
}

for _, field := range typ.Fields {

var forceResolver bool
if entryExists {
if typeField, ok := typeEntry.Fields[field.Name]; ok {
forceResolver = typeField.Resolver
}
}

var args []FieldArgument
for _, arg := range field.Args {
newArg := FieldArgument{
Expand All @@ -108,10 +117,11 @@ func (cfg *Config) buildObject(types NamedTypes, typ *schema.Object) (*Object, e
}

obj.Fields = append(obj.Fields, Field{
GQLName: field.Name,
Type: types.getType(field.Type),
Args: args,
Object: obj,
GQLName: field.Name,
Type: types.getType(field.Type),
Args: args,
Object: obj,
ForceResolver: forceResolver,
})
}

Expand Down
3 changes: 3 additions & 0 deletions test/.gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ models:
model: github.com/vektah/gqlgen/test.Viewer
User:
model: remote_api.User
fields:
likes:
resolver: true
43 changes: 43 additions & 0 deletions test/generated.go

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

6 changes: 5 additions & 1 deletion test/resolvers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (r *testResolvers) Query_jsonEncoding(ctx context.Context) (string, error)

func (r *testResolvers) Query_viewer(ctx context.Context) (*Viewer, error) {
return &Viewer{
User: &remote_api.User{"Bob"},
User: &remote_api.User{Name: "Bob"},
}, nil
}

Expand All @@ -129,6 +129,10 @@ func (r *testResolvers) Element_error(ctx context.Context, obj *Element) (bool,
return false, r.err
}

func (r *testResolvers) User_likes(ctx context.Context, obj *remote_api.User) ([]string, error) {
return obj.Likes, nil
}

type specialErr struct{}

func (*specialErr) Error() string {
Expand Down
2 changes: 2 additions & 0 deletions test/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ input DateFilter {

type User {
name: String
likes: [String]
}

type Viewer {
user: User
}
Expand Down
3 changes: 2 additions & 1 deletion test/vendor/remote_api/user.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package remote_api

type User struct {
Name string
Name string
Likes []string
}

0 comments on commit ce65704

Please sign in to comment.