@graphiql/plugin-query-builder 0.0.0 → 1.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README.md +34 -0
  2. package/dist/components/arg-input/arg-input-by-type.d.ts +21 -0
  3. package/dist/components/arg-input/arg-input-by-type.js +459 -0
  4. package/dist/components/arg-input/arg-input-by-type.js.map +1 -0
  5. package/dist/components/arg-input/enum-arg-control.d.ts +9 -0
  6. package/dist/components/arg-input/enum-arg-control.js +56 -0
  7. package/dist/components/arg-input/enum-arg-control.js.map +1 -0
  8. package/dist/components/arg-input/index.d.ts +29 -0
  9. package/dist/components/arg-input/index.js +38 -0
  10. package/dist/components/arg-input/index.js.map +1 -0
  11. package/dist/components/arg-input/scalar-arg-control.d.ts +16 -0
  12. package/dist/components/arg-input/scalar-arg-control.js +66 -0
  13. package/dist/components/arg-input/scalar-arg-control.js.map +1 -0
  14. package/dist/components/field-row.d.ts +34 -0
  15. package/dist/components/field-row.js +199 -0
  16. package/dist/components/field-row.js.map +1 -0
  17. package/dist/components/field-tree-context.d.ts +32 -0
  18. package/dist/components/field-tree-context.js +33 -0
  19. package/dist/components/field-tree-context.js.map +1 -0
  20. package/dist/components/field-tree-node.d.ts +9 -0
  21. package/dist/components/field-tree-node.js +118 -0
  22. package/dist/components/field-tree-node.js.map +1 -0
  23. package/dist/components/field-tree.d.ts +30 -0
  24. package/dist/components/field-tree.js +204 -0
  25. package/dist/components/field-tree.js.map +1 -0
  26. package/dist/components/fragment-editor.d.ts +28 -0
  27. package/dist/components/fragment-editor.js +154 -0
  28. package/dist/components/fragment-editor.js.map +1 -0
  29. package/dist/components/fragment-section.d.ts +23 -0
  30. package/dist/components/fragment-section.js +207 -0
  31. package/dist/components/fragment-section.js.map +1 -0
  32. package/dist/components/query-builder.d.ts +2 -0
  33. package/dist/components/query-builder.js +193 -0
  34. package/dist/components/query-builder.js.map +1 -0
  35. package/dist/components/type-condition-selector.d.ts +18 -0
  36. package/dist/components/type-condition-selector.js +472 -0
  37. package/dist/components/type-condition-selector.js.map +1 -0
  38. package/dist/components/use-cursor-path.d.ts +16 -0
  39. package/dist/components/use-cursor-path.js +79 -0
  40. package/dist/components/use-cursor-path.js.map +1 -0
  41. package/dist/components/use-cursor-reveal.d.ts +15 -0
  42. package/dist/components/use-cursor-reveal.js +92 -0
  43. package/dist/components/use-cursor-reveal.js.map +1 -0
  44. package/dist/components/use-working-document.d.ts +31 -0
  45. package/dist/components/use-working-document.js +548 -0
  46. package/dist/components/use-working-document.js.map +1 -0
  47. package/dist/index.d.ts +4 -0
  48. package/dist/index.js +13 -0
  49. package/dist/index.js.map +1 -0
  50. package/dist/lib/arg-value.d.ts +42 -0
  51. package/dist/lib/arg-value.js +147 -0
  52. package/dist/lib/arg-value.js.map +1 -0
  53. package/dist/lib/arguments.d.ts +28 -0
  54. package/dist/lib/arguments.js +62 -0
  55. package/dist/lib/arguments.js.map +1 -0
  56. package/dist/lib/ast-path.d.ts +109 -0
  57. package/dist/lib/ast-path.js +330 -0
  58. package/dist/lib/ast-path.js.map +1 -0
  59. package/dist/lib/document-mutator.d.ts +8 -0
  60. package/dist/lib/field-list-view.d.ts +30 -0
  61. package/dist/lib/field-list-view.js +47 -0
  62. package/dist/lib/field-list-view.js.map +1 -0
  63. package/dist/lib/field-selection.d.ts +26 -0
  64. package/dist/lib/field-selection.js +76 -0
  65. package/dist/lib/field-selection.js.map +1 -0
  66. package/dist/lib/fragments.d.ts +63 -0
  67. package/dist/lib/fragments.js +165 -0
  68. package/dist/lib/fragments.js.map +1 -0
  69. package/dist/lib/inline-fragments.d.ts +26 -0
  70. package/dist/lib/inline-fragments.js +71 -0
  71. package/dist/lib/inline-fragments.js.map +1 -0
  72. package/dist/lib/schema-walk.d.ts +47 -0
  73. package/dist/lib/schema-walk.js +177 -0
  74. package/dist/lib/schema-walk.js.map +1 -0
  75. package/dist/lib/variables.d.ts +33 -0
  76. package/dist/lib/variables.js +100 -0
  77. package/dist/lib/variables.js.map +1 -0
  78. package/dist/stories/query-builder.stories.d.ts +43 -0
  79. package/dist/style.css +957 -0
  80. package/package.json +67 -1
