@graphiql/plugin-query-builder 1.0.0-beta.0 → 1.0.0-beta.2
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.
- package/LICENSE +21 -0
- package/dist/components/arg-input/arg-input-by-type.js +441 -444
- package/dist/components/arg-input/arg-input-by-type.js.map +1 -1
- package/dist/components/arg-input/enum-arg-control.js +49 -52
- package/dist/components/arg-input/enum-arg-control.js.map +1 -1
- package/dist/components/arg-input/index.js +44 -36
- package/dist/components/arg-input/index.js.map +1 -1
- package/dist/components/arg-input/scalar-arg-control.js +58 -63
- package/dist/components/arg-input/scalar-arg-control.js.map +1 -1
- package/dist/components/field-row.js +212 -197
- package/dist/components/field-row.js.map +1 -1
- package/dist/components/field-tree-context.js +23 -27
- package/dist/components/field-tree-context.js.map +1 -1
- package/dist/components/field-tree-node.js +128 -105
- package/dist/components/field-tree-node.js.map +1 -1
- package/dist/components/field-tree.js +178 -199
- package/dist/components/field-tree.js.map +1 -1
- package/dist/components/fragment-editor.js +157 -151
- package/dist/components/fragment-editor.js.map +1 -1
- package/dist/components/fragment-section.js +215 -203
- package/dist/components/fragment-section.js.map +1 -1
- package/dist/components/query-builder.js +205 -181
- package/dist/components/query-builder.js.map +1 -1
- package/dist/components/type-condition-selector.js +417 -462
- package/dist/components/type-condition-selector.js.map +1 -1
- package/dist/components/use-cursor-path.js +69 -70
- package/dist/components/use-cursor-path.js.map +1 -1
- package/dist/components/use-cursor-reveal.js +83 -89
- package/dist/components/use-cursor-reveal.js.map +1 -1
- package/dist/components/use-working-document.js +432 -527
- package/dist/components/use-working-document.js.map +1 -1
- package/dist/index.js +11 -11
- package/dist/index.js.map +1 -1
- package/dist/lib/arg-value.js +132 -139
- package/dist/lib/arg-value.js.map +1 -1
- package/dist/lib/arguments.js +54 -55
- package/dist/lib/arguments.js.map +1 -1
- package/dist/lib/ast-path.js +266 -291
- package/dist/lib/ast-path.js.map +1 -1
- package/dist/lib/field-list-view.js +33 -43
- package/dist/lib/field-list-view.js.map +1 -1
- package/dist/lib/field-selection.js +63 -67
- package/dist/lib/field-selection.js.map +1 -1
- package/dist/lib/fragments.js +166 -152
- package/dist/lib/fragments.js.map +1 -1
- package/dist/lib/inline-fragments.js +64 -62
- package/dist/lib/inline-fragments.js.map +1 -1
- package/dist/lib/schema-walk.js +141 -158
- package/dist/lib/schema-walk.js.map +1 -1
- package/dist/lib/variables.js +96 -90
- package/dist/lib/variables.js.map +1 -1
- package/dist/style.css +4 -22
- package/package.json +21 -18
package/dist/lib/ast-path.js
CHANGED
|
@@ -1,330 +1,305 @@
|
|
|
1
1
|
import { Kind } from "graphql";
|
|
2
|
+
//#region src/lib/ast-path.ts
|
|
2
3
|
function fieldSegment(name) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
return {
|
|
5
|
+
kind: "field",
|
|
6
|
+
name
|
|
7
|
+
};
|
|
7
8
|
}
|
|
9
|
+
/** Builds the path segment that addresses the `... on TypeName` inline fragment. */
|
|
8
10
|
function inlineFragmentSegment(typeName) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
return {
|
|
12
|
+
kind: "inlineFragment",
|
|
13
|
+
typeName
|
|
14
|
+
};
|
|
13
15
|
}
|
|
14
16
|
function segmentsEqual(a, b) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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;
|
|
17
|
+
if (a.kind !== b.kind) return false;
|
|
18
|
+
if (a.kind === "field" && b.kind === "field") return a.name === b.name;
|
|
19
|
+
if (a.kind === "inlineFragment" && b.kind === "inlineFragment") return a.typeName === b.typeName;
|
|
20
|
+
return false;
|
|
25
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* True when `selection` is the field or inline fragment named by `segment`.
|
|
24
|
+
*
|
|
25
|
+
* Fields are matched by schema name, not response key: the builder's tree is
|
|
26
|
+
* driven by the schema, so a path segment is a field name and never carries an
|
|
27
|
+
* alias. A document the user hand-aliased (`a: hero b: hero`) therefore matches
|
|
28
|
+
* the first selection with that name. Helpers that walk to a single node act
|
|
29
|
+
* on that first match only (see `mapNodeAtPath`), so unchecking an aliased
|
|
30
|
+
* field removes one occurrence rather than wiping every alias of it. Full
|
|
31
|
+
* alias-aware editing would require a selection-driven tree and is out of scope.
|
|
32
|
+
*/
|
|
26
33
|
function selectionMatchesSegment(selection, segment) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
case "field":
|
|
32
|
-
return selection.kind === Kind.FIELD && selection.name.value === segment.name;
|
|
33
|
-
}
|
|
34
|
+
switch (segment.kind) {
|
|
35
|
+
case "inlineFragment": return selection.kind === Kind.INLINE_FRAGMENT && selection.typeCondition?.name.value === segment.typeName;
|
|
36
|
+
case "field": return selection.kind === Kind.FIELD && selection.name.value === segment.name;
|
|
37
|
+
}
|
|
34
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Walks `path` through `selectionSet` via `selectionMatchesSegment` and
|
|
41
|
+
* returns the node at the final segment, or `undefined` if any segment is
|
|
42
|
+
* missing.
|
|
43
|
+
*/
|
|
35
44
|
function findNodeAtPath(selectionSet, path) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
if (!node.selectionSet) {
|
|
47
|
-
return void 0;
|
|
48
|
-
}
|
|
49
|
-
current = node.selectionSet;
|
|
50
|
-
}
|
|
51
|
-
return void 0;
|
|
45
|
+
let current = selectionSet;
|
|
46
|
+
for (let i = 0; i < path.length; i++) {
|
|
47
|
+
const segment = path[i];
|
|
48
|
+
const node = current.selections.find((s) => selectionMatchesSegment(s, segment));
|
|
49
|
+
if (!node) return;
|
|
50
|
+
if (i === path.length - 1) return node;
|
|
51
|
+
if (!node.selectionSet) return;
|
|
52
|
+
current = node.selectionSet;
|
|
53
|
+
}
|
|
52
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Immutably rebuilds `selectionSet`, descending to the node at `path` via
|
|
57
|
+
* `selectionMatchesSegment`, then applying `fn` to it. `fn` returns the
|
|
58
|
+
* replacement node, or `undefined` to delete it. When `pruneEmptyParent` is
|
|
59
|
+
* true (default), a non-leaf ancestor whose child selection set becomes empty
|
|
60
|
+
* after the operation is itself pruned. Returns the original `selectionSet`
|
|
61
|
+
* reference unchanged when `path` does not resolve.
|
|
62
|
+
*/
|
|
53
63
|
function mapNodeAtPath(selectionSet, path, fn, pruneEmptyParent = true) {
|
|
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
|
-
...node,
|
|
89
|
-
selectionSet: newChildSet
|
|
90
|
-
};
|
|
91
|
-
const newSelections = [...selectionSet.selections];
|
|
92
|
-
newSelections[nodeIndex] = updated;
|
|
93
|
-
return {
|
|
94
|
-
...selectionSet,
|
|
95
|
-
selections: newSelections
|
|
96
|
-
};
|
|
64
|
+
const [segment, ...rest] = path;
|
|
65
|
+
const nodeIndex = selectionSet.selections.findIndex((s) => selectionMatchesSegment(s, segment));
|
|
66
|
+
if (nodeIndex === -1) return selectionSet;
|
|
67
|
+
const node = selectionSet.selections[nodeIndex];
|
|
68
|
+
if (rest.length === 0) {
|
|
69
|
+
const replacement = fn(node);
|
|
70
|
+
const newSelections = [...selectionSet.selections];
|
|
71
|
+
if (replacement === null) newSelections.splice(nodeIndex, 1);
|
|
72
|
+
else newSelections[nodeIndex] = replacement;
|
|
73
|
+
return {
|
|
74
|
+
...selectionSet,
|
|
75
|
+
selections: newSelections
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
if (!node.selectionSet) return selectionSet;
|
|
79
|
+
const newChildSet = mapNodeAtPath(node.selectionSet, rest, fn, pruneEmptyParent);
|
|
80
|
+
if (newChildSet === node.selectionSet) return selectionSet;
|
|
81
|
+
if (pruneEmptyParent && newChildSet.selections.length === 0) {
|
|
82
|
+
const newSelections = selectionSet.selections.filter((_, i) => i !== nodeIndex);
|
|
83
|
+
return {
|
|
84
|
+
...selectionSet,
|
|
85
|
+
selections: newSelections
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
const updated = {
|
|
89
|
+
...node,
|
|
90
|
+
selectionSet: newChildSet
|
|
91
|
+
};
|
|
92
|
+
const newSelections = [...selectionSet.selections];
|
|
93
|
+
newSelections[nodeIndex] = updated;
|
|
94
|
+
return {
|
|
95
|
+
...selectionSet,
|
|
96
|
+
selections: newSelections
|
|
97
|
+
};
|
|
97
98
|
}
|
|
99
|
+
/** Creates the field or inline fragment for `segment`, with the given child set. */
|
|
98
100
|
function createSelectionForSegment(segment, selectionSet) {
|
|
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
|
-
}
|
|
101
|
+
switch (segment.kind) {
|
|
102
|
+
case "inlineFragment": return {
|
|
103
|
+
kind: Kind.INLINE_FRAGMENT,
|
|
104
|
+
typeCondition: {
|
|
105
|
+
kind: Kind.NAMED_TYPE,
|
|
106
|
+
name: {
|
|
107
|
+
kind: Kind.NAME,
|
|
108
|
+
value: segment.typeName
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
directives: [],
|
|
112
|
+
selectionSet: selectionSet ?? {
|
|
113
|
+
kind: Kind.SELECTION_SET,
|
|
114
|
+
selections: []
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
case "field": 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
128
|
}
|
|
129
129
|
function addField(selectionSet, path) {
|
|
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
|
-
return {
|
|
166
|
-
...selectionSet,
|
|
167
|
-
selections: newSelections
|
|
168
|
-
};
|
|
130
|
+
const [segment, ...rest] = path;
|
|
131
|
+
if (rest.length === 0) {
|
|
132
|
+
if (selectionSet.selections.some((s) => selectionMatchesSegment(s, segment))) return selectionSet;
|
|
133
|
+
const newNode = createSelectionForSegment(segment);
|
|
134
|
+
return {
|
|
135
|
+
...selectionSet,
|
|
136
|
+
selections: [...selectionSet.selections, newNode]
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
const existingIndex = selectionSet.selections.findIndex((s) => selectionMatchesSegment(s, segment));
|
|
140
|
+
if (existingIndex === -1) {
|
|
141
|
+
const parentNode = createSelectionForSegment(segment, addField({
|
|
142
|
+
kind: Kind.SELECTION_SET,
|
|
143
|
+
selections: []
|
|
144
|
+
}, rest));
|
|
145
|
+
return {
|
|
146
|
+
...selectionSet,
|
|
147
|
+
selections: [...selectionSet.selections, parentNode]
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
const existing = selectionSet.selections[existingIndex];
|
|
151
|
+
const childSet = existing.selectionSet ?? {
|
|
152
|
+
kind: Kind.SELECTION_SET,
|
|
153
|
+
selections: []
|
|
154
|
+
};
|
|
155
|
+
const updated = {
|
|
156
|
+
...existing,
|
|
157
|
+
selectionSet: addField(childSet, rest)
|
|
158
|
+
};
|
|
159
|
+
const newSelections = [...selectionSet.selections];
|
|
160
|
+
newSelections[existingIndex] = updated;
|
|
161
|
+
return {
|
|
162
|
+
...selectionSet,
|
|
163
|
+
selections: newSelections
|
|
164
|
+
};
|
|
169
165
|
}
|
|
170
166
|
function removeField(selectionSet, path) {
|
|
171
|
-
|
|
167
|
+
return mapNodeAtPath(selectionSet, path, () => null, true);
|
|
172
168
|
}
|
|
173
169
|
function replaceFieldSelectionSet(selectionSet, path, newSet) {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
170
|
+
return mapNodeAtPath(selectionSet, path, (node) => ({
|
|
171
|
+
...node,
|
|
172
|
+
selectionSet: newSet
|
|
173
|
+
}), false);
|
|
178
174
|
}
|
|
179
175
|
function setArgInSelectionSet(selectionSet, path, argName, value) {
|
|
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
|
-
return {
|
|
207
|
-
...field,
|
|
208
|
-
arguments: newArgs
|
|
209
|
-
};
|
|
210
|
-
},
|
|
211
|
-
false
|
|
212
|
-
// do not prune empty parents when setting args
|
|
213
|
-
);
|
|
176
|
+
return mapNodeAtPath(selectionSet, path, (node) => {
|
|
177
|
+
const field = node;
|
|
178
|
+
const existingArgs = field.arguments ?? [];
|
|
179
|
+
let newArgs;
|
|
180
|
+
if (value === void 0) newArgs = existingArgs.filter((a) => a.name.value !== argName);
|
|
181
|
+
else {
|
|
182
|
+
const existingIndex = existingArgs.findIndex((a) => a.name.value === argName);
|
|
183
|
+
const newArg = {
|
|
184
|
+
kind: Kind.ARGUMENT,
|
|
185
|
+
name: {
|
|
186
|
+
kind: Kind.NAME,
|
|
187
|
+
value: argName
|
|
188
|
+
},
|
|
189
|
+
value
|
|
190
|
+
};
|
|
191
|
+
if (existingIndex === -1) newArgs = [...existingArgs, newArg];
|
|
192
|
+
else {
|
|
193
|
+
newArgs = [...existingArgs];
|
|
194
|
+
newArgs[existingIndex] = newArg;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
...field,
|
|
199
|
+
arguments: newArgs
|
|
200
|
+
};
|
|
201
|
+
}, false);
|
|
214
202
|
}
|
|
215
203
|
function findSelectionSet(selectionSet, path) {
|
|
216
|
-
|
|
217
|
-
return node == null ? void 0 : node.selectionSet;
|
|
204
|
+
return findNodeAtPath(selectionSet, path)?.selectionSet;
|
|
218
205
|
}
|
|
206
|
+
/**
|
|
207
|
+
* Returns the index in `doc.definitions` of the operation to operate on:
|
|
208
|
+
* the operation whose name matches `operationName` when given and present,
|
|
209
|
+
* otherwise the first operation definition. Returns -1 when the document has
|
|
210
|
+
* no operation definitions.
|
|
211
|
+
*/
|
|
219
212
|
function findOperationIndex(doc, operationName) {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
if (named !== -1) {
|
|
226
|
-
return named;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
return doc.definitions.findIndex((d) => d.kind === Kind.OPERATION_DEFINITION);
|
|
213
|
+
if (operationName) {
|
|
214
|
+
const named = doc.definitions.findIndex((d) => d.kind === Kind.OPERATION_DEFINITION && d.name?.value === operationName);
|
|
215
|
+
if (named !== -1) return named;
|
|
216
|
+
}
|
|
217
|
+
return doc.definitions.findIndex((d) => d.kind === Kind.OPERATION_DEFINITION);
|
|
230
218
|
}
|
|
219
|
+
/**
|
|
220
|
+
* Resolves the target operation node (see {@link findOperationIndex}), or
|
|
221
|
+
* `undefined` when the document has no matching operation.
|
|
222
|
+
*/
|
|
231
223
|
function findOperation(doc, operationName) {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
const operation = doc.definitions[index];
|
|
237
|
-
return (operation == null ? void 0 : operation.kind) === Kind.OPERATION_DEFINITION ? operation : void 0;
|
|
224
|
+
const index = findOperationIndex(doc, operationName);
|
|
225
|
+
if (index === -1) return;
|
|
226
|
+
const operation = doc.definitions[index];
|
|
227
|
+
return operation?.kind === Kind.OPERATION_DEFINITION ? operation : void 0;
|
|
238
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* Finds the target operation, calls `fn` with it, and returns a new
|
|
231
|
+
* `DocumentNode` with the result spliced back into `definitions`. When `fn`
|
|
232
|
+
* returns `null` the operation is removed entirely (used when an operation
|
|
233
|
+
* becomes empty and unprintable). Returns `doc` unchanged when no matching
|
|
234
|
+
* operation definition exists.
|
|
235
|
+
*/
|
|
239
236
|
function mapOperation(doc, operationName, fn) {
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
if (result === null) {
|
|
254
|
-
newDefinitions.splice(index, 1);
|
|
255
|
-
} else {
|
|
256
|
-
newDefinitions[index] = result;
|
|
257
|
-
}
|
|
258
|
-
return {
|
|
259
|
-
...doc,
|
|
260
|
-
definitions: newDefinitions
|
|
261
|
-
};
|
|
237
|
+
const index = findOperationIndex(doc, operationName);
|
|
238
|
+
if (index === -1) return doc;
|
|
239
|
+
const def = doc.definitions[index];
|
|
240
|
+
if (def?.kind !== Kind.OPERATION_DEFINITION) return doc;
|
|
241
|
+
const result = fn(def);
|
|
242
|
+
if (result === def) return doc;
|
|
243
|
+
const newDefinitions = [...doc.definitions];
|
|
244
|
+
if (result === null) newDefinitions.splice(index, 1);
|
|
245
|
+
else newDefinitions[index] = result;
|
|
246
|
+
return {
|
|
247
|
+
...doc,
|
|
248
|
+
definitions: newDefinitions
|
|
249
|
+
};
|
|
262
250
|
}
|
|
251
|
+
/**
|
|
252
|
+
* Returns the index in `doc.definitions` of the definition addressed by
|
|
253
|
+
* `target` — the matching operation (see {@link findOperationIndex}) or the
|
|
254
|
+
* named fragment. Returns -1 when no such definition exists.
|
|
255
|
+
*/
|
|
263
256
|
function findDefinitionIndex(doc, target) {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
}
|
|
267
|
-
return doc.definitions.findIndex((d) => d.kind === Kind.FRAGMENT_DEFINITION && d.name.value === target.name);
|
|
257
|
+
if (target.kind === "operation") return findOperationIndex(doc, target.name);
|
|
258
|
+
return doc.definitions.findIndex((d) => d.kind === Kind.FRAGMENT_DEFINITION && d.name.value === target.name);
|
|
268
259
|
}
|
|
260
|
+
/**
|
|
261
|
+
* Resolves the definition node addressed by `target`, or `undefined` when the
|
|
262
|
+
* document has no matching definition.
|
|
263
|
+
*/
|
|
269
264
|
function findDefinition(doc, target) {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
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;
|
|
265
|
+
const index = findDefinitionIndex(doc, target);
|
|
266
|
+
if (index === -1) return;
|
|
267
|
+
const def = doc.definitions[index];
|
|
268
|
+
if (def?.kind === Kind.OPERATION_DEFINITION || def?.kind === Kind.FRAGMENT_DEFINITION) return def;
|
|
279
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* Finds the definition addressed by `target`, calls `fn` with it, and returns a
|
|
272
|
+
* new `DocumentNode` with the result spliced back into `definitions`. When `fn`
|
|
273
|
+
* returns `null` the definition is removed entirely. Returns `doc` unchanged
|
|
274
|
+
* when no matching definition exists or `fn` returns the same reference.
|
|
275
|
+
*/
|
|
280
276
|
function mapDefinition(doc, target, fn) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
if (result === null) {
|
|
295
|
-
newDefinitions.splice(index, 1);
|
|
296
|
-
} else {
|
|
297
|
-
newDefinitions[index] = result;
|
|
298
|
-
}
|
|
299
|
-
return {
|
|
300
|
-
...doc,
|
|
301
|
-
definitions: newDefinitions
|
|
302
|
-
};
|
|
277
|
+
const index = findDefinitionIndex(doc, target);
|
|
278
|
+
if (index === -1) return doc;
|
|
279
|
+
const def = doc.definitions[index];
|
|
280
|
+
if (def?.kind !== Kind.OPERATION_DEFINITION && def?.kind !== Kind.FRAGMENT_DEFINITION) return doc;
|
|
281
|
+
const result = fn(def);
|
|
282
|
+
if (result === def) return doc;
|
|
283
|
+
const newDefinitions = [...doc.definitions];
|
|
284
|
+
if (result === null) newDefinitions.splice(index, 1);
|
|
285
|
+
else newDefinitions[index] = result;
|
|
286
|
+
return {
|
|
287
|
+
...doc,
|
|
288
|
+
definitions: newDefinitions
|
|
289
|
+
};
|
|
303
290
|
}
|
|
291
|
+
/**
|
|
292
|
+
* A source offset just inside the selection set of the definition addressed by
|
|
293
|
+
* `target`, or `undefined` when it can't be located. Used to move the editor
|
|
294
|
+
* cursor into a definition so the builder — which follows the cursor — switches
|
|
295
|
+
* to editing it. Requires `doc` to carry location info (parse without
|
|
296
|
+
* `noLocation`).
|
|
297
|
+
*/
|
|
304
298
|
function definitionSelectionOffset(doc, target) {
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
return loc ? loc.start + 1 : void 0;
|
|
299
|
+
const loc = findDefinition(doc, target)?.selectionSet.loc;
|
|
300
|
+
return loc ? loc.start + 1 : void 0;
|
|
308
301
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
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
|
|
302
|
+
//#endregion
|
|
303
|
+
export { addField, createSelectionForSegment, definitionSelectionOffset, fieldSegment, findDefinition, findDefinitionIndex, findNodeAtPath, findOperation, findOperationIndex, findSelectionSet, inlineFragmentSegment, mapDefinition, mapNodeAtPath, mapOperation, removeField, replaceFieldSelectionSet, segmentsEqual, selectionMatchesSegment, setArgInSelectionSet };
|
|
304
|
+
|
|
305
|
+
//# sourceMappingURL=ast-path.js.map
|