Skip to content

Commit dc89262

Browse files
resolveType: remove support for returning GraphQLObjectType (#2905)
1 parent c0ff68e commit dc89262

File tree

4 files changed

+18
-95
lines changed

4 files changed

+18
-95
lines changed

src/execution/__tests__/abstract-test.js

Lines changed: 6 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { expect } from 'chai';
22
import { describe, it } from 'mocha';
33

4-
import invariant from '../../jsutils/invariant';
5-
64
import { parse } from '../../language/parser';
75

86
import { GraphQLSchema } from '../../type/schema';
@@ -297,89 +295,6 @@ describe('Execute: Handles execution of abstract types', () => {
297295
});
298296
});
299297

300-
it('deprecated(will be removed in v16.0.0): resolveType allows resolving with type object', async () => {
301-
const PetType = new GraphQLInterfaceType({
302-
name: 'Pet',
303-
resolveType(obj, context) {
304-
if (obj instanceof Dog) {
305-
return context.async ? Promise.resolve(DogType) : DogType;
306-
}
307-
// istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
308-
if (obj instanceof Cat) {
309-
return context.async ? Promise.resolve(CatType) : CatType;
310-
}
311-
312-
// istanbul ignore next (Not reachable. All possible types have been considered)
313-
invariant(false);
314-
},
315-
fields: {
316-
name: { type: GraphQLString },
317-
},
318-
});
319-
320-
const DogType = new GraphQLObjectType({
321-
name: 'Dog',
322-
interfaces: [PetType],
323-
fields: {
324-
name: { type: GraphQLString },
325-
woofs: { type: GraphQLBoolean },
326-
},
327-
});
328-
329-
const CatType = new GraphQLObjectType({
330-
name: 'Cat',
331-
interfaces: [PetType],
332-
fields: {
333-
name: { type: GraphQLString },
334-
meows: { type: GraphQLBoolean },
335-
},
336-
});
337-
338-
const schema = new GraphQLSchema({
339-
query: new GraphQLObjectType({
340-
name: 'Query',
341-
fields: {
342-
pets: {
343-
type: new GraphQLList(PetType),
344-
resolve() {
345-
return [new Dog('Odie', true), new Cat('Garfield', false)];
346-
},
347-
},
348-
},
349-
}),
350-
types: [CatType, DogType],
351-
});
352-
353-
const query = `
354-
{
355-
pets {
356-
name
357-
... on Dog {
358-
woofs
359-
}
360-
... on Cat {
361-
meows
362-
}
363-
}
364-
}
365-
`;
366-
367-
expect(await executeQuery({ schema, query })).to.deep.equal({
368-
data: {
369-
pets: [
370-
{
371-
name: 'Odie',
372-
woofs: true,
373-
},
374-
{
375-
name: 'Garfield',
376-
meows: false,
377-
},
378-
],
379-
},
380-
});
381-
});
382-
383298
it('resolveType can throw', async () => {
384299
const PetType = new GraphQLInterfaceType({
385300
name: 'Pet',
@@ -658,5 +573,11 @@ describe('Execute: Handles execution of abstract types', () => {
658573
expectError({ forTypeName: undefined }).toEqual(
659574
'Abstract type "Pet" must resolve to an Object type at runtime for field "Query.pet" with value { __typename: undefined }, received "[]".',
660575
);
576+
577+
// FIXME: workaround since we can't inject resolveType into SDL
578+
(schema.getType('Pet'): any).resolveType = () => schema.getType('Cat');
579+
expectError({ forTypeName: undefined }).toEqual(
580+
'Support for returning GraphQLObjectType from resolveType was removed in [email protected] please return type name instead.',
581+
);
661582
});
662583
});

src/execution/execute.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import {
5252
GraphQLSkipDirective,
5353
} from '../type/directives';
5454
import {
55-
isNamedType,
5655
isObjectType,
5756
isAbstractType,
5857
isLeafType,
@@ -954,29 +953,32 @@ function completeAbstractValue(
954953
}
955954

956955
function ensureValidRuntimeType(
957-
runtimeTypeOrName: mixed,
956+
runtimeTypeName: mixed,
958957
exeContext: ExecutionContext,
959958
returnType: GraphQLAbstractType,
960959
fieldNodes: $ReadOnlyArray<FieldNode>,
961960
info: GraphQLResolveInfo,
962961
result: mixed,
963962
): GraphQLObjectType {
964-
if (runtimeTypeOrName == null) {
963+
if (runtimeTypeName == null) {
965964
throw new GraphQLError(
966965
`Abstract type "${returnType.name}" must resolve to an Object type at runtime for field "${info.parentType.name}.${info.fieldName}". Either the "${returnType.name}" type should provide a "resolveType" function or each possible type should provide an "isTypeOf" function.`,
967966
fieldNodes,
968967
);
969968
}
970969

971-
// FIXME: temporary workaround until support for passing object types would be removed in v16.0.0
972-
const runtimeTypeName = isNamedType(runtimeTypeOrName)
973-
? runtimeTypeOrName.name
974-
: runtimeTypeOrName;
970+
// releases before 16.0.0 supported returning `GraphQLObjectType` from `resolveType`
971+
// TODO: remove in 17.0.0 release
972+
if (isObjectType(runtimeTypeName)) {
973+
throw new GraphQLError(
974+
'Support for returning GraphQLObjectType from resolveType was removed in [email protected] please return type name instead.',
975+
);
976+
}
975977

976978
if (typeof runtimeTypeName !== 'string') {
977979
throw new GraphQLError(
978980
`Abstract type "${returnType.name}" must resolve to an Object type at runtime for field "${info.parentType.name}.${info.fieldName}" with ` +
979-
`value ${inspect(result)}, received "${inspect(runtimeTypeOrName)}".`,
981+
`value ${inspect(result)}, received "${inspect(runtimeTypeName)}".`,
980982
);
981983
}
982984

src/type/definition.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ export type GraphQLTypeResolver<TSource, TContext> = (
456456
context: TContext,
457457
info: GraphQLResolveInfo,
458458
abstractType: GraphQLAbstractType,
459-
) => PromiseOrValue<Maybe<GraphQLObjectType<TSource, TContext> | string>>;
459+
) => PromiseOrValue<string | undefined>;
460460

461461
export type GraphQLIsTypeOfFn<TSource, TContext> = (
462462
source: TSource,

src/type/definition.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ export type GraphQLTypeResolver<TSource, TContext> = (
931931
context: TContext,
932932
info: GraphQLResolveInfo,
933933
abstractType: GraphQLAbstractType,
934-
) => PromiseOrValue<?GraphQLObjectType | string>;
934+
) => PromiseOrValue<string | void>;
935935

936936
export type GraphQLIsTypeOfFn<TSource, TContext> = (
937937
source: TSource,

0 commit comments

Comments
 (0)