Skip to content

Commit c82ff68

Browse files
tgriesserleebyron
authored andcommitted
Allow resolveType to return a type name string (#509)
1 parent f379d69 commit c82ff68

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

src/execution/__tests__/abstract-test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,79 @@ describe('Execute: Handles execution of abstract types', () => {
353353
});
354354
});
355355

356+
it('resolveType allows resolving with type name', async () => {
357+
const PetType = new GraphQLInterfaceType({
358+
name: 'Pet',
359+
resolveType(obj) {
360+
return obj instanceof Dog ? 'Dog' :
361+
obj instanceof Cat ? 'Cat' :
362+
null;
363+
},
364+
fields: {
365+
name: { type: GraphQLString }
366+
}
367+
});
368+
369+
const DogType = new GraphQLObjectType({
370+
name: 'Dog',
371+
interfaces: [ PetType ],
372+
fields: {
373+
name: { type: GraphQLString },
374+
woofs: { type: GraphQLBoolean },
375+
}
376+
});
377+
378+
const CatType = new GraphQLObjectType({
379+
name: 'Cat',
380+
interfaces: [ PetType ],
381+
fields: {
382+
name: { type: GraphQLString },
383+
meows: { type: GraphQLBoolean },
384+
}
385+
});
386+
387+
const schema = new GraphQLSchema({
388+
query: new GraphQLObjectType({
389+
name: 'Query',
390+
fields: {
391+
pets: {
392+
type: new GraphQLList(PetType),
393+
resolve() {
394+
return [
395+
new Dog('Odie', true),
396+
new Cat('Garfield', false)
397+
];
398+
}
399+
}
400+
}
401+
}),
402+
types: [ CatType, DogType ]
403+
});
404+
405+
const query = `{
406+
pets {
407+
name
408+
... on Dog {
409+
woofs
410+
}
411+
... on Cat {
412+
meows
413+
}
414+
}
415+
}`;
416+
417+
const result = await graphql(schema, query);
418+
419+
expect(result).to.jsonEqual({
420+
data: {
421+
pets: [
422+
{ name: 'Odie',
423+
woofs: true },
424+
{ name: 'Garfield',
425+
meows: false },
426+
]
427+
}
428+
});
429+
});
430+
356431
});

src/execution/execute.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,10 +903,15 @@ function completeAbstractValue(
903903
path: Array<string | number>,
904904
result: mixed
905905
): mixed {
906-
const runtimeType = returnType.resolveType ?
906+
let runtimeType = returnType.resolveType ?
907907
returnType.resolveType(result, exeContext.contextValue, info) :
908908
defaultResolveTypeFn(result, exeContext.contextValue, info, returnType);
909909

910+
// If resolveType returns a string, we assume it's a GraphQLObjectType name.
911+
if (typeof runtimeType === 'string') {
912+
runtimeType = exeContext.schema.getType(runtimeType);
913+
}
914+
910915
if (!(runtimeType instanceof GraphQLObjectType)) {
911916
throw new GraphQLError(
912917
`Abstract type ${returnType.name} must resolve to an Object type at ` +

src/type/definition.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ export type GraphQLTypeResolveFn = (
471471
value: mixed,
472472
context: mixed,
473473
info: GraphQLResolveInfo
474-
) => ?GraphQLObjectType;
474+
) => ?GraphQLObjectType | ?string;
475475

476476
export type GraphQLIsTypeOfFn = (
477477
source: mixed,

0 commit comments

Comments
 (0)