Skip to content

Commit

Permalink
Merge pull request #328 from JeffRMoore/default-resolve-type-fn
Browse files Browse the repository at this point in the history
Move getTypeOf to execute.js and rename to defaultResolveTypeFn
  • Loading branch information
leebyron committed Mar 24, 2016
2 parents 81671dc + edc405a commit 8ba02b2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
27 changes: 26 additions & 1 deletion src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ` +
Expand Down Expand Up @@ -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
Expand Down
24 changes: 0 additions & 24 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 8ba02b2

Please sign in to comment.