Skip to content

Commit 03b92b0

Browse files
committed
Move getTypeOf to execute.js and rename to defaultResolveTypeFn to mirror defaultResolveFn
Commit: edc405a11508a110759ce53c9efb2eb6dd2d181c [edc405a] Parents: 3f6a7f4ca9 Author: [email protected] <[email protected]> Date: 24 March 2016 at 3:15:33 PM SGT
1 parent d46d545 commit 03b92b0

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

definition.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ func IsCompositeType(ttype interface{}) bool {
143143
// Abstract interface for types that may describe the parent context of a selection set.
144144
type Abstract interface {
145145
Name() string
146-
ObjectType(value interface{}, info ResolveInfo) *Object
147146
PossibleTypes() []*Object
148147
IsPossibleType(ttype *Object) bool
149148
}
@@ -760,32 +759,13 @@ func (it *Interface) IsPossibleType(ttype *Object) bool {
760759
}
761760
return false
762761
}
763-
func (it *Interface) ObjectType(value interface{}, info ResolveInfo) *Object {
764-
if it.ResolveType != nil {
765-
return it.ResolveType(value, info)
766-
}
767-
return getTypeOf(value, info, it)
768-
}
769762
func (it *Interface) String() string {
770763
return it.PrivateName
771764
}
772765
func (it *Interface) Error() error {
773766
return it.err
774767
}
775768

776-
func getTypeOf(value interface{}, info ResolveInfo, abstractType Abstract) *Object {
777-
possibleTypes := abstractType.PossibleTypes()
778-
for _, possibleType := range possibleTypes {
779-
if possibleType.IsTypeOf == nil {
780-
continue
781-
}
782-
if res := possibleType.IsTypeOf(value, info); res {
783-
return possibleType
784-
}
785-
}
786-
return nil
787-
}
788-
789769
// Union Type Definition
790770
//
791771
// When a field can return one of a heterogeneous set of types, a Union type
@@ -901,12 +881,6 @@ func (ut *Union) IsPossibleType(ttype *Object) bool {
901881
}
902882
return false
903883
}
904-
func (ut *Union) ObjectType(value interface{}, info ResolveInfo) *Object {
905-
if ut.ResolveType != nil {
906-
return ut.ResolveType(value, info)
907-
}
908-
return getTypeOf(value, info, ut)
909-
}
910884
func (ut *Union) String() string {
911885
return ut.PrivateName
912886
}

executor.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -654,14 +654,19 @@ func completeAbstractValue(eCtx *ExecutionContext, returnType Abstract, fieldAST
654654

655655
var runtimeType *Object
656656

657-
if returnType, ok := returnType.(Abstract); ok {
658-
runtimeType = returnType.ObjectType(result, info)
659-
if runtimeType != nil && !returnType.IsPossibleType(runtimeType) {
660-
panic(gqlerrors.NewFormattedError(
661-
fmt.Sprintf(`Runtime Object type "%v" is not a possible type `+
662-
`for "%v".`, runtimeType, returnType),
663-
))
664-
}
657+
if unionReturnType, ok := returnType.(*Union); ok && unionReturnType.ResolveType != nil {
658+
runtimeType = unionReturnType.ResolveType(result, info)
659+
} else if interfaceReturnType, ok := returnType.(*Interface); ok && interfaceReturnType.ResolveType != nil {
660+
runtimeType = interfaceReturnType.ResolveType(result, info)
661+
} else {
662+
runtimeType = defaultResolveTypeFn(result, info, returnType)
663+
}
664+
665+
if runtimeType != nil && !returnType.IsPossibleType(runtimeType) {
666+
panic(gqlerrors.NewFormattedError(
667+
fmt.Sprintf(`Runtime Object type "%v" is not a possible type `+
668+
`for "%v".`, runtimeType, returnType),
669+
))
665670
}
666671

667672
if runtimeType == nil {
@@ -749,6 +754,26 @@ func completeListValue(eCtx *ExecutionContext, returnType *List, fieldASTs []*as
749754
return completedResults
750755
}
751756

757+
// defaultResolveTypeFn If a resolveType function is not given, then a default resolve behavior is
758+
// used which tests each possible type for the abstract type by calling
759+
// isTypeOf for the object being coerced, returning the first type that matches.
760+
func defaultResolveTypeFn(value interface{}, info ResolveInfo, abstractType Abstract) *Object {
761+
possibleTypes := abstractType.PossibleTypes()
762+
for _, possibleType := range possibleTypes {
763+
if possibleType.IsTypeOf == nil {
764+
continue
765+
}
766+
if res := possibleType.IsTypeOf(value, info); res {
767+
return possibleType
768+
}
769+
}
770+
return nil
771+
}
772+
773+
// defaultResolveFn If a resolve function is not given, then a default resolve behavior is used
774+
// which takes the property of the source object of the same name as the field
775+
// and returns it as the result, or if it's a function, returns the result
776+
// of calling that function.
752777
func defaultResolveFn(p ResolveParams) (interface{}, error) {
753778
// try to resolve p.Source as a struct first
754779
sourceVal := reflect.ValueOf(p.Source)

0 commit comments

Comments
 (0)