-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
GraphQLSchemaUtils.js
253 lines (231 loc) · 6.11 KB
/
GraphQLSchemaUtils.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule GraphQLSchemaUtils
* @flow
* @format
*/
'use strict';
const invariant = require('invariant');
const nullthrows = require('../util/nullthrowsOSS');
const {
assertAbstractType,
getNamedType,
getNullableType,
isType,
print,
typeFromAST,
GraphQLInterfaceType,
GraphQLList,
GraphQLObjectType,
GraphQLSchema,
GraphQLUnionType,
} = require('graphql');
import type {
ASTNode,
GraphQLCompositeType,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLNamedType,
GraphQLNonNull,
GraphQLScalarType,
GraphQLType,
TypeNode,
} from 'graphql';
const ID = 'id';
const ID_TYPE = 'ID';
type GraphQLSingularType =
| GraphQLScalarType
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType
| GraphQLEnumType
| GraphQLInputObjectType
| GraphQLNonNull<*>;
/**
* Determine if the given type may implement the named type:
* - it is the named type
* - it implements the named interface
* - it is an abstract type and *some* of its concrete types may
* implement the named type
*/
function mayImplement(
schema: GraphQLSchema,
type: GraphQLType,
typeName: string,
): boolean {
const unmodifiedType = getRawType(type);
return (
unmodifiedType.toString() === typeName ||
implementsInterface(unmodifiedType, typeName) ||
(isAbstractType(unmodifiedType) &&
hasConcreteTypeThatImplements(schema, unmodifiedType, typeName))
);
}
function canHaveSelections(type: GraphQLType): boolean {
return (
type instanceof GraphQLObjectType || type instanceof GraphQLInterfaceType
);
}
/**
* Implements duck typing that checks whether a type has an id field of the ID
* type. This is approximating what we can hopefully do with the __id proposal
* a bit more cleanly.
*
* https://github.com/graphql/graphql-future/blob/master/01%20-%20__id.md
*/
function hasID(schema: GraphQLSchema, type: GraphQLCompositeType): boolean {
const unmodifiedType = getRawType(type);
invariant(
unmodifiedType instanceof GraphQLObjectType ||
unmodifiedType instanceof GraphQLInterfaceType,
'GraphQLSchemaUtils.hasID(): Expected a concrete type or interface, ' +
'got type `%s`.',
type,
);
const idType = schema.getType(ID_TYPE);
const idField = unmodifiedType.getFields()[ID];
return idField && getRawType(idField.type) === idType;
}
/**
* Determine if a type is abstract (not concrete).
*
* Note: This is used in place of the `graphql` version of the function in order
* to not break `instanceof` checks with Jest. This version also unwraps
* non-null/list wrapper types.
*/
function isAbstractType(type: GraphQLType): boolean {
const rawType = getRawType(type);
return (
rawType instanceof GraphQLInterfaceType ||
rawType instanceof GraphQLUnionType
);
}
function isUnionType(type: GraphQLType): boolean %checks {
return type instanceof GraphQLUnionType;
}
/**
* Get the unmodified type, with list/null wrappers removed.
*/
function getRawType(type: GraphQLType): GraphQLNamedType {
return nullthrows(getNamedType(type));
}
/**
* Gets the non-list type, removing the list wrapper if present.
*/
function getSingularType(type: GraphQLType): GraphQLSingularType {
let unmodifiedType = type;
while (unmodifiedType instanceof GraphQLList) {
unmodifiedType = unmodifiedType.ofType;
}
return (unmodifiedType: any);
}
/**
* @public
*/
function implementsInterface(
type: GraphQLType,
interfaceName: string,
): boolean {
return getInterfaces(type).some(
interfaceType => interfaceType.toString() === interfaceName,
);
}
/**
* @private
*/
function hasConcreteTypeThatImplements(
schema: GraphQLSchema,
type: GraphQLType,
interfaceName: string,
): boolean {
return (
isAbstractType(type) &&
getConcreteTypes(schema, type).some(concreteType =>
implementsInterface(concreteType, interfaceName),
)
);
}
/**
* @private
*/
function getConcreteTypes(
schema: GraphQLSchema,
type: GraphQLType,
): Array<GraphQLObjectType> {
return schema.getPossibleTypes(assertAbstractType(type));
}
/**
* @private
*/
function getInterfaces(type: GraphQLType): Array<GraphQLInterfaceType> {
if (type instanceof GraphQLObjectType) {
return type.getInterfaces();
}
return [];
}
/**
* @public
*
* Determine if an AST node contains a fragment/operation definition.
*/
function isOperationDefinitionAST(ast: ASTNode): boolean %checks {
return (
ast.kind === 'FragmentDefinition' || ast.kind === 'OperationDefinition'
);
}
/**
* @public
*
* Determine if an AST node contains a schema definition.
*/
function isSchemaDefinitionAST(ast: ASTNode): boolean %checks {
return (
ast.kind === 'DirectiveDefinition' ||
ast.kind === 'EnumTypeDefinition' ||
ast.kind === 'InputObjectTypeDefinition' ||
ast.kind === 'InterfaceTypeDefinition' ||
ast.kind === 'ObjectTypeDefinition' ||
ast.kind === 'ScalarTypeDefinition' ||
ast.kind === 'TypeExtensionDefinition' ||
ast.kind === 'UnionTypeDefinition'
);
}
function assertTypeWithFields(
type: ?GraphQLType,
): GraphQLObjectType | GraphQLInterfaceType {
invariant(
type instanceof GraphQLObjectType || type instanceof GraphQLInterfaceType,
'GraphQLSchemaUtils: Expected type `%s` to be an object or interface type.',
type,
);
return type;
}
/**
* Helper for calling `typeFromAST()` with a clear warning when the type does
* not exist. This enables the pattern `assertXXXType(getTypeFromAST(...))`,
* emitting distinct errors for unknown types vs types of the wrong category.
*/
function getTypeFromAST(schema: GraphQLSchema, ast: TypeNode): GraphQLType {
const type = typeFromAST(schema, ast);
invariant(isType(type), 'GraphQLSchemaUtils: Unknown type `%s`.', print(ast));
return (type: any);
}
module.exports = {
assertTypeWithFields,
canHaveSelections,
getNullableType,
getRawType,
getSingularType,
getTypeFromAST,
hasID,
implementsInterface,
isAbstractType,
isUnionType,
isOperationDefinitionAST,
isSchemaDefinitionAST,
mayImplement,
};