-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathutils.ts
More file actions
303 lines (282 loc) · 8.6 KB
/
utils.ts
File metadata and controls
303 lines (282 loc) · 8.6 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import {
type ArgumentNode,
type DocumentNode,
type EnumTypeDefinitionNode,
type EnumTypeExtensionNode,
type FieldNode,
type InputObjectTypeDefinitionNode,
type InputObjectTypeExtensionNode,
type InterfaceTypeDefinitionNode,
type InterfaceTypeExtensionNode,
Kind,
type NamedTypeNode,
type NameNode,
type ObjectTypeDefinitionNode,
type ObjectTypeExtensionNode,
OperationTypeNode,
parse as graphqlParse,
type ScalarTypeDefinitionNode,
type ScalarTypeExtensionNode,
type SchemaDefinitionNode,
type SchemaExtensionNode,
type SelectionNode,
type SelectionSetNode,
type StringValueNode,
type UnionTypeDefinitionNode,
type UnionTypeExtensionNode,
} from 'graphql';
import { type CompositeOutputNode } from '../schema-building/ast';
import {
ARGUMENT_DEFINITION_UPPER,
ENUM_UPPER,
ENUM_VALUE_UPPER,
EXECUTABLE_DIRECTIVE_LOCATIONS,
FIELD_DEFINITION_UPPER,
FRAGMENT_DEFINITION_UPPER,
FRAGMENT_SPREAD_UPPER,
INLINE_FRAGMENT_UPPER,
INPUT_FIELD_DEFINITION_UPPER,
INPUT_OBJECT_UPPER,
INTERFACE_OBJECT,
INTERFACE_UPPER,
KEY,
MUTATION,
NAME,
OBJECT_UPPER,
QUERY,
SCALAR_UPPER,
SCHEMA_UPPER,
SUBSCRIPTION,
UNION_UPPER,
} from '../utils/string-constants';
export function isObjectLikeNodeEntity(node: CompositeOutputNode): boolean {
if (!node.directives?.length) {
return false;
}
for (const directive of node.directives) {
if (directive.name.value === KEY) {
return true;
}
}
return false;
}
export function isNodeInterfaceObject(node: ObjectTypeDefinitionNode): boolean {
if (!node.directives?.length) {
return false;
}
for (const directive of node.directives) {
if (directive.name.value === INTERFACE_OBJECT) {
return true;
}
}
return false;
}
export function stringToNameNode(value: string): NameNode {
return {
kind: Kind.NAME,
value,
};
}
export function stringArrayToNameNodeArray(values: string[]): NameNode[] {
const nameNodes: NameNode[] = [];
for (const value of values) {
nameNodes.push(stringToNameNode(value));
}
return nameNodes;
}
export function setToNameNodeArray(set: Set<string>): NameNode[] {
const nameNodes: NameNode[] = [];
for (const value of set) {
nameNodes.push(stringToNameNode(value));
}
return nameNodes;
}
export function stringToNamedTypeNode(value: string): NamedTypeNode {
return {
kind: Kind.NAMED_TYPE,
name: stringToNameNode(value),
};
}
export function setToNamedTypeNodeArray(set: Set<string>): NamedTypeNode[] {
const namedTypeNodes: NamedTypeNode[] = [];
for (const entry of set) {
namedTypeNodes.push(stringToNamedTypeNode(entry));
}
return namedTypeNodes;
}
export function nodeKindToDirectiveLocation(kind: Kind): string {
switch (kind) {
case Kind.ARGUMENT:
return ARGUMENT_DEFINITION_UPPER;
case Kind.ENUM_TYPE_DEFINITION:
// intentional fallthrough
case Kind.ENUM_TYPE_EXTENSION:
return ENUM_UPPER;
case Kind.ENUM_VALUE_DEFINITION:
return ENUM_VALUE_UPPER;
case Kind.FIELD_DEFINITION:
return FIELD_DEFINITION_UPPER;
case Kind.FRAGMENT_DEFINITION:
return FRAGMENT_DEFINITION_UPPER;
case Kind.FRAGMENT_SPREAD:
return FRAGMENT_SPREAD_UPPER;
case Kind.INLINE_FRAGMENT:
return INLINE_FRAGMENT_UPPER;
case Kind.INPUT_VALUE_DEFINITION:
return INPUT_FIELD_DEFINITION_UPPER;
case Kind.INPUT_OBJECT_TYPE_DEFINITION:
// intentional fallthrough
case Kind.INPUT_OBJECT_TYPE_EXTENSION:
return INPUT_OBJECT_UPPER;
case Kind.INTERFACE_TYPE_DEFINITION:
// intentional fallthrough
case Kind.INTERFACE_TYPE_EXTENSION:
return INTERFACE_UPPER;
case Kind.OBJECT_TYPE_DEFINITION:
// intentional fallthrough
case Kind.OBJECT_TYPE_EXTENSION:
return OBJECT_UPPER;
case Kind.SCALAR_TYPE_DEFINITION:
// intentional fallthrough
case Kind.SCALAR_TYPE_EXTENSION:
return SCALAR_UPPER;
case Kind.SCHEMA_DEFINITION:
// intentional fallthrough
case Kind.SCHEMA_EXTENSION:
return SCHEMA_UPPER;
case Kind.UNION_TYPE_DEFINITION:
// intentional fallthrough
case Kind.UNION_TYPE_EXTENSION:
return UNION_UPPER;
default:
return kind;
}
}
export const operationTypeNodeToDefaultType = new Map<OperationTypeNode, string>([
[OperationTypeNode.MUTATION, MUTATION],
[OperationTypeNode.QUERY, QUERY],
[OperationTypeNode.SUBSCRIPTION, SUBSCRIPTION],
]);
export function isKindAbstract(kind: Kind) {
return kind === Kind.INTERFACE_TYPE_DEFINITION || kind === Kind.UNION_TYPE_DEFINITION;
}
export function extractExecutableDirectiveLocations(
nodes: readonly NameNode[] | NameNode[],
set: Set<string>,
): Set<string> {
for (const node of nodes) {
const name = node.value;
if (EXECUTABLE_DIRECTIVE_LOCATIONS.has(name)) {
set.add(name);
}
}
return set;
}
export function formatDescription(description?: StringValueNode): StringValueNode | undefined {
if (!description) {
return description;
}
let value = description.value;
if (description.block) {
const lines = value.split('\n');
if (lines.length > 1) {
value = lines.map((line) => line.trimStart()).join('\n');
}
}
return { ...description, value: value, block: true };
}
export function lexicographicallySortArgumentNodes(fieldNode: FieldNode): Array<ArgumentNode> | undefined {
if (!fieldNode.arguments) {
return fieldNode.arguments;
}
const argumentNodes = fieldNode.arguments as Array<ArgumentNode>;
return argumentNodes.sort((a, b) => a.name.value.localeCompare(b.name.value));
}
export function lexicographicallySortSelectionSetNode(selectionSetNode: SelectionSetNode): SelectionSetNode {
const selections = selectionSetNode.selections as Array<SelectionNode>;
return {
...selectionSetNode,
selections: selections
.sort((a, b) => {
if (NAME in a) {
if (!(NAME in b)) {
return -1;
}
return a.name.value.localeCompare(b.name.value);
}
if (NAME in b) {
return 1;
}
const aName = a.typeCondition?.name.value ?? '';
return aName.localeCompare(b.typeCondition?.name.value ?? '');
})
.map((selection) => {
switch (selection.kind) {
case Kind.FIELD: {
return {
...selection,
arguments: lexicographicallySortArgumentNodes(selection),
selectionSet: selection.selectionSet
? lexicographicallySortSelectionSetNode(selection.selectionSet)
: selection.selectionSet,
};
}
case Kind.FRAGMENT_SPREAD: {
return selection;
}
case Kind.INLINE_FRAGMENT: {
return {
...selection,
selectionSet: lexicographicallySortSelectionSetNode(selection.selectionSet),
};
}
}
}),
};
}
export function lexicographicallySortDocumentNode(documentNode: DocumentNode): DocumentNode {
return {
...documentNode,
definitions: documentNode.definitions.map((definition) => {
if (definition.kind !== Kind.OPERATION_DEFINITION) {
return definition;
}
return {
...definition,
selectionSet: lexicographicallySortSelectionSetNode(definition.selectionSet),
};
}),
};
}
type ParseResult = {
documentNode?: DocumentNode;
error?: Error;
};
export function parse(source: string, noLocation = true): DocumentNode {
return graphqlParse(source, { noLocation });
}
export function safeParse(value: string, noLocation = true): ParseResult {
try {
const documentNode = parse(value, noLocation);
return { documentNode };
} catch (e) {
return { error: e as Error };
}
}
export type EnumTypeNode = EnumTypeDefinitionNode | EnumTypeExtensionNode;
export type InputObjectTypeNode = InputObjectTypeDefinitionNode | InputObjectTypeExtensionNode;
export type InterfaceTypeNode = InterfaceTypeDefinitionNode | InterfaceTypeExtensionNode;
export type ObjectTypeNode = ObjectTypeDefinitionNode | ObjectTypeExtensionNode;
export type ScalarTypeNode = ScalarTypeDefinitionNode | ScalarTypeExtensionNode;
export type SchemaNode = SchemaDefinitionNode | SchemaExtensionNode;
export type UnionTypeNode = UnionTypeDefinitionNode | UnionTypeExtensionNode;
export type ParentTypeNode =
| EnumTypeNode
| InputObjectTypeNode
| InterfaceTypeNode
| ObjectTypeNode
| ScalarTypeNode
| UnionTypeNode;
export type InterfaceNodeKind = Kind.INTERFACE_TYPE_DEFINITION | Kind.INTERFACE_TYPE_EXTENSION;
export type ObjectNodeKind = Kind.OBJECT_TYPE_DEFINITION | Kind.OBJECT_TYPE_EXTENSION;
export type CompositeOutputNodeKind = InterfaceNodeKind | ObjectNodeKind;