Skip to content

Commit 85d98f3

Browse files
committed
Unify optional callbacks validation
1 parent f373fed commit 85d98f3

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

src/type/__tests__/definition-test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,9 @@ describe('Type System: Interface types must be resolvable', () => {
730730
resolveType: {},
731731
fields: { f: { type: GraphQLString } },
732732
}),
733-
).to.throw('AnotherInterface must provide "resolveType" as a function.');
733+
).to.throw(
734+
'AnotherInterface must provide "resolveType" as a function, but got: {}.',
735+
);
734736
});
735737
});
736738

@@ -782,7 +784,9 @@ describe('Type System: Union types must be resolvable', () => {
782784
types: [ObjectWithIsTypeOf],
783785
}),
784786
),
785-
).to.throw('SomeUnion must provide "resolveType" as a function.');
787+
).to.throw(
788+
'SomeUnion must provide "resolveType" as a function, but got: {}.',
789+
);
786790
});
787791
});
788792

@@ -905,7 +909,9 @@ describe('Type System: Object types must be assertable', () => {
905909
fields: { f: { type: GraphQLString } },
906910
}),
907911
);
908-
}).to.throw('AnotherObject must provide "isTypeOf" as a function.');
912+
}).to.throw(
913+
'AnotherObject must provide "isTypeOf" as a function, but got: {}.',
914+
);
909915
});
910916
});
911917

src/type/definition.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -651,12 +651,11 @@ export class GraphQLObjectType {
651651
this._fields = defineFieldMap.bind(undefined, config);
652652
this._interfaces = defineInterfaces.bind(undefined, config);
653653
invariant(typeof config.name === 'string', 'Must provide name.');
654-
if (config.isTypeOf) {
655-
invariant(
656-
typeof config.isTypeOf === 'function',
657-
`${this.name} must provide "isTypeOf" as a function.`,
658-
);
659-
}
654+
invariant(
655+
isMaybeFunction(config.isTypeOf),
656+
`${this.name} must provide "isTypeOf" as a function, ` +
657+
`but got: ${inspect(config.isTypeOf)}.`,
658+
);
660659
}
661660

662661
getFields(): GraphQLFieldMap<*, *> {
@@ -724,7 +723,7 @@ function defineFieldMap<TSource, TContext>(
724723
name: fieldName,
725724
};
726725
invariant(
727-
isValidResolver(field.resolve),
726+
isMaybeFunction(field.resolve),
728727
`${config.name}.${fieldName} field resolver must be a function if ` +
729728
`provided, but got: ${inspect(field.resolve)}.`,
730729
);
@@ -757,8 +756,8 @@ function isPlainObj(obj) {
757756
return obj && typeof obj === 'object' && !Array.isArray(obj);
758757
}
759758

760-
// If a resolver is defined, it must be a function.
761-
function isValidResolver(resolver: mixed): boolean {
759+
// If a value is defined, it must be a function.
760+
function isMaybeFunction(resolver: mixed): boolean {
762761
return resolver == null || typeof resolver === 'function';
763762
}
764763

@@ -903,12 +902,11 @@ export class GraphQLInterfaceType {
903902
this.resolveType = config.resolveType;
904903
this._fields = defineFieldMap.bind(undefined, config);
905904
invariant(typeof config.name === 'string', 'Must provide name.');
906-
if (config.resolveType) {
907-
invariant(
908-
typeof config.resolveType === 'function',
909-
`${this.name} must provide "resolveType" as a function.`,
910-
);
911-
}
905+
invariant(
906+
isMaybeFunction(config.resolveType),
907+
`${this.name} must provide "resolveType" as a function, ` +
908+
`but got: ${inspect(config.resolveType)}.`,
909+
);
912910
}
913911

914912
getFields(): GraphQLFieldMap<*, *> {
@@ -981,12 +979,11 @@ export class GraphQLUnionType {
981979
this.resolveType = config.resolveType;
982980
this._types = defineTypes.bind(undefined, config);
983981
invariant(typeof config.name === 'string', 'Must provide name.');
984-
if (config.resolveType) {
985-
invariant(
986-
typeof config.resolveType === 'function',
987-
`${this.name} must provide "resolveType" as a function.`,
988-
);
989-
}
982+
invariant(
983+
isMaybeFunction(config.resolveType),
984+
`${this.name} must provide "resolveType" as a function, ` +
985+
`but got: ${inspect(config.resolveType)}.`,
986+
);
990987
}
991988

992989
getTypes(): Array<GraphQLObjectType> {

0 commit comments

Comments
 (0)