Skip to content

Commit 8c8be29

Browse files
committed
Unify optional callbacks validation
1 parent 4bccc3f commit 8c8be29

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
@@ -661,12 +661,11 @@ export class GraphQLObjectType {
661661
this.isTypeOf = config.isTypeOf;
662662
this._typeConfig = config;
663663
invariant(typeof config.name === 'string', 'Must provide name.');
664-
if (config.isTypeOf) {
665-
invariant(
666-
typeof config.isTypeOf === 'function',
667-
`${this.name} must provide "isTypeOf" as a function.`,
668-
);
669-
}
664+
invariant(
665+
isMaybeFunction(config.isTypeOf),
666+
`${this.name} must provide "isTypeOf" as a function, ` +
667+
`but got: ${inspect(config.isTypeOf)}.`,
668+
);
670669
}
671670

672671
getFields(): GraphQLFieldMap<*, *> {
@@ -734,7 +733,7 @@ function defineFieldMap<TSource, TContext>(
734733
name: fieldName,
735734
};
736735
invariant(
737-
isValidResolver(field.resolve),
736+
isMaybeFunction(field.resolve),
738737
`${type.name}.${fieldName} field resolver must be a function if ` +
739738
`provided, but got: ${inspect(field.resolve)}.`,
740739
);
@@ -767,8 +766,8 @@ function isPlainObj(obj) {
767766
return obj && typeof obj === 'object' && !Array.isArray(obj);
768767
}
769768

770-
// If a resolver is defined, it must be a function.
771-
function isValidResolver(resolver: mixed): boolean {
769+
// If a value is defined, it must be a function.
770+
function isMaybeFunction(resolver: mixed): boolean {
772771
return resolver == null || typeof resolver === 'function';
773772
}
774773

@@ -914,12 +913,11 @@ export class GraphQLInterfaceType {
914913
this.resolveType = config.resolveType;
915914
this._typeConfig = config;
916915
invariant(typeof config.name === 'string', 'Must provide name.');
917-
if (config.resolveType) {
918-
invariant(
919-
typeof config.resolveType === 'function',
920-
`${this.name} must provide "resolveType" as a function.`,
921-
);
922-
}
916+
invariant(
917+
isMaybeFunction(config.resolveType),
918+
`${this.name} must provide "resolveType" as a function, ` +
919+
`but got: ${inspect(config.resolveType)}.`,
920+
);
923921
}
924922

925923
getFields(): GraphQLFieldMap<*, *> {
@@ -993,12 +991,11 @@ export class GraphQLUnionType {
993991
this.resolveType = config.resolveType;
994992
this._typeConfig = config;
995993
invariant(typeof config.name === 'string', 'Must provide name.');
996-
if (config.resolveType) {
997-
invariant(
998-
typeof config.resolveType === 'function',
999-
`${this.name} must provide "resolveType" as a function.`,
1000-
);
1001-
}
994+
invariant(
995+
isMaybeFunction(config.resolveType),
996+
`${this.name} must provide "resolveType" as a function, ` +
997+
`but got: ${inspect(config.resolveType)}.`,
998+
);
1002999
}
10031000

10041001
getTypes(): Array<GraphQLObjectType> {

0 commit comments

Comments
 (0)