Skip to content

Commit 497e762

Browse files
committed
Use __typename detection only if resolveType and isTypeOf missing.
1 parent 062c9d9 commit 497e762

File tree

5 files changed

+16
-81
lines changed

5 files changed

+16
-81
lines changed

src/execution/execute.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -1047,9 +1047,9 @@ function completeAbstractValue(
10471047
path: ResponsePath,
10481048
result: mixed
10491049
): mixed {
1050-
const runtimeType = exeContext.typeResolver(
1051-
result, exeContext, info, returnType
1052-
);
1050+
const runtimeType = returnType.resolveType ?
1051+
returnType.resolveType(result, exeContext.contextValue, info) :
1052+
defaultResolveTypeFn(result, exeContext, info, returnType);
10531053
10541054
const promise = getPromise(runtimeType);
10551055
if (promise) {
@@ -1215,10 +1215,11 @@ function collectAndExecuteSubfields(
12151215
*/
12161216
function defaultResolveTypeFn(
12171217
value: mixed,
1218-
context: mixed,
1218+
exeContext: ExecutionContext,
12191219
info: GraphQLResolveInfo,
12201220
abstractType: GraphQLAbstractType
1221-
): ?GraphQLObjectType | Promise<?GraphQLObjectType> {
1221+
): ?GraphQLObjectType | string | Promise<?GraphQLObjectType | string> {
1222+
const context = exeContext.contextValue;
12221223
const possibleTypes = info.schema.getPossibleTypes(abstractType);
12231224
const promisedIsTypeOfResults = [];
12241225

@@ -1246,6 +1247,8 @@ function defaultResolveTypeFn(
12461247
}
12471248
});
12481249
}
1250+
1251+
return exeContext.typeResolver(value, context, info);
12491252
}
12501253

