Skip to content

Commit 91cd9a8

Browse files
author
Salman Ahmad
committed
Fixed bug to handle interfaces
1 parent ef8b880 commit 91cd9a8

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

internal/exec/resolvable/resolvable.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ func makeScalarExec(t *schema.Scalar, resolverType reflect.Type) (Resolvable, er
208208
return &Scalar{}, nil
209209
}
210210

211-
func (b *execBuilder) makeObjectExec(typeName string, fields schema.FieldList, possibleTypes []*schema.Object, nonNull bool, resolverType reflect.Type) (*Object, error) {
211+
func (b *execBuilder) makeObjectExec(typeName string, fields schema.FieldList, possibleTypes []*schema.Object,
212+
nonNull bool, resolverType reflect.Type) (*Object, error) {
212213

213214
if !nonNull {
214215
if resolverType.Kind() != reflect.Ptr && resolverType.Kind() != reflect.Interface {
@@ -219,19 +220,22 @@ func (b *execBuilder) makeObjectExec(typeName string, fields schema.FieldList, p
219220
methodHasReceiver := resolverType.Kind() != reflect.Interface
220221

221222
Fields := make(map[string]*Field)
223+
rt := unwrapPtr(resolverType)
222224
for _, f := range fields {
223225
methodIndex := -1
224226
fieldIndex := -1
225-
rt := unwrapPtr(resolverType)
226227

227228
/**
228-
* 1) Use resolver type's method for
229+
* 1) Use resolver type's method when
229230
* 1.1) __Type and __Schema requests
230-
* 1.2) or when field has arguments
231+
* 1.2) or field has arguments
231232
* 1.3) or it is configured to use method
233+
* 1.4) or it is an interface
232234
* 2) Otherwise use resolver type's field
233235
*/
234-
if isResolverSchemaOrType(rt) == true || len(f.Args) > 0 || conf.UseResolverMethods == true {
236+
//fmt.Printf("resName=%s, fName=%s\n", rt.Name(), f.Name)
237+
if isResolverSchemaOrType(rt) == true || len(f.Args) > 0 ||
238+
conf.UseResolverMethods == true || rt.Kind() == reflect.Interface {
235239
methodIndex = findMethod(resolverType, f.Name)
236240
} else {
237241
fieldIndex = findField(rt, f.Name)
@@ -259,22 +263,27 @@ func (b *execBuilder) makeObjectExec(typeName string, fields schema.FieldList, p
259263
Fields[f.Name] = fe
260264
}
261265

266+
// check type assertions when
267+
// 1) __Type and __Schema requests
268+
// 2) or it is configured to use method
262269
typeAssertions := make(map[string]*TypeAssertion)
263-
for _, impl := range possibleTypes {
264-
methodIndex := findMethod(resolverType, "To"+impl.Name)
265-
if methodIndex == -1 {
266-
return nil, fmt.Errorf("%s does not resolve %q: missing method %q to convert to %q", resolverType, typeName, "To"+impl.Name, impl.Name)
267-
}
268-
if resolverType.Method(methodIndex).Type.NumOut() != 2 {
269-
return nil, fmt.Errorf("%s does not resolve %q: method %q should return a value and a bool indicating success", resolverType, typeName, "To"+impl.Name)
270-
}
271-
a := &TypeAssertion{
272-
MethodIndex: methodIndex,
273-
}
274-
if err := b.assignExec(&a.TypeExec, impl, resolverType.Method(methodIndex).Type.Out(0)); err != nil {
275-
return nil, err
270+
if isResolverSchemaOrType(rt) == true || conf.UseResolverMethods == true {
271+
for _, impl := range possibleTypes {
272+
methodIndex := findMethod(resolverType, "To"+impl.Name)
273+
if methodIndex == -1 {
274+
return nil, fmt.Errorf("%s does not resolve %q: missing method %q to convert to %q", resolverType, typeName, "To"+impl.Name, impl.Name)
275+
}
276+
if resolverType.Method(methodIndex).Type.NumOut() != 2 {
277+
return nil, fmt.Errorf("%s does not resolve %q: method %q should return a value and a bool indicating success", resolverType, typeName, "To"+impl.Name)
278+
}
279+
a := &TypeAssertion{
280+
MethodIndex: methodIndex,
281+
}
282+
if err := b.assignExec(&a.TypeExec, impl, resolverType.Method(methodIndex).Type.Out(0)); err != nil {
283+
return nil, err
284+
}
285+
typeAssertions[impl.Name] = a
276286
}
277-
typeAssertions[impl.Name] = a
278287
}
279288

280289
return &Object{

0 commit comments

Comments
 (0)