Skip to content

Commit

Permalink
Move getTypeOf to execute.js and rename to defaultResolveTypeFn to mi…
Browse files Browse the repository at this point in the history
…rror defaultResolveFn

Commit:
edc405a11508a110759ce53c9efb2eb6dd2d181c [edc405a]
Parents:
3f6a7f4ca9
Author:
jeff@procata.com <jeff@procata.com>
Date:
24 March 2016 at 3:15:33 PM SGT
  • Loading branch information
sogko committed May 31, 2016
1 parent d46d545 commit 03b92b0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
26 changes: 0 additions & 26 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ func IsCompositeType(ttype interface{}) bool {
// Abstract interface for types that may describe the parent context of a selection set.
type Abstract interface {
Name() string
ObjectType(value interface{}, info ResolveInfo) *Object
PossibleTypes() []*Object
IsPossibleType(ttype *Object) bool
}
Expand Down Expand Up @@ -760,32 +759,13 @@ func (it *Interface) IsPossibleType(ttype *Object) bool {
}
return false
}
func (it *Interface) ObjectType(value interface{}, info ResolveInfo) *Object {
if it.ResolveType != nil {
return it.ResolveType(value, info)
}
return getTypeOf(value, info, it)
}
func (it *Interface) String() string {
return it.PrivateName
}
func (it *Interface) Error() error {
return it.err
}

func getTypeOf(value interface{}, info ResolveInfo, abstractType Abstract) *Object {
possibleTypes := abstractType.PossibleTypes()
for _, possibleType := range possibleTypes {
if possibleType.IsTypeOf == nil {
continue
}
if res := possibleType.IsTypeOf(value, info); res {
return possibleType
}
}
return nil
}

// Union Type Definition
//
// When a field can return one of a heterogeneous set of types, a Union type
Expand Down Expand Up @@ -901,12 +881,6 @@ func (ut *Union) IsPossibleType(ttype *Object) bool {
}
return false
}
func (ut *Union) ObjectType(value interface{}, info ResolveInfo) *Object {
if ut.ResolveType != nil {
return ut.ResolveType(value, info)
}
return getTypeOf(value, info, ut)
}
func (ut *Union) String() string {
return ut.PrivateName
}
Expand Down
41 changes: 33 additions & 8 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,14 +654,19 @@ func completeAbstractValue(eCtx *ExecutionContext, returnType Abstract, fieldAST

var runtimeType *Object

if returnType, ok := returnType.(Abstract); ok {
runtimeType = returnType.ObjectType(result, info)
if runtimeType != nil && !returnType.IsPossibleType(runtimeType) {
panic(gqlerrors.NewFormattedError(
fmt.Sprintf(`Runtime Object type "%v" is not a possible type `+
`for "%v".`, runtimeType, returnType),
))
}
if unionReturnType, ok := returnType.(*Union); ok && unionReturnType.ResolveType != nil {
runtimeType = unionReturnType.ResolveType(result, info)
} else if interfaceReturnType, ok := returnType.(*Interface); ok && interfaceReturnType.ResolveType != nil {
runtimeType = interfaceReturnType.ResolveType(result, info)
} else {
runtimeType = defaultResolveTypeFn(result, info, returnType)
}

if runtimeType != nil && !returnType.IsPossibleType(runtimeType) {
panic(gqlerrors.NewFormattedError(
fmt.Sprintf(`Runtime Object type "%v" is not a possible type `+
`for "%v".`, runtimeType, returnType),
))
}

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

// defaultResolveTypeFn If a resolveType function is not given, then a default resolve behavior is
// used which tests each possible type for the abstract type by calling
// isTypeOf for the object being coerced, returning the first type that matches.
func defaultResolveTypeFn(value interface{}, info ResolveInfo, abstractType Abstract) *Object {
possibleTypes := abstractType.PossibleTypes()
for _, possibleType := range possibleTypes {
if possibleType.IsTypeOf == nil {
continue
}
if res := possibleType.IsTypeOf(value, info); res {
return possibleType
}
}
return nil
}

// defaultResolveFn If a resolve function is not given, then a default resolve behavior is used
// which takes the property of the source object of the same name as the field
// and returns it as the result, or if it's a function, returns the result
// of calling that function.
func defaultResolveFn(p ResolveParams) (interface{}, error) {
// try to resolve p.Source as a struct first
sourceVal := reflect.ValueOf(p.Source)
Expand Down

0 comments on commit 03b92b0

Please sign in to comment.