forked from graphql/graphql-js
/
definition.ts
165 lines (146 loc) · 4.67 KB
/
definition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import type {
GraphQLAbstractType,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLInputType,
GraphQLInterfaceType,
GraphQLLeafType,
GraphQLList,
GraphQLNamedType,
GraphQLNonNull,
GraphQLNullableType,
GraphQLObjectType,
GraphQLOutputType,
GraphQLScalarType,
GraphQLType,
GraphQLUnionType,
GraphQLWrappingType,
} from 'graphql';
import { memoize1 } from '../jsutils/memoize1';
/**
* There are predicates for each kind of GraphQL type.
*/
function _isScalarType(type: unknown) {
return Object.prototype.toString.call(type) === '[object GraphQLScalarType]';
}
export const isScalarType = memoize1(_isScalarType) as (type: {
[key: string]: any;
}) => type is GraphQLScalarType;
function _isObjectType(type: unknown) {
return Object.prototype.toString.call(type) === '[object GraphQLObjectType]';
}
export const isObjectType = memoize1(_isObjectType) as (type: {
[key: string]: any;
}) => type is GraphQLObjectType;
function _isInterfaceType(type: unknown) {
return (
Object.prototype.toString.call(type) === '[object GraphQLInterfaceType]'
);
}
export const isInterfaceType = memoize1(_isInterfaceType) as (type: {
[key: string]: any;
}) => type is GraphQLInterfaceType;
function _isUnionType(type: unknown) {
return Object.prototype.toString.call(type) === '[object GraphQLUnionType]';
}
export const isUnionType = memoize1(_isUnionType) as (type: {
[key: string]: any;
}) => type is GraphQLUnionType;
function _isEnumType(type: unknown) {
return Object.prototype.toString.call(type) === '[object GraphQLEnumType]';
}
export const isEnumType = memoize1(_isEnumType) as (type: {
[key: string]: any;
}) => type is GraphQLEnumType;
function _isInputObjectType(type: unknown): type is GraphQLInputObjectType {
return (
Object.prototype.toString.call(type) === '[object GraphQLInputObjectType]'
);
}
export const isInputObjectType = memoize1(_isInputObjectType) as (type: {
[key: string]: any;
}) => type is GraphQLInputObjectType;
function _isListType(
type: GraphQLInputType,
): type is GraphQLList<GraphQLInputType>;
function _isListType(
type: GraphQLOutputType,
): type is GraphQLList<GraphQLOutputType>;
function _isListType(type: {
[key: string]: any;
}): type is GraphQLList<GraphQLType>;
function _isListType(type: { [key: string]: any }) {
return Object.prototype.toString.call(type) === '[object GraphQLList]';
}
export const isListType = memoize1(_isListType) as (type: {
[key: string]: any;
}) => type is GraphQLList<any>;
export type GraphQLNullableInputType =
| GraphQLScalarType
| GraphQLEnumType
| GraphQLInputObjectType
| GraphQLList<GraphQLInputType>;
export type GraphQLNullableOutputType =
| GraphQLScalarType
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType
| GraphQLEnumType
| GraphQLList<GraphQLOutputType>;
function _isNonNullType(
type: GraphQLInputType,
): type is GraphQLNonNull<GraphQLNullableInputType>;
function _isNonNullType(
type: GraphQLOutputType,
): type is GraphQLNonNull<GraphQLNullableOutputType>;
function _isNonNullType(type: {
[key: string]: any;
}): type is GraphQLNonNull<GraphQLNullableType>;
function _isNonNullType(type: { [key: string]: any }) {
return Object.prototype.toString.call(type) === '[object GraphQLNonNull]';
}
export const isNonNullType = memoize1(_isNonNullType) as (type: {
[key: string]: any;
}) => type is GraphQLNonNull<any>;
function _isInputType(type: { [key: string]: any }) {
return (
isScalarType(type) ||
isEnumType(type) ||
isInputObjectType(type) ||
(isWrappingType(type) && isInputType(type.ofType))
);
}
export const isInputType = memoize1(_isInputType) as (type: {
[key: string]: any;
}) => type is GraphQLInputType;
function _isLeafType(type: { [key: string]: any }) {
return isScalarType(type) || isEnumType(type);
}
export const isLeafType = memoize1(_isLeafType) as (type: {
[key: string]: any;
}) => type is GraphQLLeafType;
function _isAbstractType(type: { [key: string]: any }) {
return isInterfaceType(type) || isUnionType(type);
}
export const isAbstractType = memoize1(_isAbstractType) as (type: {
[key: string]: any;
}) => type is GraphQLAbstractType;
function _isWrappingType(type: { [key: string]: any }) {
return isListType(type) || isNonNullType(type);
}
export const isWrappingType = memoize1(_isWrappingType) as (type: {
[key: string]: any;
}) => type is GraphQLWrappingType;
function _isNamedType(type: { [key: string]: any }) {
return (
isScalarType(type) ||
isObjectType(type) ||
isInterfaceType(type) ||
isUnionType(type) ||
isEnumType(type) ||
isInputObjectType(type)
);
}
export const isNamedType = memoize1(_isNamedType) as (type: {
[key: string]: any;
}) => type is GraphQLNamedType;