Skip to content

Commit 3124fa3

Browse files
Unify optional callbacks validation (#1411)
1 parent e124ca8 commit 3124fa3

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
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: 16 additions & 24 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+
config.isTypeOf == null || typeof config.isTypeOf === 'function',
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+
field.resolve == null || typeof field.resolve === 'function',
728727
`${config.name}.${fieldName} field resolver must be a function if ` +
729728
`provided, but got: ${inspect(field.resolve)}.`,
730729
);
@@ -757,11 +756,6 @@ 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 {
762-
return resolver == null || typeof resolver === 'function';
763-
}
764-
765759
export type GraphQLObjectTypeConfig<TSource, TContext> = {|
766760
name: string,
767761
interfaces?: Thunk<?Array<GraphQLInterfaceType>>,
@@ -903,12 +897,11 @@ export class GraphQLInterfaceType {
903897
this.resolveType = config.resolveType;
904898
this._fields = defineFieldMap.bind(undefined, config);
905899
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-
}
900+
invariant(
901+
config.resolveType == null || typeof config.resolveType === 'function',
902+
`${this.name} must provide "resolveType" as a function, ` +
903+
`but got: ${inspect(config.resolveType)}.`,
904+
);
912905
}
913906
914907
getFields(): GraphQLFieldMap<*, *> {
@@ -981,12 +974,11 @@ export class GraphQLUnionType {
981974
this.resolveType = config.resolveType;
982975
this._types = defineTypes.bind(undefined, config);
983976
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-
}
977+
invariant(
978+
config.resolveType == null || typeof config.resolveType === 'function',
979+
`${this.name} must provide "resolveType" as a function, ` +
980+
`but got: ${inspect(config.resolveType)}.`,
981+
);
990982
}
991983
992984
getTypes(): Array<GraphQLObjectType> {

0 commit comments

Comments
 (0)