-
-
Notifications
You must be signed in to change notification settings - Fork 185
/
graphql.ts
272 lines (229 loc) · 7.06 KB
/
graphql.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import {
GraphQLInputType,
isWrappingType,
isListType,
isNonNullType,
GraphQLOutputType,
GraphQLNamedType,
KindEnum,
Kind,
GraphQLSchema,
GraphQLError,
DocumentNode,
TypeInfo,
visit,
visitWithTypeInfo,
getNamedType,
FieldNode,
isInterfaceType,
isScalarType,
isObjectType,
isUnionType,
isInputObjectType,
GraphQLScalarType,
GraphQLInterfaceType,
GraphQLObjectType,
GraphQLUnionType,
GraphQLEnumType,
GraphQLInputObjectType,
} from 'graphql';
import { isDeprecated } from './isDeprecated';
export function safeChangeForField(oldType: GraphQLOutputType, newType: GraphQLOutputType): boolean {
if (!isWrappingType(oldType) && !isWrappingType(newType)) {
return oldType.toString() === newType.toString();
}
if (isNonNullType(newType)) {
const ofType = isNonNullType(oldType) ? oldType.ofType : oldType;
return safeChangeForField(ofType, newType.ofType);
}
if (isListType(oldType)) {
return (
(isListType(newType) && safeChangeForField(oldType.ofType, newType.ofType)) ||
(isNonNullType(newType) && safeChangeForField(oldType, newType.ofType))
);
}
return false;
}
export function safeChangeForInputValue(oldType: GraphQLInputType, newType: GraphQLInputType): boolean {
if (!isWrappingType(oldType) && !isWrappingType(newType)) {
return oldType.toString() === newType.toString();
}
if (isListType(oldType) && isListType(newType)) {
return safeChangeForInputValue(oldType.ofType, newType.ofType);
}
if (isNonNullType(oldType)) {
const ofType = isNonNullType(newType) ? newType.ofType : newType;
return safeChangeForInputValue(oldType.ofType, ofType);
}
return false;
}
export function getKind(type: GraphQLNamedType): KindEnum {
const node = type.astNode as any;
return (node && node.kind) || '';
}
export function getTypePrefix(type: GraphQLNamedType): string {
const kind = getKind(type);
const kindsMap: Record<string, string> = {
[Kind.SCALAR_TYPE_DEFINITION]: 'scalar',
[Kind.OBJECT_TYPE_DEFINITION]: 'type',
[Kind.INTERFACE_TYPE_DEFINITION]: 'interface',
[Kind.UNION_TYPE_DEFINITION]: 'union',
[Kind.ENUM_TYPE_DEFINITION]: 'enum',
[Kind.INPUT_OBJECT_TYPE_DEFINITION]: 'input',
};
return kindsMap[kind.toString()];
}
export function isPrimitive(type: GraphQLNamedType | string): boolean {
return ['String', 'Int', 'Float', 'Boolean', 'ID'].indexOf(typeof type === 'string' ? type : type.name) !== -1;
}
export function isForIntrospection(type: GraphQLNamedType | string): boolean {
return (
[
'__Schema',
'__Type',
'__TypeKind',
'__Field',
'__InputValue',
'__EnumValue',
'__Directive',
'__DirectiveLocation',
].indexOf(typeof type === 'string' ? type : type.name) !== -1
);
}
export function findDeprecatedUsages(schema: GraphQLSchema, ast: DocumentNode): Array<GraphQLError> {
const errors: GraphQLError[] = [];
const typeInfo = new TypeInfo(schema);
visit(
ast,
visitWithTypeInfo(typeInfo, {
Argument(node) {
const argument = typeInfo.getArgument();
if (argument) {
const reason = argument.deprecationReason;
if (reason) {
const fieldDef = typeInfo.getFieldDef();
if (fieldDef) {
errors.push(
new GraphQLError(`The argument '${argument?.name}' of '${fieldDef.name}' is deprecated. ${reason}`, [
node,
])
);
}
}
}
},
Field(node) {
const fieldDef = typeInfo.getFieldDef();
if (fieldDef && isDeprecated(fieldDef)) {
const parentType = typeInfo.getParentType();
if (parentType) {
const reason = fieldDef.deprecationReason;
errors.push(
new GraphQLError(
`The field '${parentType.name}.${fieldDef.name}' is deprecated.${reason ? ' ' + reason : ''}`,
[node]
)
);
}
}
},
EnumValue(node) {
const enumVal = typeInfo.getEnumValue();
if (enumVal && isDeprecated(enumVal)) {
const type = getNamedType(typeInfo.getInputType()!);
if (type) {
const reason = enumVal.deprecationReason;
errors.push(
new GraphQLError(
`The enum value '${type.name}.${enumVal.name}' is deprecated.${reason ? ' ' + reason : ''}`,
[node]
)
);
}
}
},
})
);
return errors;
}
export function removeFieldIfDirectives(node: FieldNode, directiveNames: string[]): FieldNode | null {
if (node.directives) {
if (node.directives.some(d => directiveNames.indexOf(d.name.value) !== -1)) {
return null;
}
}
return node;
}
export function removeDirectives(node: FieldNode, directiveNames: string[]): FieldNode {
if (node.directives) {
return {
...node,
directives: node.directives.filter(d => directiveNames.indexOf(d.name.value) === -1),
};
}
return node;
}
export function getReachableTypes(schema: GraphQLSchema): Set<string> {
const reachableTypes = new Set<string>();
const collect = (type: GraphQLNamedType): false | void => {
const typeName = type.name;
if (reachableTypes.has(typeName)) {
return;
}
reachableTypes.add(typeName);
if (isScalarType(type)) {
return;
} else if (isInterfaceType(type) || isObjectType(type)) {
if (isInterfaceType(type)) {
const { objects, interfaces } = schema.getImplementations(type);
for (const child of objects) {
collect(child);
}
for (const child of interfaces) {
collect(child);
}
}
const fields = type.getFields();
for (const fieldName in fields) {
const field = fields[fieldName];
collect(resolveOutputType(field.type));
const args = field.args;
for (const argName in args) {
const arg = args[argName];
collect(resolveInputType(arg.type));
}
}
} else if (isUnionType(type)) {
const types = type.getTypes();
for (const child of types) {
collect(child);
}
} else if (isInputObjectType(type)) {
const fields = type.getFields();
for (const fieldName in fields) {
const field = fields[fieldName];
collect(resolveInputType(field.type));
}
}
};
for (const type of [schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()]) {
if (type) {
collect(type);
}
}
return reachableTypes;
}
function resolveOutputType(
output: GraphQLOutputType
): GraphQLScalarType | GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType | GraphQLEnumType {
if (isListType(output) || isNonNullType(output)) {
return resolveOutputType(output.ofType);
}
return output;
}
function resolveInputType(input: GraphQLInputType): GraphQLScalarType | GraphQLEnumType | GraphQLInputObjectType {
if (isListType(input) || isNonNullType(input)) {
return resolveInputType(input.ofType);
}
return input;
}