Skip to content

Commit

Permalink
fix default resolver, introspection and typeWalker
Browse files Browse the repository at this point in the history
  • Loading branch information
Fontinalis committed Apr 24, 2020
1 parent ab2c51b commit cdc5319
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
33 changes: 24 additions & 9 deletions execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,16 @@ func defaultResolver(fname string) Resolver {
return func(ctx Context) (interface{}, error) {
t := reflect.TypeOf(ctx.Parent())
v := reflect.ValueOf(ctx.Parent())

if t.Kind() == reflect.Ptr {
t = t.Elem()
v = v.Elem()
}

if t.Kind() != reflect.Struct {
return nil, nil
}

for i := 0; i < t.NumField(); i++ {
// Get the field, returns https://golang.org/pkg/reflect/#StructField
field := t.Field(i)
Expand Down Expand Up @@ -930,11 +940,12 @@ func completeValue(ctx *gqlCtx, path []interface{}, ft Type, fs ast.Fields, resu

func getTypes(s *Schema) (map[string]Type, map[string]Directive, map[string][]Type) {
types := map[string]Type{
"String": String,
"Boolean": Boolean,
"Int": Int,
"ID": ID,
"Float": Float,
"String": String,
"Boolean": Boolean,
"Int": Int,
"ID": ID,
"Float": Float,
"DateTime": DateTime,
}
directives := map[string]Directive{
"skip": skipDirective,
Expand Down Expand Up @@ -974,7 +985,7 @@ func typeWalker(types map[string]Type, directives map[string]Directive, implemen
return
}
// TODO: directives are not checked and "walked" through
if hf, ok := t.(hasFields); ok {
if hf, ok := wt.(hasFields); ok {
if o, ok := t.(*Object); ok {
for _, i := range o.Implements {
if os, ok := implementors[i.Name]; ok {
Expand All @@ -987,15 +998,19 @@ func typeWalker(types map[string]Type, directives map[string]Directive, implemen
}
}
for _, f := range hf.GetFields() {
for _, arg := range f.GetArguments() {
for _, arg := range f.Arguments {
typeWalker(types, directives, implementors, arg.Type)
}
typeWalker(types, directives, implementors, f.GetType())
typeWalker(types, directives, implementors, f.Type)
}
} else if u, ok := wt.(*Union); ok {
for _, m := range u.GetMembers() {
for _, m := range u.Members {
typeWalker(types, directives, implementors, m)
}
} else if io, ok := wt.(*InputObject); ok {
for _, f := range io.Fields {
typeWalker(types, directives, implementors, f.Type)
}
}
}

Expand Down
31 changes: 25 additions & 6 deletions introspection.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package gql

import (
"encoding/json"
)

func init() {
typeIntrospection.AddField(
"interfaces",
Expand Down Expand Up @@ -403,12 +407,27 @@ var (
Type: String,
Resolver: func(ctx Context) (interface{}, error) {
if v, ok := ctx.Parent().(*iArgument); ok && v.arg.IsDefaultValueSet() {
return v.arg.DefaultValue, nil
} else if v, ok := ctx.Parent().(struct {
field *InputField
name string
}); ok && v.field.IsDefaultValueSet() {
return v.field.DefaultValue, nil
if unwrapper(v.arg.Type).GetKind() == EnumKind {
return v.arg.DefaultValue, nil
}
if v.arg.DefaultValue != nil {
bs, err := json.Marshal(v.arg.DefaultValue)
if err != nil {
return nil, err
}
return string(bs), nil
}
} else if v, ok := ctx.Parent().(iInputField); ok && v.field.IsDefaultValueSet() {
if unwrapper(v.field.Type).GetKind() == EnumKind {
return v.field.DefaultValue, nil
}
if v.field.DefaultValue != nil {
bs, err := json.Marshal(v.field.DefaultValue)
if err != nil {
return nil, err
}
return string(bs), nil
}
}
return nil, nil
},
Expand Down

0 comments on commit cdc5319

Please sign in to comment.