Skip to content

Commit b7ea845

Browse files
authored
Merge pull request #570 from josephsavona/type-assertion-helpers
Add assertXXXType helpers
2 parents 6375948 + e8b7167 commit b7ea845

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ export {
9090
__EnumValue,
9191
__TypeKind,
9292

93+
// Assertions
94+
assertType,
95+
assertInputType,
96+
assertOutputType,
97+
assertLeafType,
98+
assertCompositeType,
99+
assertAbstractType,
100+
93101
// Predicates
94102
isType,
95103
isInputType,

src/type/definition.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ export type GraphQLType =
3636
GraphQLList<any> |
3737
GraphQLNonNull<any>;
3838

39+
export function assertType(type: mixed): GraphQLType {
40+
invariant(
41+
isType(type),
42+
`GraphQL: Expected ${String(type)} to be a GraphQLType.`
43+
);
44+
return (type: any);
45+
}
46+
3947
export function isType(type: mixed): boolean {
4048
return (
4149
type instanceof GraphQLScalarType ||
@@ -64,6 +72,14 @@ export type GraphQLInputType =
6472
GraphQLList<GraphQLInputType>
6573
>;
6674

75+
export function assertInputType(type: ?GraphQLType): GraphQLInputType {
76+
invariant(
77+
isInputType(type),
78+
`GraphQL: Expected type ${String(type)} to be a GraphQLInputType.`
79+
);
80+
return (type: any);
81+
}
82+
6783
export function isInputType(type: ?GraphQLType): boolean {
6884
const namedType = getNamedType(type);
6985
return (
@@ -92,6 +108,14 @@ export type GraphQLOutputType =
92108
GraphQLList<GraphQLOutputType>
93109
>;
94110

111+
export function assertOutputType(type: ?GraphQLType): GraphQLOutputType {
112+
invariant(
113+
isOutputType(type),
114+
`GraphQL: Expected type ${String(type)} to be a GraphQLOutputType.`,
115+
);
116+
return (type: any);
117+
}
118+
95119
export function isOutputType(type: ?GraphQLType): boolean {
96120
const namedType = getNamedType(type);
97121
return (
@@ -110,6 +134,14 @@ export type GraphQLLeafType =
110134
GraphQLScalarType |
111135
GraphQLEnumType;
112136

137+
export function assertLeafType(type: ?GraphQLType): GraphQLLeafType {
138+
invariant(
139+
isLeafType(type),
140+
`GraphQL: Expected type ${String(type)} to be a GraphQLLeafType.`,
141+
);
142+
return (type: any);
143+
}
144+
113145
export function isLeafType(type: ?GraphQLType): boolean {
114146
const namedType = getNamedType(type);
115147
return (
@@ -126,6 +158,14 @@ export type GraphQLCompositeType =
126158
GraphQLInterfaceType |
127159
GraphQLUnionType;
128160

161+
export function assertCompositeType(type: ?GraphQLType): GraphQLCompositeType {
162+
invariant(
163+
isCompositeType(type),
164+
`GraphQL: Expected type ${String(type)} to be a GraphQLCompositeType.`,
165+
);
166+
return (type: any);
167+
}
168+
129169
export function isCompositeType(type: ?GraphQLType): boolean {
130170
return (
131171
type instanceof GraphQLObjectType ||
@@ -141,6 +181,14 @@ export type GraphQLAbstractType =
141181
GraphQLInterfaceType |
142182
GraphQLUnionType;
143183

184+
export function assertAbstractType(type: ?GraphQLType): GraphQLAbstractType {
185+
invariant(
186+
isAbstractType(type),
187+
`GraphQL: Expected type ${String(type)} to be a GraphQLAbstractType.`,
188+
);
189+
return (type: any);
190+
}
191+
144192
export function isAbstractType(type: ?GraphQLType): boolean {
145193
return (
146194
type instanceof GraphQLInterfaceType ||

src/type/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
export { GraphQLSchema } from './schema';
1313

1414
export {
15+
// Assertions
16+
assertType,
17+
assertInputType,
18+
assertOutputType,
19+
assertLeafType,
20+
assertCompositeType,
21+
assertAbstractType,
22+
1523
// Predicates
1624
isType,
1725
isInputType,

0 commit comments

Comments
 (0)