Skip to content

Commit edc405a

Browse files
committed
Move getTypeOf to execute.js and rename to defaultResolveTypeFn to mirror defaultResolveFn
1 parent 3f6a7f4 commit edc405a

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

src/execution/execute.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,13 @@ function completeAbstractValue(
798798
info: GraphQLResolveInfo,
799799
result: mixed
800800
): mixed {
801-
const runtimeType = returnType.getObjectType(result, info);
801+
let runtimeType: ?GraphQLObjectType;
802+
if (returnType.resolveType) {
803+
runtimeType = returnType.resolveType(result, info);
804+
} else {
805+
runtimeType = defaultResolveTypeFn(result, info, returnType);
806+
}
807+
802808
if (runtimeType && !returnType.isPossibleType(runtimeType)) {
803809
throw new GraphQLError(
804810
`Runtime Object type "${runtimeType}" is not a possible type ` +
@@ -859,6 +865,25 @@ function completeObjectValue(
859865
return executeFields(exeContext, returnType, result, subFieldASTs);
860866
}
861867

868+
/**
869+
* If a resolveType function is not given, then a default resolve behavior is
870+
* used which tests each possible type for the abstract type by calling
871+
* isTypeOf for the object being coerced, returning the first type that matches.
872+
*/
873+
function defaultResolveTypeFn(
874+
value: mixed,
875+
info: GraphQLResolveInfo,
876+
abstractType: GraphQLAbstractType
877+
): ?GraphQLObjectType {
878+
const possibleTypes = abstractType.getPossibleTypes();
879+
for (let i = 0; i < possibleTypes.length; i++) {
880+
const type = possibleTypes[i];
881+
if (typeof type.isTypeOf === 'function' && type.isTypeOf(value, info)) {
882+
return type;
883+
}
884+
}
885+
}
886+
862887
/**
863888
* If a resolve function is not given, then a default resolve behavior is used
864889
* which takes the property of the source object of the same name as the field

src/type/definition.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -600,30 +600,11 @@ export class GraphQLInterfaceType {
600600
return Boolean(possibleTypes[type.name]);
601601
}
602602

603-
getObjectType(value: mixed, info: GraphQLResolveInfo): ?GraphQLObjectType {
604-
const resolver = this.resolveType;
605-
return resolver ? resolver(value, info) : getTypeOf(value, info, this);
606-
}
607-
608603
toString(): string {
609604
return this.name;
610605
}
611606
}
612607

613-
function getTypeOf(
614-
value: mixed,
615-
info: GraphQLResolveInfo,
616-
abstractType: GraphQLAbstractType
617-
): ?GraphQLObjectType {
618-
const possibleTypes = abstractType.getPossibleTypes();
619-
for (let i = 0; i < possibleTypes.length; i++) {
620-
const type = possibleTypes[i];
621-
if (typeof type.isTypeOf === 'function' && type.isTypeOf(value, info)) {
622-
return type;
623-
}
624-
}
625-
}
626-
627608
export type GraphQLInterfaceTypeConfig = {
628609
name: string,
629610
fields: GraphQLFieldConfigMapThunk | GraphQLFieldConfigMap,
@@ -721,11 +702,6 @@ export class GraphQLUnionType {
721702
return possibleTypeNames[type.name] === true;
722703
}
723704

724-
getObjectType(value: mixed, info: GraphQLResolveInfo): ?GraphQLObjectType {
725-
const resolver = this._typeConfig.resolveType;
726-
return resolver ? resolver(value, info) : getTypeOf(value, info, this);
727-
}
728-
729705
toString(): string {
730706
return this.name;
731707
}

0 commit comments

Comments
 (0)