Skip to content

Commit 40e7806

Browse files
committed
Allow interfaces in GraphQLObjectType to be a thunk
1 parent 822dca8 commit 40e7806

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/type/__tests__/definition.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,26 @@ describe('Type System: Example', () => {
159159
expect(schema.getTypeMap().SomeSubtype).to.equal(SomeSubtype);
160160
});
161161

162+
it('includes interfaces\' thunk subtypes in the type map', () => {
163+
var SomeInterface = new GraphQLInterfaceType({
164+
name: 'SomeInterface',
165+
fields: {}
166+
});
167+
168+
var SomeSubtype = new GraphQLObjectType({
169+
name: 'SomeSubtype',
170+
fields: {},
171+
interfaces: () => [SomeInterface]
172+
});
173+
174+
var schema = new GraphQLSchema({
175+
query: SomeInterface
176+
});
177+
178+
expect(schema.getTypeMap().SomeSubtype).to.equal(SomeSubtype);
179+
});
180+
181+
162182
it('stringifies simple types', () => {
163183

164184
expect('' + GraphQLInt).to.equal('Int');

src/type/definition.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ export class GraphQLObjectType {
252252

253253
_typeConfig: GraphQLObjectTypeConfig;
254254
_fields: GraphQLFieldDefinitionMap;
255+
_interfaces: Array<GraphQLInterfaceType>;
255256

256257
constructor(config: GraphQLObjectTypeConfig) {
257258
invariant(config.name, 'Type must be named.');
@@ -267,7 +268,8 @@ export class GraphQLObjectType {
267268
}
268269

269270
getInterfaces(): Array<GraphQLInterfaceType> {
270-
return this._typeConfig.interfaces || [];
271+
return this._interfaces ||
272+
(this._interfaces = defineInterfaces(this._typeConfig.interfaces || []));
271273
}
272274

273275
isTypeOf(value: any): ?boolean {
@@ -282,10 +284,18 @@ export class GraphQLObjectType {
282284
}
283285
}
284286

287+
function resolveMaybeThunk<T>(thingOrThunk: T | () => T): T {
288+
return typeof thingOrThunk === 'function' ? thingOrThunk() : thingOrThunk;
289+
}
290+
291+
function defineInterfaces(interfacesOrThunk): Array<GraphQLInterfaceType> {
292+
return resolveMaybeThunk(interfacesOrThunk);
293+
}
294+
285295
function defineFieldMap(
286296
fields: GraphQLFieldConfigMap
287297
): GraphQLFieldDefinitionMap {
288-
var fieldMap: any = typeof fields === 'function' ? fields() : fields;
298+
var fieldMap: any = resolveMaybeThunk(fields);
289299
Object.keys(fieldMap).forEach(fieldName => {
290300
var field = fieldMap[fieldName];
291301
field.name = fieldName;

0 commit comments

Comments
 (0)