@@ -0,0 +1,330 @@
1
+ import { Kind } from "graphql";
2
+ function fieldSegment(name) {
3
+ return {
4
+ kind: "field",
5
+ name
6
+ };
7
+ }
8
+ function inlineFragmentSegment(typeName) {
9
+ return {
10
+ kind: "inlineFragment",
11
+ typeName
12
+ };
13
+ }
14
+ function segmentsEqual(a, b) {
15
+ if (a.kind !== b.kind) {
16
+ return false;
17
+ }
18
+ if (a.kind === "field" && b.kind === "field") {
19
+ return a.name === b.name;
20
+ }
21
+ if (a.kind === "inlineFragment" && b.kind === "inlineFragment") {
22
+ return a.typeName === b.typeName;
23
+ }
24
+ return false;
25
+ }
26
+ function selectionMatchesSegment(selection, segment) {
27
+ var _a;
28
+ switch (segment.kind) {
29
+ case "inlineFragment":
30
+ return selection.kind === Kind.INLINE_FRAGMENT && ((_a = selection.typeCondition) == null ? void 0 : _a.name.value) === segment.typeName;
31
+ case "field":
32
+ return selection.kind === Kind.FIELD && selection.name.value === segment.name;
33
+ }
34
+ }
35
+ function findNodeAtPath(selectionSet, path) {
36
+ let current = selectionSet;
37
+ for (let i = 0; i < path.length; i++) {
38
+ const segment = path[i];
39
+ const node = current.selections.find((s) => selectionMatchesSegment(s, segment));
40
+ if (!node) {
41
+ return void 0;
42
+ }
43
+ if (i === path.length - 1) {
44
+ return node;
45
+ }
46
+ if (!node.selectionSet) {
47
+ return void 0;
48
+ }
49
+ current = node.selectionSet;
50
+ }
51
+ return void 0;
52
+ }
53
+ function mapNodeAtPath(selectionSet, path, fn, pruneEmptyParent = true) {
54
+ const [segment, ...rest] = path;
55
+ const nodeIndex = selectionSet.selections.findIndex((s) => selectionMatchesSegment(s, segment));
56
+ if (nodeIndex === -1) {
57
+ return selectionSet;
58
+ }
59
+ const node = selectionSet.selections[nodeIndex];
60
+ if (rest.length === 0) {
61
+ const replacement = fn(node);
62
+ const newSelections2 = [...selectionSet.selections];
63
+ if (replacement === null) {
64
+ newSelections2.splice(nodeIndex, 1);
65
+ } else {
66
+ newSelections2[nodeIndex] = replacement;
67
+ }
68
+ return {
69
+ ...selectionSet,
70
+ selections: newSelections2
71
+ };
72
+ }
73
+ if (!node.selectionSet) {
74
+ return selectionSet;
75
+ }
76
+ const newChildSet = mapNodeAtPath(node.selectionSet, rest, fn, pruneEmptyParent);
77
+ if (newChildSet === node.selectionSet) {
78
+ return selectionSet;
79
+ }
80
+ if (pruneEmptyParent && newChildSet.selections.length === 0) {
81
+ const newSelections2 = selectionSet.selections.filter((_, i) => i !== nodeIndex);
82
+ return {
83
+ ...selectionSet,
84
+ selections: newSelections2
85
+ };
86
+ }
87
+ const updated = {
88
+ ...node,
89
+ selectionSet: newChildSet
90
+ };
91
+ const newSelections = [...selectionSet.selections];
92
+ newSelections[nodeIndex] = updated;
93
+ return {
94
+ ...selectionSet,
95
+ selections: newSelections
96
+ };
97
+ }
98
+ function createSelectionForSegment(segment, selectionSet) {
99
+ switch (segment.kind) {
100
+ case "inlineFragment":
101
+ return {
102
+ kind: Kind.INLINE_FRAGMENT,
103
+ typeCondition: {
104
+ kind: Kind.NAMED_TYPE,
105
+ name: {
106
+ kind: Kind.NAME,
107
+ value: segment.typeName
108
+ }
109
+ },
110
+ directives: [],
111
+ selectionSet: selectionSet ?? {
112
+ kind: Kind.SELECTION_SET,
113
+ selections: []
114
+ }
115
+ };
116
+ case "field":
117
+ return {
118
+ kind: Kind.FIELD,
119
+ name: {
120
+ kind: Kind.NAME,
121
+ value: segment.name
122
+ },
123
+ arguments: [],
124
+ directives: [],
125
+ selectionSet
126
+ };
127
+ }
128
+ }
129
+ function addField(selectionSet, path) {
130
+ const [segment, ...rest] = path;
131
+ if (rest.length === 0) {
132
+ const alreadyPresent = selectionSet.selections.some((s) => selectionMatchesSegment(s, segment));
133
+ if (alreadyPresent) {
134
+ return selectionSet;
135
+ }
136
+ const newNode = createSelectionForSegment(segment);
137
+ return {
138
+ ...selectionSet,
139
+ selections: [...selectionSet.selections, newNode]
140
+ };
141
+ }
142
+ const existingIndex = selectionSet.selections.findIndex((s) => selectionMatchesSegment(s, segment));
143
+ if (existingIndex === -1) {
144
+ const emptySet = {
145
+ kind: Kind.SELECTION_SET,
146
+ selections: []
147
+ };
148
+ const parentNode = createSelectionForSegment(segment, addField(emptySet, rest));
149
+ return {
150
+ ...selectionSet,
151
+ selections: [...selectionSet.selections, parentNode]
152
+ };
153
+ }
154
+ const existing = selectionSet.selections[existingIndex];
155
+ const childSet = existing.selectionSet ?? {
156
+ kind: Kind.SELECTION_SET,
157
+ selections: []
158
+ };
159
+ const updated = {
160
+ ...existing,
161
+ selectionSet: addField(childSet, rest)
162
+ };
163
+ const newSelections = [...selectionSet.selections];
164
+ newSelections[existingIndex] = updated;
165
+ return {
166
+ ...selectionSet,
167
+ selections: newSelections
168
+ };
169
+ }
170
+ function removeField(selectionSet, path) {
171
+ return mapNodeAtPath(selectionSet, path, () => null, true);
172
+ }
173
+ function replaceFieldSelectionSet(selectionSet, path, newSet) {
174
+ return mapNodeAtPath(selectionSet, path, (node) => ({
175
+ ...node,
176
+ selectionSet: newSet
177
+ }), false);
178
+ }
179
+ function setArgInSelectionSet(selectionSet, path, argName, value) {
180
+ return mapNodeAtPath(
181
+ selectionSet,
182
+ path,
183
+ (node) => {
184
+ const field = node;
185
+ const existingArgs = field.arguments ?? [];
186
+ let newArgs;
187
+ if (value === void 0) {
188
+ newArgs = existingArgs.filter((a) => a.name.value !== argName);
189
+ } else {
190
+ const existingIndex = existingArgs.findIndex((a) => a.name.value === argName);
191
+ const newArg = {
192
+ kind: Kind.ARGUMENT,
193
+ name: {
194
+ kind: Kind.NAME,
195
+ value: argName
196
+ },
197
+ value
198
+ };
199
+ if (existingIndex === -1) {
200
+ newArgs = [...existingArgs, newArg];
201
+ } else {
202
+ newArgs = [...existingArgs];
203
+ newArgs[existingIndex] = newArg;
204
+ }
205
+ }
206
+ return {
207
+ ...field,
208
+ arguments: newArgs
209
+ };
210
+ },
211
+ false
212
+ // do not prune empty parents when setting args
213
+ );
214
+ }
215
+ function findSelectionSet(selectionSet, path) {
216
+ const node = findNodeAtPath(selectionSet, path);
217
+ return node == null ? void 0 : node.selectionSet;
218
+ }
219
+ function findOperationIndex(doc, operationName) {
220
+ if (operationName) {
221
+ const named = doc.definitions.findIndex((d) => {
222
+ var _a;
223
+ return d.kind === Kind.OPERATION_DEFINITION && ((_a = d.name) == null ? void 0 : _a.value) === operationName;
224
+ });
225
+ if (named !== -1) {
226
+ return named;
227
+ }
228
+ }
229
+ return doc.definitions.findIndex((d) => d.kind === Kind.OPERATION_DEFINITION);
230
+ }
231
+ function findOperation(doc, operationName) {
232
+ const index = findOperationIndex(doc, operationName);
233
+ if (index === -1) {
234
+ return void 0;
235
+ }
236
+ const operation = doc.definitions[index];
237
+ return (operation == null ? void 0 : operation.kind) === Kind.OPERATION_DEFINITION ? operation : void 0;
238
+ }
239
+ function mapOperation(doc, operationName, fn) {
240
+ const index = findOperationIndex(doc, operationName);
241
+ if (index === -1) {
242
+ return doc;
243
+ }
244
+ const def = doc.definitions[index];
245
+ if ((def == null ? void 0 : def.kind) !== Kind.OPERATION_DEFINITION) {
246
+ return doc;
247
+ }
248
+ const result = fn(def);
249
+ if (result === def) {
250
+ return doc;
251
+ }
252
+ const newDefinitions = [...doc.definitions];
253
+ if (result === null) {
254
+ newDefinitions.splice(index, 1);
255
+ } else {
256
+ newDefinitions[index] = result;
257
+ }
258
+ return {
259
+ ...doc,
260
+ definitions: newDefinitions
261
+ };
262
+ }
263
+ function findDefinitionIndex(doc, target) {
264
+ if (target.kind === "operation") {
265
+ return findOperationIndex(doc, target.name);
266
+ }
267
+ return doc.definitions.findIndex((d) => d.kind === Kind.FRAGMENT_DEFINITION && d.name.value === target.name);
268
+ }
269
+ function findDefinition(doc, target) {
270
+ const index = findDefinitionIndex(doc, target);
271
+ if (index === -1) {
272
+ return void 0;
273
+ }
274
+ const def = doc.definitions[index];
275
+ if ((def == null ? void 0 : def.kind) === Kind.OPERATION_DEFINITION || (def == null ? void 0 : def.kind) === Kind.FRAGMENT_DEFINITION) {
276
+ return def;
277
+ }
278
+ return void 0;
279
+ }
280
+ function mapDefinition(doc, target, fn) {
281
+ const index = findDefinitionIndex(doc, target);
282
+ if (index === -1) {
283
+ return doc;
284
+ }
285
+ const def = doc.definitions[index];
286
+ if ((def == null ? void 0 : def.kind) !== Kind.OPERATION_DEFINITION && (def == null ? void 0 : def.kind) !== Kind.FRAGMENT_DEFINITION) {
287
+ return doc;
288
+ }
289
+ const result = fn(def);
290
+ if (result === def) {
291
+ return doc;
292
+ }
293
+ const newDefinitions = [...doc.definitions];
294
+ if (result === null) {
295
+ newDefinitions.splice(index, 1);
296
+ } else {
297
+ newDefinitions[index] = result;
298
+ }
299
+ return {
300
+ ...doc,
301
+ definitions: newDefinitions
302
+ };
303
+ }
304
+ function definitionSelectionOffset(doc, target) {
305
+ var _a;
306
+ const loc = (_a = findDefinition(doc, target)) == null ? void 0 : _a.selectionSet.loc;
307
+ return loc ? loc.start + 1 : void 0;
308
+ }
309
+ export {
310
+ addField,
311
+ createSelectionForSegment,
312
+ definitionSelectionOffset,
313
+ fieldSegment,
314
+ findDefinition,
315
+ findDefinitionIndex,
316
+ findNodeAtPath,
317
+ findOperation,
318
+ findOperationIndex,
319
+ findSelectionSet,
320
+ inlineFragmentSegment,
321
+ mapDefinition,
322
+ mapNodeAtPath,
323
+ mapOperation,
324
+ removeField,
325
+ replaceFieldSelectionSet,
326
+ segmentsEqual,
327
+ selectionMatchesSegment,
328
+ setArgInSelectionSet
329
+ };
330
+ //# sourceMappingURL=ast-path.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ast-path.js","sources":["../../src/lib/ast-path.ts"],"sourcesContent":["import {\n Kind,\n type ArgumentNode,\n type DocumentNode,\n type FieldNode,\n type FragmentDefinitionNode,\n type InlineFragmentNode,\n type OperationDefinitionNode,\n type SelectionNode,\n type SelectionSetNode,\n type ValueNode,\n} from 'graphql';\n\nexport type PathSegment =\n | { kind: 'field'; name: string }\n | { kind: 'inlineFragment'; typeName: string };\n\nexport type Path = PathSegment[];\n\n/**\n * The definition the builder is currently editing: either an operation\n * (identified by name, or the first operation when unnamed) or a named\n * fragment. Every read/mutation helper resolves the selection set it works on\n * from this target, so the same tree and handlers drive operation and fragment\n * editing alike.\n */\nexport type DefinitionTarget =\n | { kind: 'operation'; name?: string }\n | { kind: 'fragment'; name: string };\n\n/** A definition the builder can edit: an operation or a named fragment. */\nexport type EditableDefinitionNode =\n | OperationDefinitionNode\n | FragmentDefinitionNode;\n\nexport function fieldSegment(name: string): PathSegment {\n return { kind: 'field', name };\n}\n\n/** Builds the path segment that addresses the `... on TypeName` inline fragment. */\nexport function inlineFragmentSegment(typeName: string): PathSegment {\n return { kind: 'inlineFragment', typeName };\n}\n\nexport function segmentsEqual(a: PathSegment, b: PathSegment): boolean {\n if (a.kind !== b.kind) {\n return false;\n }\n if (a.kind === 'field' && b.kind === 'field') {\n return a.name === b.name;\n }\n if (a.kind === 'inlineFragment' && b.kind === 'inlineFragment') {\n return a.typeName === b.typeName;\n }\n return false;\n}\n\n/**\n * True when `selection` is the field or inline fragment named by `segment`.\n *\n * Fields are matched by schema name, not response key: the builder's tree is\n * driven by the schema, so a path segment is a field name and never carries an\n * alias. A document the user hand-aliased (`a: hero b: hero`) therefore matches\n * the first selection with that name. Helpers that walk to a single node act\n * on that first match only (see `mapNodeAtPath`), so unchecking an aliased\n * field removes one occurrence rather than wiping every alias of it. Full\n * alias-aware editing would require a selection-driven tree and is out of scope.\n */\nexport function selectionMatchesSegment(\n selection: SelectionNode,\n segment: PathSegment,\n): boolean {\n switch (segment.kind) {\n case 'inlineFragment':\n return (\n selection.kind === Kind.INLINE_FRAGMENT &&\n selection.typeCondition?.name.value === segment.typeName\n );\n case 'field':\n return (\n selection.kind === Kind.FIELD && selection.name.value === segment.name\n );\n }\n}\n\n/**\n * Walks `path` through `selectionSet` via `selectionMatchesSegment` and\n * returns the node at the final segment, or `undefined` if any segment is\n * missing.\n */\nexport function findNodeAtPath(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n): FieldNode | InlineFragmentNode | undefined {\n let current: SelectionSetNode = selectionSet;\n for (let i = 0; i < path.length; i++) {\n const segment = path[i]!;\n const node = current.selections.find(s =>\n selectionMatchesSegment(s, segment),\n ) as FieldNode | InlineFragmentNode | undefined;\n if (!node) {\n return undefined;\n }\n if (i === path.length - 1) {\n return node;\n }\n if (!node.selectionSet) {\n return undefined;\n }\n current = node.selectionSet;\n }\n return undefined;\n}\n\n/**\n * Immutably rebuilds `selectionSet`, descending to the node at `path` via\n * `selectionMatchesSegment`, then applying `fn` to it. `fn` returns the\n * replacement node, or `undefined` to delete it. When `pruneEmptyParent` is\n * true (default), a non-leaf ancestor whose child selection set becomes empty\n * after the operation is itself pruned. Returns the original `selectionSet`\n * reference unchanged when `path` does not resolve.\n */\nexport function mapNodeAtPath(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n fn: (\n node: FieldNode | InlineFragmentNode,\n ) => FieldNode | InlineFragmentNode | null,\n pruneEmptyParent = true,\n): SelectionSetNode {\n const [segment, ...rest] = path as [PathSegment, ...PathSegment[]];\n const nodeIndex = selectionSet.selections.findIndex(s =>\n selectionMatchesSegment(s, segment),\n );\n if (nodeIndex === -1) {\n return selectionSet;\n }\n\n const node = selectionSet.selections[nodeIndex] as\n | FieldNode\n | InlineFragmentNode;\n\n if (rest.length === 0) {\n const replacement = fn(node);\n const newSelections = [...selectionSet.selections];\n if (replacement === null) {\n newSelections.splice(nodeIndex, 1);\n } else {\n newSelections[nodeIndex] = replacement;\n }\n return { ...selectionSet, selections: newSelections };\n }\n\n if (!node.selectionSet) {\n return selectionSet;\n }\n const newChildSet = mapNodeAtPath(\n node.selectionSet,\n rest,\n fn,\n pruneEmptyParent,\n );\n if (newChildSet === node.selectionSet) {\n return selectionSet;\n }\n\n if (pruneEmptyParent && newChildSet.selections.length === 0) {\n const newSelections = selectionSet.selections.filter(\n (_, i) => i !== nodeIndex,\n );\n return { ...selectionSet, selections: newSelections };\n }\n\n const updated = { ...node, selectionSet: newChildSet } as\n | FieldNode\n | InlineFragmentNode;\n const newSelections = [...selectionSet.selections];\n newSelections[nodeIndex] = updated;\n return { ...selectionSet, selections: newSelections };\n}\n\n/** Creates the field or inline fragment for `segment`, with the given child set. */\nexport function createSelectionForSegment(\n segment: PathSegment,\n selectionSet?: SelectionSetNode,\n): FieldNode | InlineFragmentNode {\n switch (segment.kind) {\n case 'inlineFragment':\n return {\n kind: Kind.INLINE_FRAGMENT,\n typeCondition: {\n kind: Kind.NAMED_TYPE,\n name: {\n kind: Kind.NAME,\n value: segment.typeName,\n },\n },\n directives: [],\n selectionSet: selectionSet ?? {\n kind: Kind.SELECTION_SET,\n selections: [],\n },\n };\n case 'field':\n return {\n kind: Kind.FIELD,\n name: { kind: Kind.NAME, value: segment.name },\n arguments: [],\n directives: [],\n selectionSet,\n };\n }\n}\n\nexport function addField(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n): SelectionSetNode {\n const [segment, ...rest] = path as [PathSegment, ...PathSegment[]];\n\n if (rest.length === 0) {\n const alreadyPresent = selectionSet.selections.some(s =>\n selectionMatchesSegment(s, segment),\n );\n if (alreadyPresent) {\n return selectionSet;\n }\n\n const newNode = createSelectionForSegment(segment);\n return {\n ...selectionSet,\n selections: [...selectionSet.selections, newNode],\n };\n }\n\n const existingIndex = selectionSet.selections.findIndex(s =>\n selectionMatchesSegment(s, segment),\n );\n\n if (existingIndex === -1) {\n // Parent node doesn't exist yet; create it and recurse to add the child.\n const emptySet: SelectionSetNode = {\n kind: Kind.SELECTION_SET,\n selections: [],\n };\n const parentNode = createSelectionForSegment(\n segment,\n addField(emptySet, rest),\n );\n return {\n ...selectionSet,\n selections: [...selectionSet.selections, parentNode],\n };\n }\n\n const existing = selectionSet.selections[existingIndex] as\n | FieldNode\n | InlineFragmentNode;\n const childSet: SelectionSetNode = existing.selectionSet ?? {\n kind: Kind.SELECTION_SET,\n selections: [],\n };\n const updated = {\n ...existing,\n selectionSet: addField(childSet, rest),\n } as FieldNode | InlineFragmentNode;\n const newSelections = [...selectionSet.selections];\n newSelections[existingIndex] = updated;\n return { ...selectionSet, selections: newSelections };\n}\n\nexport function removeField(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n): SelectionSetNode {\n // Delete the terminal node (fn returns null); pruneEmptyParent=true\n // ensures any ancestor whose child set became empty is also pruned.\n return mapNodeAtPath(selectionSet, path, () => null, true);\n}\n\nexport function replaceFieldSelectionSet(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n newSet: SelectionSetNode,\n): SelectionSetNode {\n return mapNodeAtPath(\n selectionSet,\n path,\n node =>\n ({ ...node, selectionSet: newSet }) as FieldNode | InlineFragmentNode,\n false,\n );\n}\n\nexport function setArgInSelectionSet(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n argName: string,\n value: ValueNode | undefined,\n): SelectionSetNode {\n return mapNodeAtPath(\n selectionSet,\n path,\n node => {\n // Arguments live on fields, not inline fragments.\n const field = node as FieldNode;\n const existingArgs: readonly ArgumentNode[] = field.arguments ?? [];\n let newArgs: ArgumentNode[];\n if (value === undefined) {\n newArgs = existingArgs.filter(a => a.name.value !== argName);\n } else {\n const existingIndex = existingArgs.findIndex(\n a => a.name.value === argName,\n );\n const newArg: ArgumentNode = {\n kind: Kind.ARGUMENT,\n name: { kind: Kind.NAME, value: argName },\n value,\n };\n if (existingIndex === -1) {\n newArgs = [...existingArgs, newArg];\n } else {\n newArgs = [...existingArgs];\n newArgs[existingIndex] = newArg;\n }\n }\n return { ...field, arguments: newArgs };\n },\n false, // do not prune empty parents when setting args\n );\n}\n\nexport function findSelectionSet(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n): SelectionSetNode | undefined {\n const node = findNodeAtPath(selectionSet, path);\n return node?.selectionSet;\n}\n\n/**\n * Returns the index in `doc.definitions` of the operation to operate on:\n * the operation whose name matches `operationName` when given and present,\n * otherwise the first operation definition. Returns -1 when the document has\n * no operation definitions.\n */\nexport function findOperationIndex(\n doc: DocumentNode,\n operationName?: string,\n): number {\n if (operationName) {\n const named = doc.definitions.findIndex(\n d =>\n d.kind === Kind.OPERATION_DEFINITION && d.name?.value === operationName,\n );\n if (named !== -1) {\n return named;\n }\n }\n return doc.definitions.findIndex(d => d.kind === Kind.OPERATION_DEFINITION);\n}\n\n/**\n * Resolves the target operation node (see {@link findOperationIndex}), or\n * `undefined` when the document has no matching operation.\n */\nexport function findOperation(\n doc: DocumentNode,\n operationName?: string,\n): OperationDefinitionNode | undefined {\n const index = findOperationIndex(doc, operationName);\n if (index === -1) {\n return undefined;\n }\n const operation = doc.definitions[index];\n return operation?.kind === Kind.OPERATION_DEFINITION ? operation : undefined;\n}\n\n/**\n * Finds the target operation, calls `fn` with it, and returns a new\n * `DocumentNode` with the result spliced back into `definitions`. When `fn`\n * returns `null` the operation is removed entirely (used when an operation\n * becomes empty and unprintable). Returns `doc` unchanged when no matching\n * operation definition exists.\n */\nexport function mapOperation(\n doc: DocumentNode,\n operationName: string | undefined,\n fn: (operation: OperationDefinitionNode) => OperationDefinitionNode | null,\n): DocumentNode {\n const index = findOperationIndex(doc, operationName);\n if (index === -1) {\n return doc;\n }\n const def = doc.definitions[index];\n if (def?.kind !== Kind.OPERATION_DEFINITION) {\n return doc;\n }\n const result = fn(def);\n // When fn returns the same reference, the operation is unchanged — return\n // the original doc so callers that rely on reference equality for no-op\n // detection (e.g. toBe checks) continue to work.\n if (result === def) {\n return doc;\n }\n const newDefinitions = [...doc.definitions];\n if (result === null) {\n newDefinitions.splice(index, 1);\n } else {\n newDefinitions[index] = result;\n }\n return { ...doc, definitions: newDefinitions };\n}\n\n/**\n * Returns the index in `doc.definitions` of the definition addressed by\n * `target` — the matching operation (see {@link findOperationIndex}) or the\n * named fragment. Returns -1 when no such definition exists.\n */\nexport function findDefinitionIndex(\n doc: DocumentNode,\n target: DefinitionTarget,\n): number {\n if (target.kind === 'operation') {\n return findOperationIndex(doc, target.name);\n }\n return doc.definitions.findIndex(\n d => d.kind === Kind.FRAGMENT_DEFINITION && d.name.value === target.name,\n );\n}\n\n/**\n * Resolves the definition node addressed by `target`, or `undefined` when the\n * document has no matching definition.\n */\nexport function findDefinition(\n doc: DocumentNode,\n target: DefinitionTarget,\n): EditableDefinitionNode | undefined {\n const index = findDefinitionIndex(doc, target);\n if (index === -1) {\n return undefined;\n }\n const def = doc.definitions[index];\n if (\n def?.kind === Kind.OPERATION_DEFINITION ||\n def?.kind === Kind.FRAGMENT_DEFINITION\n ) {\n return def;\n }\n return undefined;\n}\n\n/**\n * Finds the definition addressed by `target`, calls `fn` with it, and returns a\n * new `DocumentNode` with the result spliced back into `definitions`. When `fn`\n * returns `null` the definition is removed entirely. Returns `doc` unchanged\n * when no matching definition exists or `fn` returns the same reference.\n */\nexport function mapDefinition(\n doc: DocumentNode,\n target: DefinitionTarget,\n fn: (definition: EditableDefinitionNode) => EditableDefinitionNode | null,\n): DocumentNode {\n const index = findDefinitionIndex(doc, target);\n if (index === -1) {\n return doc;\n }\n const def = doc.definitions[index];\n if (\n def?.kind !== Kind.OPERATION_DEFINITION &&\n def?.kind !== Kind.FRAGMENT_DEFINITION\n ) {\n return doc;\n }\n const result = fn(def);\n if (result === def) {\n return doc;\n }\n const newDefinitions = [...doc.definitions];\n if (result === null) {\n newDefinitions.splice(index, 1);\n } else {\n newDefinitions[index] = result;\n }\n return { ...doc, definitions: newDefinitions };\n}\n\n/**\n * A source offset just inside the selection set of the definition addressed by\n * `target`, or `undefined` when it can't be located. Used to move the editor\n * cursor into a definition so the builder — which follows the cursor — switches\n * to editing it. Requires `doc` to carry location info (parse without\n * `noLocation`).\n */\nexport function definitionSelectionOffset(\n doc: DocumentNode,\n target: DefinitionTarget,\n): number | undefined {\n const loc = findDefinition(doc, target)?.selectionSet.loc;\n return loc ? loc.start + 1 : undefined;\n}\n"],"names":["fieldSegment","name","kind","inlineFragmentSegment","typeName","segmentsEqual","a","b","selectionMatchesSegment","selection","segment","Kind","INLINE_FRAGMENT","typeCondition","value","FIELD","findNodeAtPath","selectionSet","path","current","i","length","node","selections","find","s","undefined","mapNodeAtPath","fn","pruneEmptyParent","rest","nodeIndex","findIndex","replacement","newSelections","splice","newChildSet","filter","_","updated","createSelectionForSegment","NAMED_TYPE","NAME","directives","SELECTION_SET","arguments","addField","alreadyPresent","some","newNode","existingIndex","emptySet","parentNode","existing","childSet","removeField","replaceFieldSelectionSet","newSet","setArgInSelectionSet","argName","field","existingArgs","newArgs","newArg","ARGUMENT","findSelectionSet","findOperationIndex","doc","operationName","named","definitions","d","OPERATION_DEFINITION","findOperation","index","operation","mapOperation","def","result","newDefinitions","findDefinitionIndex","target","FRAGMENT_DEFINITION","findDefinition","mapDefinition","definitionSelectionOffset","loc","start"],"mappings":";AAmCO,SAASA,aAAaC,MAA2B;AAC/C,SAAA;AAAA,IAAEC,MAAM;AAAA,IAASD;AAAAA,EAAK;AAC/B;AAGO,SAASE,sBAAsBC,UAA+B;AAC5D,SAAA;AAAA,IAAEF,MAAM;AAAA,IAAkBE;AAAAA,EAAS;AAC5C;AAEgBC,SAAAA,cAAcC,GAAgBC,GAAyB;AACjED,MAAAA,EAAEJ,SAASK,EAAEL,MAAM;AACd,WAAA;AAAA,EAAA;AAET,MAAII,EAAEJ,SAAS,WAAWK,EAAEL,SAAS,SAAS;AACrCI,WAAAA,EAAEL,SAASM,EAAEN;AAAAA,EAAAA;AAEtB,MAAIK,EAAEJ,SAAS,oBAAoBK,EAAEL,SAAS,kBAAkB;AACvDI,WAAAA,EAAEF,aAAaG,EAAEH;AAAAA,EAAAA;AAEnB,SAAA;AACT;AAagBI,SAAAA,wBACdC,WACAC,SACS;;AACT,UAAQA,QAAQR,MAAI;AAAA,IAClB,KAAK;AAEDO,aAAAA,UAAUP,SAASS,KAAKC,qBACxBH,eAAUI,kBAAVJ,mBAAyBR,KAAKa,WAAUJ,QAA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g-value';
2
+ export { argValueToValueNode, scalarToValueNode, valueNodeToArgValue, } from './arg-value';
3
+ export { findOperation, fieldSegment, inlineFragmentSegment, segmentsEqual, type DefinitionTarget, type Path, type PathSegment, } from './ast-path';
4
+ export { isFieldSelected, toggleFieldSelection } from './field-selection';
5
+ export { getFieldArgValues, getFieldArgVariables, setFieldArgument, } from './arguments';
6
+ export { demoteVariable, promoteArgToVariable, suggestVarName, } from './variables';
7
+ export { createFragmentFromSelection, inlineFragment, listFragments, listFragmentInfos, removeFragmentSpread, renameFragment, type FragmentInfo, } from './fragments';
8
+ export { addInlineFragment, isInlineFragmentPresent, removeInlineFragment, } from './inline-fragments';
@@ -0,0 +1,30 @@
1
+ import { GraphQLField } from 'graphql';
2
+ /** Field lists longer than this are capped by default and gain a filter input. */
3
+ export declare const FIELD_LIST_THRESHOLD = 20;
4
+ /**
5
+ * Case-insensitive substring match over a field's name, description, and named
6
+ * type. An empty/whitespace query matches everything (treated as "no filter").
7
+ */
8
+ export declare function fieldMatchesFilter(field: GraphQLField<unknown, unknown>, query: string): boolean;
9
+ interface SelectVisibleFieldsInput {
10
+ fields: GraphQLField<unknown, unknown>[];
11
+ /** True when the field (by name) is already selected in the query at this path. */
12
+ isSelected: (fieldName: string) => boolean;
13
+ threshold: number;
14
+ /** True when the user clicked "N more" to reveal the full list. */
15
+ expanded: boolean;
16
+ /** Active filter text; empty string means no filter. */
17
+ filter: string;
18
+ }
19
+ interface VisibleFields {
20
+ visible: GraphQLField<unknown, unknown>[];
21
+ /** Count of fields hidden only by the cap (0 while filtering or expanded). */
22
+ hiddenCount: number;
23
+ }
24
+ /**
25
+ * Decide which fields of a list to render. A filter (when non-empty) bypasses the
26
+ * cap and shows all matches. Otherwise the first `threshold` fields show in schema
27
+ * order, plus any selected field beyond the cap (selected fields never hide).
28
+ */
29
+ export declare function selectVisibleFields({ fields, isSelected, threshold, expanded, filter, }: SelectVisibleFieldsInput): VisibleFields;
30
+ export {};
@@ -0,0 +1,47 @@
1
+ import { getNamedType } from "graphql";
2
+ const FIELD_LIST_THRESHOLD = 20;
3
+ function fieldMatchesFilter(field, query) {
4
+ var _a;
5
+ const q = query.trim().toLowerCase();
6
+ if (q === "") {
7
+ return true;
8
+ }
9
+ if (field.name.toLowerCase().includes(q)) {
10
+ return true;
11
+ }
12
+ if ((_a = field.description) == null ? void 0 : _a.toLowerCase().includes(q)) {
13
+ return true;
14
+ }
15
+ return getNamedType(field.type).name.toLowerCase().includes(q);
16
+ }
17
+ function selectVisibleFields({
18
+ fields,
19
+ isSelected,
20
+ threshold,
21
+ expanded,
22
+ filter
23
+ }) {
24
+ if (filter.trim() !== "") {
25
+ return {
26
+ visible: fields.filter((f) => fieldMatchesFilter(f, filter)),
27
+ hiddenCount: 0
28
+ };
29
+ }
30
+ if (expanded || fields.length <= threshold) {
31
+ return {
32
+ visible: fields,
33
+ hiddenCount: 0
34
+ };
35
+ }
36
+ const visible = fields.filter((field, index) => index < threshold || isSelected(field.name));
37
+ return {
38
+ visible,
39
+ hiddenCount: fields.length - visible.length
40
+ };
41
+ }
42
+ export {
43
+ FIELD_LIST_THRESHOLD,
44
+ fieldMatchesFilter,
45
+ selectVisibleFields
46
+ };
47
+ //# sourceMappingURL=field-list-view.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"field-list-view.js","sources":["../../src/lib/field-list-view.ts"],"sourcesContent":["import { getNamedType, type GraphQLField } from 'graphql';\n\n/** Field lists longer than this are capped by default and gain a filter input. */\nexport const FIELD_LIST_THRESHOLD = 20;\n\n/**\n * Case-insensitive substring match over a field's name, description, and named\n * type. An empty/whitespace query matches everything (treated as \"no filter\").\n */\nexport function fieldMatchesFilter(\n field: GraphQLField<unknown, unknown>,\n query: string,\n): boolean {\n const q = query.trim().toLowerCase();\n if (q === '') {\n return true;\n }\n if (field.name.toLowerCase().includes(q)) {\n return true;\n }\n if (field.description?.toLowerCase().includes(q)) {\n return true;\n }\n return getNamedType(field.type).name.toLowerCase().includes(q);\n}\n\ninterface SelectVisibleFieldsInput {\n fields: GraphQLField<unknown, unknown>[];\n /** True when the field (by name) is already selected in the query at this path. */\n isSelected: (fieldName: string) => boolean;\n threshold: number;\n /** True when the user clicked \"N more\" to reveal the full list. */\n expanded: boolean;\n /** Active filter text; empty string means no filter. */\n filter: string;\n}\n\ninterface VisibleFields {\n visible: GraphQLField<unknown, unknown>[];\n /** Count of fields hidden only by the cap (0 while filtering or expanded). */\n hiddenCount: number;\n}\n\n/**\n * Decide which fields of a list to render. A filter (when non-empty) bypasses the\n * cap and shows all matches. Otherwise the first `threshold` fields show in schema\n * order, plus any selected field beyond the cap (selected fields never hide).\n */\nexport function selectVisibleFields({\n fields,\n isSelected,\n threshold,\n expanded,\n filter,\n}: SelectVisibleFieldsInput): VisibleFields {\n if (filter.trim() !== '') {\n return {\n visible: fields.filter(f => fieldMatchesFilter(f, filter)),\n hiddenCount: 0,\n };\n }\n\n if (expanded || fields.length <= threshold) {\n return { visible: fields, hiddenCount: 0 };\n }\n\n const visible = fields.filter(\n (field, index) => index < threshold || isSelected(field.name),\n );\n return { visible, hiddenCount: fields.length - visible.length };\n}\n"],"names":["FIELD_LIST_THRESHOLD","fieldMatchesFilter","field","query","q","trim","toLowerCase","name","includes","description","getNamedType","type","selectVisibleFields","fields","isSelected","threshold","expanded","filter","visible","f","hiddenCount","length","index"],"mappings":";AAGO,MAAMA,uBAAuB;AAMpBC,SAAAA,mBACdC,OACAC,OACS;;AACT,QAAMC,IAAID,MAAME,KAAK,EAAEC,YAAY;AACnC,MAAIF,MAAM,IAAI;AACL,WAAA;AAAA,EAAA;AAET,MAAIF,MAAMK,KAAKD,YAAcE,EAAAA,SAASJ,CAAC,GAAG;AACjC,WAAA;AAAA,EAAA;AAET,OAAIF,WAAMO,gBAANP,mBAAmBI,cAAcE,SAASJ,IAAI;AACzC,WAAA;AAAA,EAAA;AAEFM,SAAAA,aAAaR,MAAMS,IAAI,EAAEJ,KAAKD,YAAY,EAAEE,SAASJ,CAAC;AAC/D;AAwBO,SAASQ,oBAAoB;AAAA,EAClCC;AAAAA,EACAC;AAAAA,EACAC;AAAAA,EACAC;AAAAA,EACAC;AACwB,GAAkB;AACtCA,MAAAA,OAAOZ,KAAK,MAAM,IAAI;AACjB,WAAA;AAAA,MACLa,SAASL,OAAOI,OAAOE,OAAKlB,mBAAmBkB,GAAGF,MAAM,CAAC;AAAA,MACzDG,aAAa;AAAA,IACf;AAAA,EAAA;AAGEJ,MAAAA,YAAYH,OAAOQ,UAAUN,WAAW;AACnC,WAAA;AAAA,MAAEG,SAASL;AAAAA,MAAQO,aAAa;AAAA,IAAE;AAAA,EAAA;AAGrCF,QAAAA,UAAUL,OAAOI,OACrB,CAACf,OAAOoB,UAAUA,QAAQP,aAAaD,WAAWZ,MAAMK,IAAI,CAC9D;AACO,SAAA;AAAA,IAAEW;AAAAA,IAASE,aAAaP,OAAOQ,SAASH,QAAQG;AAAAA,EAAO;AAChE;"}
@@ -0,0 +1,26 @@
1
+ import { DocumentNode, OperationDefinitionNode, SelectionSetNode } from 'graphql';
2
+ import { DefinitionTarget, PathSegment } from './ast-path';
3
+ /** Collects every variable name referenced anywhere within `selectionSet`. */
4
+ export declare function collectVariableReferences(selectionSet: SelectionSetNode, into: Set<string>): void;
5
+ /**
6
+ * Drops variable definitions that are no longer referenced by the operation,
7
+ * so removing the last field that used a promoted variable doesn't leave a
8
+ * dangling (invalid) `$var` in the operation header. Named fragments in `doc`
9
+ * are scanned too, so a variable used only through a fragment spread is kept.
10
+ */
11
+ export declare function pruneUnusedVariableDefinitions(operation: OperationDefinitionNode, doc: DocumentNode): OperationDefinitionNode;
12
+ /**
13
+ * Returns whether the field at the given path is present in the definition
14
+ * addressed by `target` (an operation or a named fragment) of `doc`.
15
+ */
16
+ export declare function isFieldSelected(doc: DocumentNode, path: PathSegment[], target: DefinitionTarget): boolean;
17
+ /**
18
+ * Toggles the field at `path` in the definition addressed by `target` (an
19
+ * operation or a named fragment) of `doc`:
20
+ * - If the field is already selected, removes it (along with its children).
21
+ * - If the field is not selected, adds it (creating intermediate selection
22
+ * sets as needed).
23
+ *
24
+ * Returns a new `DocumentNode`; does not mutate `doc`.
25
+ */
26
+ export declare function toggleFieldSelection(doc: DocumentNode, path: PathSegment[], target: DefinitionTarget): DocumentNode;
@@ -0,0 +1,76 @@
1
+ import { Kind, visit } from "graphql";
2
+ import { mapDefinition, findDefinition, findNodeAtPath, removeField, addField } from "./ast-path.js";
3
+ function collectVariableReferences(selectionSet, into) {
4
+ visit(selectionSet, {
5
+ Variable(node) {
6
+ into.add(node.name.value);
7
+ }
8
+ });
9
+ }
10
+ function pruneUnusedVariableDefinitions(operation, doc) {
11
+ const varDefs = operation.variableDefinitions;
12
+ if (!varDefs) {
13
+ return operation;
14
+ }
15
+ const used = /* @__PURE__ */ new Set();
16
+ collectVariableReferences(operation.selectionSet, used);
17
+ for (const def of doc.definitions) {
18
+ if (def.kind === Kind.FRAGMENT_DEFINITION) {
19
+ collectVariableReferences(def.selectionSet, used);
20
+ }
21
+ }
22
+ const kept = varDefs.filter((vd) => used.has(vd.variable.name.value));
23
+ if (kept.length === varDefs.length) {
24
+ return operation;
25
+ }
26
+ return {
27
+ ...operation,
28
+ variableDefinitions: kept
29
+ };
30
+ }
31
+ function isFieldSelected(doc, path, target) {
32
+ if (path.length === 0) {
33
+ return false;
34
+ }
35
+ const definition = findDefinition(doc, target);
36
+ if (!definition) {
37
+ return false;
38
+ }
39
+ return findNodeAtPath(definition.selectionSet, path) !== void 0;
40
+ }
41
+ function toggleFieldSelection(doc, path, target) {
42
+ if (path.length === 0) {
43
+ return doc;
44
+ }
45
+ const selected = isFieldSelected(doc, path, target);
46
+ return mapDefinition(doc, target, (definition) => {
47
+ const newSelectionSet = selected ? removeField(definition.selectionSet, path) : addField(definition.selectionSet, path);
48
+ if (newSelectionSet.selections.length === 0) {
49
+ if (definition.kind === Kind.OPERATION_DEFINITION) {
50
+ return null;
51
+ }
52
+ return definition;
53
+ }
54
+ if (definition.kind === Kind.FRAGMENT_DEFINITION) {
55
+ return {
56
+ ...definition,
57
+ selectionSet: newSelectionSet
58
+ };
59
+ }
60
+ let newOperation = {
61
+ ...definition,
62
+ selectionSet: newSelectionSet
63
+ };
64
+ if (selected) {
65
+ newOperation = pruneUnusedVariableDefinitions(newOperation, doc);
66
+ }
67
+ return newOperation;
68
+ });
69
+ }
70
+ export {
71
+ collectVariableReferences,
72
+ isFieldSelected,
73
+ pruneUnusedVariableDefinitions,
74
+ toggleFieldSelection
75
+ };
76
+ //# sourceMappingURL=field-selection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"field-selection.js","sources":["../../src/lib/field-selection.ts"],"sourcesContent":["import {\n Kind,\n visit,\n type DocumentNode,\n type OperationDefinitionNode,\n type SelectionSetNode,\n} from 'graphql';\n\nimport {\n addField,\n findDefinition,\n mapDefinition,\n findNodeAtPath,\n removeField,\n type DefinitionTarget,\n type PathSegment,\n} from './ast-path';\n\n/** Collects every variable name referenced anywhere within `selectionSet`. */\nexport function collectVariableReferences(\n selectionSet: SelectionSetNode,\n into: Set<string>,\n): void {\n visit(selectionSet, {\n Variable(node) {\n into.add(node.name.value);\n },\n });\n}\n\n/**\n * Drops variable definitions that are no longer referenced by the operation,\n * so removing the last field that used a promoted variable doesn't leave a\n * dangling (invalid) `$var` in the operation header. Named fragments in `doc`\n * are scanned too, so a variable used only through a fragment spread is kept.\n */\nexport function pruneUnusedVariableDefinitions(\n operation: OperationDefinitionNode,\n doc: DocumentNode,\n): OperationDefinitionNode {\n const varDefs = operation.variableDefinitions;\n if (!varDefs) {\n return operation;\n }\n\n const used = new Set<string>();\n collectVariableReferences(operation.selectionSet, used);\n for (const def of doc.definitions) {\n if (def.kind === Kind.FRAGMENT_DEFINITION) {\n collectVariableReferences(def.selectionSet, used);\n }\n }\n\n const kept = varDefs.filter(vd => used.has(vd.variable.name.value));\n if (kept.length === varDefs.length) {\n return operation;\n }\n return { ...operation, variableDefinitions: kept };\n}\n\n/**\n * Returns whether the field at the given path is present in the definition\n * addressed by `target` (an operation or a named fragment) of `doc`.\n */\nexport function isFieldSelected(\n doc: DocumentNode,\n path: PathSegment[],\n target: DefinitionTarget,\n): boolean {\n if (path.length === 0) {\n return false;\n }\n\n const definition = findDefinition(doc, target);\n if (!definition) {\n return false;\n }\n\n return findNodeAtPath(definition.selectionSet, path) !== undefined;\n}\n\n/**\n * Toggles the field at `path` in the definition addressed by `target` (an\n * operation or a named fragment) of `doc`:\n * - If the field is already selected, removes it (along with its children).\n * - If the field is not selected, adds it (creating intermediate selection\n * sets as needed).\n *\n * Returns a new `DocumentNode`; does not mutate `doc`.\n */\nexport function toggleFieldSelection(\n doc: DocumentNode,\n path: PathSegment[],\n target: DefinitionTarget,\n): DocumentNode {\n if (path.length === 0) {\n return doc;\n }\n\n const selected = isFieldSelected(doc, path, target);\n return mapDefinition(doc, target, definition => {\n const newSelectionSet = selected\n ? removeField(definition.selectionSet, path)\n : addField(definition.selectionSet, path);\n\n if (newSelectionSet.selections.length === 0) {\n if (definition.kind === Kind.OPERATION_DEFINITION) {\n // Removing the last field empties the operation. An operation with no\n // selection set is unprintable (it would fail to parse and leave the\n // builder out of sync), so drop the whole operation definition instead.\n return null;\n }\n // A fragment can't be emptied without becoming invalid and orphaning its\n // spreads, so removing its last field is a no-op. Deleting a fragment\n // outright is a separate action.\n return definition;\n }\n\n if (definition.kind === Kind.FRAGMENT_DEFINITION) {\n return { ...definition, selectionSet: newSelectionSet };\n }\n\n let newOperation: OperationDefinitionNode = {\n ...definition,\n selectionSet: newSelectionSet,\n };\n // Removing a field can orphan a promoted variable; drop any now-unused\n // variable definitions so the operation stays valid.\n if (selected) {\n newOperation = pruneUnusedVariableDefinitions(newOperation, doc);\n }\n return newOperation;\n });\n}\n"],"names":["collectVariableReferences","selectionSet","into","visit","Variable","node","add","name","value","pruneUnusedVariableDefinitions","operation","doc","varDefs","variableDefinitions","used","Set","def","definitions","kind","Kind","FRAGMENT_DEFINITION","kept","filter","vd","has","variable","length","isFieldSelected","path","target","definition","findDefinition","findNodeAtPath","undefined","toggleFieldSelection","selected","mapDefinition","newSelectionSet","removeField","addField","selections","OPERATION_DEFINITION","newOperation"],"mappings":";;AAmBgBA,SAAAA,0BACdC,cACAC,MACM;AACNC,QAAMF,cAAc;AAAA,IAClBG,SAASC,MAAM;AACRC,WAAAA,IAAID,KAAKE,KAAKC,KAAK;AAAA,IAAA;AAAA,EAC1B,CACD;AACH;AAQgBC,SAAAA,+BACdC,WACAC,KACyB;AACzB,QAAMC,UAAUF,UAAUG;AAC1B,MAAI,CAACD,SAAS;AACLF,WAAAA;AAAAA,EAAAA;AAGHI,QAAAA,2BAAWC,IAAY;AACHL,4BAAAA,UAAUT,cAAca,IAAI;AAC3CE,aAAAA,OAAOL,IAAIM,aAAa;AAC7BD,QAAAA,IAAIE,SAASC,KAAKC,qBAAqB;AACfJ,gCAAAA,IAAIf,cAAca,IAAI;AAAA,IAAA;AAAA,EAClD;AAGIO,QAAAA,OAAOT,QAAQU,OAAOC,CAAMT,OAAAA,KAAKU,IAAID,GAAGE,SAASlB,KAAKC,KAAK,CAAC;AAC9Da,MAAAA,KAAKK,WAAWd,QAAQc,QAAQ;AAC3BhB,WAAAA;AAAAA,EAAAA;AAEF,SAAA;AAAA,IAAE,GAAGA;AAAAA,IAAWG,qBAAqBQ;AAAAA,EAAK;AACnD;AAMgBM,SAAAA,gBACdhB,KACAiB,MACAC,QACS;AACLD,MAAAA,KAAKF,WAAW,GAAG;AACd,WAAA;AAAA,EAAA;AAGHI,QAAAA,aAAaC,eAAepB,KAAKkB,MAAM;AAC7C,MAAI,CAACC,YAAY;AACR,WAAA;AAAA,EAAA;AAGT,SAAOE,eAAeF,WAAW7B,cAAc2B,IAAI,MAAMK;AAC3D;AAWgBC,SAAAA,qBACdvB,KACAiB,MACAC,QACc;AACVD,MAAAA,KAAKF,WAAW,GAAG;AACdf,WAAAA;AAAAA,EAAAA;AAGT,QAAMwB,WAAWR,gBAAgBhB,KAAKiB,MAAMC,MAAM;AAC3CO,SAAAA,cAAczB,KAAKkB,QAAQC,CAAc,eAAA;AACxCO,UAAAA,kBAAkBF,WACpBG,YAAYR,WAAW7B,cAAc2B,IAAI,IACzCW,SAAST,WAAW7B,cAAc2B,IAAI;AAEtCS,QAAAA,gBAAgBG,WAAWd,WAAW,GAAG;AACvCI,UAAAA,WAAWZ,SAASC,KAAKsB,sBAAsB;AAI1C,eAAA;AAAA,MAAA;AAKFX,aAAAA;AAAAA,IAAAA;AAGLA,QAAAA,WAAWZ,SAASC,KAAKC,qBAAqB;AACzC,aAAA;AAAA,QAAE,GAAGU;AAAAA,QAAY7B,cAAcoC;AAAAA,MAAgB;AAAA,IAAA;AAGxD,QAAIK,eAAwC;AAAA,MAC1C,GAAGZ;AAAAA,MACH7B,cAAcoC;AAAAA,IAChB;AAGA,QAAIF,UAAU;AACG1B,qBAAAA,+BAA+BiC,cAAc/B,GAAG;AAAA,IAAA;AAE1D+B,WAAAA;AAAAA,EAAAA,CACR;AACH;"}
@@ -0,0 +1,63 @@
1
+ import { DocumentNode } from 'graphql';
2
+ import { DefinitionTarget, PathSegment } from './ast-path';
3
+ /**
4
+ * Returns the names of all named fragment definitions in `doc`, in document
5
+ * order.
6
+ */
7
+ export declare function listFragments(doc: DocumentNode): string[];
8
+ /** A named fragment's name paired with its type condition. */
9
+ export type FragmentInfo = {
10
+ name: string;
11
+ typeName: string;
12
+ };
13
+ /**
14
+ * Returns the name and type condition of every named fragment definition in
15
+ * `doc`, in document order.
16
+ */
17
+ export declare function listFragmentInfos(doc: DocumentNode): FragmentInfo[];
18
+ /**
19
+ * Extracts the selection set of the field at `path` into a new named fragment
20
+ * definition (`fragmentName on typeName`), and replaces the field's selections
21
+ * with a single fragment spread (`...fragmentName`).
22
+ *
23
+ * Returns a new `DocumentNode`; does not mutate `doc`. Returns the original
24
+ * document unchanged when the path does not resolve to a field with a
25
+ * selection set.
26
+ */
27
+ export declare function createFragmentFromSelection(doc: DocumentNode, path: PathSegment[], fragmentName: string, typeName: string, target: DefinitionTarget): DocumentNode;
28
+ /**
29
+ * Removes the `...fragmentName` spread from the selection set of the field at
30
+ * `fieldPath` (or the definition root when `fieldPath` is empty) in the
31
+ * definition addressed by `target`. If that leaves the field with no
32
+ * selections, the field itself is pruned; if it empties an operation, the
33
+ * operation is dropped.
34
+ *
35
+ * Returns a new `DocumentNode`; does not mutate `doc`. Returns the original
36
+ * document unchanged when no such spread is present.
37
+ */
38
+ export declare function removeFragmentSpread(doc: DocumentNode, fieldPath: PathSegment[], fragmentName: string, target: DefinitionTarget): DocumentNode;
39
+ /**
40
+ * Deletes the named fragment, inlining it: every `...fragmentName` spread
41
+ * across the document (operations and other fragments alike) is replaced in
42
+ * place by the fragment's own selections, and the fragment definition is
43
+ * removed.
44
+ *
45
+ * Assumes each spread site is type-compatible with the fragment's type
46
+ * condition — the builder only creates spreads on matching-type fields — so the
47
+ * selections are inlined directly rather than wrapped in an `... on Type`
48
+ * inline fragment. Overlapping selections are left as-is (GraphQL merges
49
+ * identically-named fields).
50
+ *
51
+ * Returns a new `DocumentNode`; does not mutate `doc`. Returns the original
52
+ * document unchanged when no fragment named `fragmentName` exists.
53
+ */
54
+ export declare function inlineFragment(doc: DocumentNode, fragmentName: string): DocumentNode;
55
+ /**
56
+ * Renames the named fragment `oldName` to `newName`, updating the definition
57
+ * and every `...oldName` spread across the document (operations and other
58
+ * fragments alike). Returns the original document unchanged when no fragment
59
+ * named `oldName` exists or when `newName` is empty or already taken.
60
+ *
61
+ * Returns a new `DocumentNode`; does not mutate `doc`.
62
+ */
63
+ export declare function renameFragment(doc: DocumentNode, oldName: string, newName: string): DocumentNode;