diff --git a/src/type/__tests__/definition-test.js b/src/type/__tests__/definition-test.js index ec27aaf94a..86489c0942 100644 --- a/src/type/__tests__/definition-test.js +++ b/src/type/__tests__/definition-test.js @@ -730,7 +730,9 @@ describe('Type System: Interface types must be resolvable', () => { resolveType: {}, fields: { f: { type: GraphQLString } }, }), - ).to.throw('AnotherInterface must provide "resolveType" as a function.'); + ).to.throw( + 'AnotherInterface must provide "resolveType" as a function, but got: {}.', + ); }); }); @@ -782,7 +784,9 @@ describe('Type System: Union types must be resolvable', () => { types: [ObjectWithIsTypeOf], }), ), - ).to.throw('SomeUnion must provide "resolveType" as a function.'); + ).to.throw( + 'SomeUnion must provide "resolveType" as a function, but got: {}.', + ); }); }); @@ -905,7 +909,9 @@ describe('Type System: Object types must be assertable', () => { fields: { f: { type: GraphQLString } }, }), ); - }).to.throw('AnotherObject must provide "isTypeOf" as a function.'); + }).to.throw( + 'AnotherObject must provide "isTypeOf" as a function, but got: {}.', + ); }); }); diff --git a/src/type/definition.js b/src/type/definition.js index d588a565d1..8507c387d6 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -651,12 +651,11 @@ export class GraphQLObjectType { this._fields = defineFieldMap.bind(undefined, config); this._interfaces = defineInterfaces.bind(undefined, config); invariant(typeof config.name === 'string', 'Must provide name.'); - if (config.isTypeOf) { - invariant( - typeof config.isTypeOf === 'function', - `${this.name} must provide "isTypeOf" as a function.`, - ); - } + invariant( + config.isTypeOf == null || typeof config.isTypeOf === 'function', + `${this.name} must provide "isTypeOf" as a function, ` + + `but got: ${inspect(config.isTypeOf)}.`, + ); } getFields(): GraphQLFieldMap<*, *> { @@ -724,7 +723,7 @@ function defineFieldMap( name: fieldName, }; invariant( - isValidResolver(field.resolve), + field.resolve == null || typeof field.resolve === 'function', `${config.name}.${fieldName} field resolver must be a function if ` + `provided, but got: ${inspect(field.resolve)}.`, ); @@ -757,11 +756,6 @@ function isPlainObj(obj) { return obj && typeof obj === 'object' && !Array.isArray(obj); } -// If a resolver is defined, it must be a function. -function isValidResolver(resolver: mixed): boolean { - return resolver == null || typeof resolver === 'function'; -} - export type GraphQLObjectTypeConfig = {| name: string, interfaces?: Thunk>, @@ -903,12 +897,11 @@ export class GraphQLInterfaceType { this.resolveType = config.resolveType; this._fields = defineFieldMap.bind(undefined, config); invariant(typeof config.name === 'string', 'Must provide name.'); - if (config.resolveType) { - invariant( - typeof config.resolveType === 'function', - `${this.name} must provide "resolveType" as a function.`, - ); - } + invariant( + config.resolveType == null || typeof config.resolveType === 'function', + `${this.name} must provide "resolveType" as a function, ` + + `but got: ${inspect(config.resolveType)}.`, + ); } getFields(): GraphQLFieldMap<*, *> { @@ -981,12 +974,11 @@ export class GraphQLUnionType { this.resolveType = config.resolveType; this._types = defineTypes.bind(undefined, config); invariant(typeof config.name === 'string', 'Must provide name.'); - if (config.resolveType) { - invariant( - typeof config.resolveType === 'function', - `${this.name} must provide "resolveType" as a function.`, - ); - } + invariant( + config.resolveType == null || typeof config.resolveType === 'function', + `${this.name} must provide "resolveType" as a function, ` + + `but got: ${inspect(config.resolveType)}.`, + ); } getTypes(): Array {