Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow resolveType to return a type name string #509

Merged
merged 1 commit into from
Oct 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/execution/__tests__/abstract-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,79 @@ describe('Execute: Handles execution of abstract types', () => {
});
});

it('resolveType allows resolving with type name', async () => {
const PetType = new GraphQLInterfaceType({
name: 'Pet',
resolveType(obj) {
return obj instanceof Dog ? 'Dog' :
obj instanceof Cat ? 'Cat' :
null;
},
fields: {
name: { type: GraphQLString }
}
});

const DogType = new GraphQLObjectType({
name: 'Dog',
interfaces: [ PetType ],
fields: {
name: { type: GraphQLString },
woofs: { type: GraphQLBoolean },
}
});

const CatType = new GraphQLObjectType({
name: 'Cat',
interfaces: [ PetType ],
fields: {
name: { type: GraphQLString },
meows: { type: GraphQLBoolean },
}
});

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
pets: {
type: new GraphQLList(PetType),
resolve() {
return [
new Dog('Odie', true),
new Cat('Garfield', false)
];
}
}
}
}),
types: [ CatType, DogType ]
});

const query = `{
pets {
name
... on Dog {
woofs
}
... on Cat {
meows
}
}
}`;

const result = await graphql(schema, query);

expect(result).to.jsonEqual({
data: {
pets: [
{ name: 'Odie',
woofs: true },
{ name: 'Garfield',
meows: false },
]
}
});
});

});
7 changes: 6 additions & 1 deletion src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -903,10 +903,15 @@ function completeAbstractValue(
path: Array<string | number>,
result: mixed
): mixed {
const runtimeType = returnType.resolveType ?
let runtimeType = returnType.resolveType ?
returnType.resolveType(result, exeContext.contextValue, info) :
defaultResolveTypeFn(result, exeContext.contextValue, info, returnType);

// If resolveType returns a string, we assume it's a GraphQLObjectType name.
if (typeof runtimeType === 'string') {
runtimeType = exeContext.schema.getType(runtimeType);
}

if (!(runtimeType instanceof GraphQLObjectType)) {
throw new GraphQLError(
`Abstract type ${returnType.name} must resolve to an Object type at ` +
Expand Down
2 changes: 1 addition & 1 deletion src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ export type GraphQLTypeResolveFn = (
value: mixed,
context: mixed,
info: GraphQLResolveInfo
) => ?GraphQLObjectType;
) => ?GraphQLObjectType | ?string;

export type GraphQLIsTypeOfFn = (
source: mixed,
Expand Down