diff --git a/src/execution/execute.js b/src/execution/execute.js index f419ebc156..620e507577 100644 --- a/src/execution/execute.js +++ b/src/execution/execute.js @@ -798,7 +798,13 @@ function completeAbstractValue( info: GraphQLResolveInfo, result: mixed ): mixed { - const runtimeType = returnType.getObjectType(result, info); + let runtimeType: ?GraphQLObjectType; + if (returnType.resolveType) { + runtimeType = returnType.resolveType(result, info); + } else { + runtimeType = defaultResolveTypeFn(result, info, returnType); + } + if (runtimeType && !returnType.isPossibleType(runtimeType)) { throw new GraphQLError( `Runtime Object type "${runtimeType}" is not a possible type ` + @@ -859,6 +865,25 @@ function completeObjectValue( return executeFields(exeContext, returnType, result, subFieldASTs); } +/** + * 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. + */ +function defaultResolveTypeFn( + value: mixed, + info: GraphQLResolveInfo, + abstractType: GraphQLAbstractType +): ?GraphQLObjectType { + const possibleTypes = abstractType.getPossibleTypes(); + for (let i = 0; i < possibleTypes.length; i++) { + const type = possibleTypes[i]; + if (typeof type.isTypeOf === 'function' && type.isTypeOf(value, info)) { + return type; + } + } +} + /** * 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 diff --git a/src/type/definition.js b/src/type/definition.js index 8c360173c0..e0e219fc74 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -600,30 +600,11 @@ export class GraphQLInterfaceType { return Boolean(possibleTypes[type.name]); } - getObjectType(value: mixed, info: GraphQLResolveInfo): ?GraphQLObjectType { - const resolver = this.resolveType; - return resolver ? resolver(value, info) : getTypeOf(value, info, this); - } - toString(): string { return this.name; } } -function getTypeOf( - value: mixed, - info: GraphQLResolveInfo, - abstractType: GraphQLAbstractType -): ?GraphQLObjectType { - const possibleTypes = abstractType.getPossibleTypes(); - for (let i = 0; i < possibleTypes.length; i++) { - const type = possibleTypes[i]; - if (typeof type.isTypeOf === 'function' && type.isTypeOf(value, info)) { - return type; - } - } -} - export type GraphQLInterfaceTypeConfig = { name: string, fields: GraphQLFieldConfigMapThunk | GraphQLFieldConfigMap, @@ -721,11 +702,6 @@ export class GraphQLUnionType { return possibleTypeNames[type.name] === true; } - getObjectType(value: mixed, info: GraphQLResolveInfo): ?GraphQLObjectType { - const resolver = this._typeConfig.resolveType; - return resolver ? resolver(value, info) : getTypeOf(value, info, this); - } - toString(): string { return this.name; }