12511254
/**
@@ -1267,14 +1270,14 @@ function (source, args, context, info) {
12671270
};
12681271

12691272
export const defaultTypeResolver: GraphQLTypeResolver<any, *> =
1270-
function (result, context, info, returnType) {
1271-
if (result && result.__typename) {
1272-
return result.__typename;
1273+
function (result, context, info) {
1274+
if (typeof result === 'object' && result.__typename) {
1275+
const typename = result.__typename;
1276+
if (typeof typename === 'function') {
1277+
return result.__typename(context, info);
1278+
}
1279+
return typename;
12731280
}
1274-
1275-
return returnType.resolveType ?
1276-
returnType.resolveType(result, context.contextValue, info) :
1277-
defaultResolveTypeFn(result, context.contextValue, info, returnType);
12781281
};
12791282

12801283
/**

src/type/__tests__/validation-test.js

-33
Original file line numberDiff line numberDiff line change
@@ -968,26 +968,6 @@ describe('Type System: Interface types must be resolvable', () => {
968968
);
969969
});
970970

971-
it('rejects an Interface type not defining resolveType with implementing type not defining isTypeOf', () => {
972-
expect(() => {
973-
const InterfaceTypeWithoutResolveType = new GraphQLInterfaceType({
974-
name: 'InterfaceTypeWithoutResolveType',
975-
fields: { f: { type: GraphQLString } }
976-
});
977-
978-
schemaWithFieldType(new GraphQLObjectType({
979-
name: 'SomeObject',
980-
interfaces: [ InterfaceTypeWithoutResolveType ],
981-
fields: { f: { type: GraphQLString } }
982-
}));
983-
}).to.throw(
984-
'Interface Type InterfaceTypeWithoutResolveType does not provide a ' +
985-
'"resolveType" function and implementing Type SomeObject does not ' +
986-
'provide a "isTypeOf" function. ' +
987-
'There is no way to resolve this implementing type during execution.'
988-
);
989-
});
990-
991971
});
992972

993973

@@ -1034,19 +1014,6 @@ describe('Type System: Union types must be resolvable', () => {
10341014
);
10351015
});
10361016

1037-
it('rejects a Union type not defining resolveType of Object types not defining isTypeOf', () => {
1038-
expect(() =>
1039-
schemaWithFieldType(new GraphQLUnionType({
1040-
name: 'SomeUnion',
1041-
types: [ SomeObjectType ],
1042-
}))
1043-
).to.throw(
1044-
'Union type "SomeUnion" does not provide a "resolveType" function and ' +
1045-
'possible type "SomeObject" does not provide an "isTypeOf" function. ' +
1046-
'There is no way to resolve this possible type during execution.'
1047-
);
1048-
});
1049-
10501017
});
10511018

10521019

src/type/definition.js

+1-20
Original file line numberDiff line numberDiff line change
@@ -501,15 +501,6 @@ function defineInterfaces(
501501
`${type.name} may declare it implements ${iface.name} only once.`
502502
);
503503
implementedTypeNames[iface.name] = true;
504-
if (typeof iface.resolveType !== 'function') {
505-
invariant(
506-
typeof type.isTypeOf === 'function',
507-
`Interface Type ${iface.name} does not provide a "resolveType" ` +
508-
`function and implementing Type ${type.name} does not provide a ` +
509-
'"isTypeOf" function. There is no way to resolve this implementing ' +
510-
'type during execution.'
511-
);
512-
}
513504
});
514505
return interfaces;
515506
}
@@ -614,8 +605,7 @@ export type GraphQLObjectTypeConfig<TSource, TContext> = {
614605
export type GraphQLTypeResolver<TSource, TContext> = (
615606
value: TSource,
616607
context: TContext,
617-
info: GraphQLResolveInfo,
618-
returnValue: any
608+
info: GraphQLResolveInfo
619609
) => ?GraphQLObjectType | string | Promise<?GraphQLObjectType | string>;
620610

621611
export type GraphQLIsTypeOfFn<TSource, TContext> = (
@@ -862,15 +852,6 @@ function defineTypes(
862852
`${unionType.name} can include ${objType.name} type only once.`
863853
);
864854
includedTypeNames[objType.name] = true;
865-
if (typeof unionType.resolveType !== 'function') {
866-
invariant(
867-
typeof objType.isTypeOf === 'function',
868-
`Union type "${unionType.name}" does not provide a "resolveType" ` +
869-
`function and possible type "${objType.name}" does not provide an ` +
870-
'"isTypeOf" function. There is no way to resolve this possible type ' +
871-
'during execution.'
872-
);
873-
}
874855
});
875856

876857
return types;

src/utilities/buildASTSchema.js

-8
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,6 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
398398
description: getDescription(def),
399399
fields: () => makeFieldDefMap(def),
400400
astNode: def,
401-
resolveType: cannotExecuteSchema,
402401
});
403402
}
404403

@@ -424,7 +423,6 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
424423
name: def.name.value,
425424
description: getDescription(def),
426425
types: def.types.map(t => produceObjectType(t)),
427-
resolveType: cannotExecuteSchema,
428426
astNode: def,
429427
});
430428
}
@@ -516,9 +514,3 @@ function leadingSpaces(str) {
516514
}
517515
return i;
518516
}
519-
520-
function cannotExecuteSchema() {
521-
throw new Error(
522-
'Generated Schema cannot use Interface or Union types for execution.'
523-
);
524-
}

src/utilities/extendSchema.js

-8
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,6 @@ export function extendSchema(
479479
description: getDescription(typeNode),
480480
fields: () => buildFieldMap(typeNode),
481481
astNode: typeNode,
482-
resolveType: cannotExecuteExtendedSchema,
483482
});
484483
}
485484

@@ -489,7 +488,6 @@ export function extendSchema(
489488
description: getDescription(typeNode),
490489
types: typeNode.types.map(getObjectTypeFromAST),
491490
astNode: typeNode,
492-
resolveType: cannotExecuteExtendedSchema,
493491
});
494492
}
495493

@@ -608,9 +606,3 @@ export function extendSchema(
608606
return getOutputTypeFromAST(typeNode);
609607
}
610608
}
611-
612-
function cannotExecuteExtendedSchema() {
613-
throw new Error(
614-
'Extended Schema cannot use Interface or Union types for execution.'
615-
);
616-
}

0 commit comments

Comments
 (0)