Skip to content

Commit c650f30

Browse files
committed
Support for union types when using buildSchema (#947)
Closes #947 Squashed commit of the following: commit 8d19d2634d728abfe61a50070259c588096343e2 Author: Lee Byron <[email protected]> Date: Fri Dec 1 13:45:24 2017 -0800 Clean up existing tests and improve error messages commit 95018b1a8484dea64f29faf48f111a3906df348e Author: Lee Byron <[email protected]> Date: Fri Dec 1 13:29:45 2017 -0800 Move resolveType __typename checking into defaultResolveType commit 1af7646053dd426d9fe43e10e6ecd3ab7c1905a8 Author: jonbretman <[email protected]> Date: Thu Jul 13 11:16:40 2017 +0100 Adds support for resolving union/interface types when using a generated schema
1 parent 714ee98 commit c650f30

16 files changed

+152
-167
lines changed

src/execution/execute.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,9 @@ function ensureValidRuntimeType(
10921092
throw new GraphQLError(
10931093
`Abstract type ${returnType.name} must resolve to an Object type at ` +
10941094
`runtime for field ${info.parentType.name}.${info.fieldName} with ` +
1095-
`value "${String(result)}", received "${String(runtimeType)}".`,
1095+
`value "${String(result)}", received "${String(runtimeType)}". ` +
1096+
`Either the ${returnType.name} type should provide a "resolveType" ` +
1097+
'function or each possible types should provide an "isTypeOf" function.',
10961098
fieldNodes
10971099
);
10981100
}
@@ -1197,15 +1199,30 @@ function collectAndExecuteSubfields(
11971199

11981200
/**
11991201
* If a resolveType function is not given, then a default resolve behavior is
1200-
* used which tests each possible type for the abstract type by calling
1202+
* used which attempts two strategies:
1203+
*
1204+
* First, See if the provided value has a `__typename` field defined, if so, use
1205+
* that value as name of the resolved type.
1206+
*
1207+
* Otherwise, test each possible type for the abstract type by calling
12011208
* isTypeOf for the object being coerced, returning the first type that matches.
12021209
*/
12031210
function defaultResolveTypeFn(
12041211
value: mixed,
12051212
context: mixed,
12061213
info: GraphQLResolveInfo,
12071214
abstractType: GraphQLAbstractType
1208-
): ?GraphQLObjectType | Promise<?GraphQLObjectType> {
1215+
): ?GraphQLObjectType | string | Promise<?GraphQLObjectType | string> {
1216+
// First, look for `__typename`.
1217+
if (
1218+
value !== null &&
1219+
typeof value === 'object' &&
1220+
typeof value.__typename === 'string'
1221+
) {
1222+
return value.__typename;
1223+
}
1224+
1225+
// Otherwise, test each possible type.
12091226
const possibleTypes = info.schema.getPossibleTypes(abstractType);
12101227
const promisedIsTypeOfResults = [];
12111228

src/type/__tests__/definition-test.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ const BlogSubscription = new GraphQLObjectType({
8989
}
9090
});
9191

92-
const ObjectType = new GraphQLObjectType({
93-
name: 'Object',
94-
isTypeOf: () => true
95-
});
92+
const ObjectType = new GraphQLObjectType({ name: 'Object' });
9693
const InterfaceType = new GraphQLInterfaceType({ name: 'Interface' });
9794
const UnionType =
9895
new GraphQLUnionType({ name: 'Union', types: [ ObjectType ] });
@@ -281,7 +278,6 @@ describe('Type System: Example', () => {
281278
f: { type: GraphQLInt }
282279
},
283280
interfaces: [ SomeInterface ],
284-
isTypeOf: () => true
285281
});
286282

287283
const schema = new GraphQLSchema({
@@ -311,7 +307,6 @@ describe('Type System: Example', () => {
311307
f: { type: GraphQLInt }
312308
},
313309
interfaces: () => [ SomeInterface ],
314-
isTypeOf: () => true
315310
});
316311

317312
const schema = new GraphQLSchema({

src/type/__tests__/schema-test.js

-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ import { expect } from 'chai';
1818
const InterfaceType = new GraphQLInterfaceType({
1919
name: 'Interface',
2020
fields: { fieldName: { type: GraphQLString } },
21-
resolveType() {
22-
return ImplementingType;
23-
}
2421
});
2522

2623
const ImplementingType = new GraphQLObjectType({

0 commit comments

Comments
 (0)