@graphiql/plugin-query-builder 1.0.0-beta.1 → 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/schema-walk.js
CHANGED
|
@@ -1,177 +1,160 @@
|
|
|
1
|
-
import { visit, Kind, OperationTypeNode, isObjectType, isInterfaceType, getNamedType } from "graphql";
|
|
2
1
|
import { fieldSegment, inlineFragmentSegment } from "./ast-path.js";
|
|
2
|
+
import { Kind, OperationTypeNode, getNamedType, isInterfaceType, isObjectType, visit } from "graphql";
|
|
3
|
+
//#region src/lib/schema-walk.ts
|
|
4
|
+
/**
|
|
5
|
+
* Safely parse the variables JSON text, returning a plain object or {} on
|
|
6
|
+
* failure. Arrays and non-object values are treated as empty.
|
|
7
|
+
*/
|
|
3
8
|
function readVariables(text) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return {};
|
|
12
|
-
}
|
|
9
|
+
if (!text || !text.trim()) return {};
|
|
10
|
+
try {
|
|
11
|
+
const parsed = JSON.parse(text);
|
|
12
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
|
13
|
+
} catch {
|
|
14
|
+
return {};
|
|
15
|
+
}
|
|
13
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Returns the tree path of the selection whose source range contains `offset`,
|
|
19
|
+
* walking from the operation that contains the cursor down through nested
|
|
20
|
+
* selection sets. Inline fragments contribute an `inlineFragment` path segment
|
|
21
|
+
* (matching the builder's path model), so the cursor resolves to fields nested
|
|
22
|
+
* inside union/interface type conditions too. Returns `[]` when the cursor
|
|
23
|
+
* isn't inside a field or fragment.
|
|
24
|
+
*/
|
|
14
25
|
function pathWithinSelections(rootSelections, offset) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
path.push(inlineFragmentSegment(typeName));
|
|
31
|
-
}
|
|
32
|
-
if (!selection.selectionSet) {
|
|
33
|
-
break;
|
|
34
|
-
}
|
|
35
|
-
selections = selection.selectionSet.selections;
|
|
36
|
-
}
|
|
37
|
-
return path;
|
|
26
|
+
const path = [];
|
|
27
|
+
let selections = rootSelections;
|
|
28
|
+
while (true) {
|
|
29
|
+
const selection = selections.find((s) => (s.kind === Kind.FIELD || s.kind === Kind.INLINE_FRAGMENT) && s.loc != null && s.loc.start <= offset && s.loc.end >= offset);
|
|
30
|
+
if (!selection) break;
|
|
31
|
+
if (selection.kind === Kind.FIELD) path.push(fieldSegment(selection.name.value));
|
|
32
|
+
else {
|
|
33
|
+
const typeName = selection.typeCondition?.name.value;
|
|
34
|
+
if (!typeName) break;
|
|
35
|
+
path.push(inlineFragmentSegment(typeName));
|
|
36
|
+
}
|
|
37
|
+
if (!selection.selectionSet) break;
|
|
38
|
+
selections = selection.selectionSet.selections;
|
|
39
|
+
}
|
|
40
|
+
return path;
|
|
38
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Resolves the definition the cursor sits in — an operation or a named
|
|
44
|
+
* fragment — together with the field path within it. Returns `undefined` when
|
|
45
|
+
* the cursor is not inside any editable definition. Drives cursor-following:
|
|
46
|
+
* moving the editor cursor into a fragment switches the builder to editing it.
|
|
47
|
+
*/
|
|
39
48
|
function cursorContextAtOffset(doc, offset) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
name: def.name.value
|
|
56
|
-
},
|
|
57
|
-
path: pathWithinSelections(def.selectionSet.selections, offset)
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
return void 0;
|
|
49
|
+
const def = doc.definitions.find((d) => (d.kind === Kind.OPERATION_DEFINITION || d.kind === Kind.FRAGMENT_DEFINITION) && d.loc != null && d.loc.start <= offset && d.loc.end >= offset);
|
|
50
|
+
if (def?.kind === Kind.OPERATION_DEFINITION) return {
|
|
51
|
+
target: {
|
|
52
|
+
kind: "operation",
|
|
53
|
+
name: def.name?.value
|
|
54
|
+
},
|
|
55
|
+
path: pathWithinSelections(def.selectionSet.selections, offset)
|
|
56
|
+
};
|
|
57
|
+
if (def?.kind === Kind.FRAGMENT_DEFINITION) return {
|
|
58
|
+
target: {
|
|
59
|
+
kind: "fragment",
|
|
60
|
+
name: def.name.value
|
|
61
|
+
},
|
|
62
|
+
path: pathWithinSelections(def.selectionSet.selections, offset)
|
|
63
|
+
};
|
|
61
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Resolves the operation to read from: the one named `operationName` when given
|
|
67
|
+
* and present, otherwise the first operation definition.
|
|
68
|
+
*/
|
|
62
69
|
function findOperationDefinition(doc, operationName) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return d.kind === Kind.OPERATION_DEFINITION && ((_a = d.name) == null ? void 0 : _a.value) === operationName;
|
|
66
|
-
}) : void 0;
|
|
67
|
-
const op = byName ?? doc.definitions.find((d) => d.kind === Kind.OPERATION_DEFINITION);
|
|
68
|
-
return (op == null ? void 0 : op.kind) === Kind.OPERATION_DEFINITION ? op : void 0;
|
|
70
|
+
const op = (operationName ? doc.definitions.find((d) => d.kind === Kind.OPERATION_DEFINITION && d.name?.value === operationName) : void 0) ?? doc.definitions.find((d) => d.kind === Kind.OPERATION_DEFINITION);
|
|
71
|
+
return op?.kind === Kind.OPERATION_DEFINITION ? op : void 0;
|
|
69
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Matches a path segment against a selection, by schema field name or, for an
|
|
75
|
+
* inline-fragment segment, by type condition. Mirrors the document-mutator path
|
|
76
|
+
* model so the cursor/path helpers agree with it.
|
|
77
|
+
*/
|
|
70
78
|
function selectionMatchesSegment(selection, segment) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
case "field":
|
|
76
|
-
return selection.kind === Kind.FIELD && selection.name.value === segment.name;
|
|
77
|
-
}
|
|
79
|
+
switch (segment.kind) {
|
|
80
|
+
case "inlineFragment": return selection.kind === Kind.INLINE_FRAGMENT && selection.typeCondition?.name.value === segment.typeName;
|
|
81
|
+
case "field": return selection.kind === Kind.FIELD && selection.name.value === segment.name;
|
|
82
|
+
}
|
|
78
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* Extracts the raw, printable value string for an argument at `path` from the
|
|
86
|
+
* document, e.g. `"abc"` for a string or `42` for an int. Returns an empty
|
|
87
|
+
* string when the field, arg, or a value we can render is not found. Walks both
|
|
88
|
+
* field and inline-fragment path segments.
|
|
89
|
+
*/
|
|
79
90
|
function extractRawArgValue(doc, path, argName, operationName) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
if (v.kind === Kind.INT || v.kind === Kind.FLOAT) {
|
|
104
|
-
return v.value;
|
|
105
|
-
}
|
|
106
|
-
if (v.kind === Kind.BOOLEAN) {
|
|
107
|
-
return String(v.value);
|
|
108
|
-
}
|
|
109
|
-
if (v.kind === Kind.ENUM) {
|
|
110
|
-
return v.value;
|
|
111
|
-
}
|
|
112
|
-
return "";
|
|
113
|
-
}
|
|
114
|
-
if (!node.selectionSet) {
|
|
115
|
-
return "";
|
|
116
|
-
}
|
|
117
|
-
selections = node.selectionSet.selections;
|
|
118
|
-
}
|
|
119
|
-
return "";
|
|
91
|
+
const op = findOperationDefinition(doc, operationName);
|
|
92
|
+
if (!op) return "";
|
|
93
|
+
let selections = op.selectionSet.selections;
|
|
94
|
+
for (let i = 0; i < path.length; i++) {
|
|
95
|
+
const seg = path[i];
|
|
96
|
+
const node = selections.find((s) => selectionMatchesSegment(s, seg));
|
|
97
|
+
if (!node) return "";
|
|
98
|
+
if (i === path.length - 1) {
|
|
99
|
+
if (node.kind !== Kind.FIELD) return "";
|
|
100
|
+
const argNode = (node.arguments ?? []).find((a) => a.name.value === argName);
|
|
101
|
+
if (!argNode) return "";
|
|
102
|
+
const v = argNode.value;
|
|
103
|
+
if (v.kind === Kind.STRING) return `"${v.value}"`;
|
|
104
|
+
if (v.kind === Kind.INT || v.kind === Kind.FLOAT) return v.value;
|
|
105
|
+
if (v.kind === Kind.BOOLEAN) return String(v.value);
|
|
106
|
+
if (v.kind === Kind.ENUM) return v.value;
|
|
107
|
+
return "";
|
|
108
|
+
}
|
|
109
|
+
if (!node.selectionSet) return "";
|
|
110
|
+
selections = node.selectionSet.selections;
|
|
111
|
+
}
|
|
112
|
+
return "";
|
|
120
113
|
}
|
|
114
|
+
/** Counts the fields selected in the target operation (nested fields included). */
|
|
121
115
|
function countSelectedFields(doc, operationName) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
return count;
|
|
116
|
+
let count = 0;
|
|
117
|
+
const op = findOperationDefinition(doc, operationName);
|
|
118
|
+
if (op) visit(op, { Field() {
|
|
119
|
+
count++;
|
|
120
|
+
} });
|
|
121
|
+
return count;
|
|
132
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Resolves the schema `GraphQLArgument` for the argument `argName` on the field
|
|
125
|
+
* addressed by `path`, walking from the root type for `activeOpKind`. Returns
|
|
126
|
+
* undefined when the field or arg can't be resolved against the schema.
|
|
127
|
+
*/
|
|
133
128
|
function resolveSchemaArg(schema, activeOpKind, path, argName) {
|
|
134
|
-
|
|
135
|
-
return resolveSchemaArgFromRoot(schema, rootType ?? void 0, path, argName);
|
|
129
|
+
return resolveSchemaArgFromRoot(schema, schema.getRootType(activeOpKind ?? OperationTypeNode.QUERY) ?? schema.getQueryType() ?? void 0, path, argName);
|
|
136
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Like {@link resolveSchemaArg} but walks from an explicit `rootType` rather
|
|
133
|
+
* than an operation kind, so it resolves args inside a fragment (whose root is
|
|
134
|
+
* its type condition) as well as inside an operation.
|
|
135
|
+
*/
|
|
137
136
|
function resolveSchemaArgFromRoot(schema, rootType, path, argName) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
const f = fields[seg.name];
|
|
158
|
-
if (!f) {
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
targetField = f;
|
|
162
|
-
const named = getNamedType(f.type);
|
|
163
|
-
if (named && (isObjectType(named) || isInterfaceType(named))) {
|
|
164
|
-
currentType = named;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
return targetField == null ? void 0 : targetField.args.find((a) => a.name === argName);
|
|
137
|
+
if (!rootType || path.length === 0) return;
|
|
138
|
+
let currentType = rootType;
|
|
139
|
+
let targetField;
|
|
140
|
+
for (const seg of path) {
|
|
141
|
+
if (seg.kind === "inlineFragment") {
|
|
142
|
+
const condition = schema.getType(seg.typeName);
|
|
143
|
+
if (!condition) return;
|
|
144
|
+
currentType = condition;
|
|
145
|
+
targetField = void 0;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (!(isObjectType(currentType) || isInterfaceType(currentType))) return;
|
|
149
|
+
const f = currentType.getFields()[seg.name];
|
|
150
|
+
if (!f) return;
|
|
151
|
+
targetField = f;
|
|
152
|
+
const named = getNamedType(f.type);
|
|
153
|
+
if (named && (isObjectType(named) || isInterfaceType(named))) currentType = named;
|
|
154
|
+
}
|
|
155
|
+
return targetField?.args.find((a) => a.name === argName);
|
|
168
156
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
readVariables,
|
|
174
|
-
resolveSchemaArg,
|
|
175
|
-
resolveSchemaArgFromRoot
|
|
176
|
-
};
|
|
177
|
-
//# sourceMappingURL=schema-walk.js.map
|
|
157
|
+
//#endregion
|
|
158
|
+
export { countSelectedFields, cursorContextAtOffset, extractRawArgValue, readVariables, resolveSchemaArg, resolveSchemaArgFromRoot };
|
|
159
|
+
|
|
160
|
+
//# sourceMappingURL=schema-walk.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema-walk.js","sources":["../../src/lib/schema-walk.ts"],"sourcesContent":["import {\n Kind,\n OperationTypeNode,\n isInterfaceType,\n isObjectType,\n visit,\n type DocumentNode,\n type FieldNode,\n type GraphQLArgument,\n type GraphQLNamedType,\n type GraphQLSchema,\n type InlineFragmentNode,\n type SelectionNode,\n getNamedType,\n} from 'graphql';\nimport {\n fieldSegment,\n inlineFragmentSegment,\n type DefinitionTarget,\n type PathSegment,\n} from './ast-path';\n\n/**\n * Safely parse the variables JSON text, returning a plain object or {} on\n * failure. Arrays and non-object values are treated as empty.\n */\nexport function readVariables(\n text: string | null | undefined,\n): Record<string, unknown> {\n if (!text || !text.trim()) {\n return {};\n }\n try {\n const parsed = JSON.parse(text);\n return parsed && typeof parsed === 'object' && !Array.isArray(parsed)\n ? (parsed as Record<string, unknown>)\n : {};\n } catch {\n return {};\n }\n}\n\n/**\n * Returns the tree path of the selection whose source range contains `offset`,\n * walking from the operation that contains the cursor down through nested\n * selection sets. Inline fragments contribute an `inlineFragment` path segment\n * (matching the builder's path model), so the cursor resolves to fields nested\n * inside union/interface type conditions too. Returns `[]` when the cursor\n * isn't inside a field or fragment.\n */\nfunction pathWithinSelections(\n rootSelections: readonly SelectionNode[],\n offset: number,\n): PathSegment[] {\n const path: PathSegment[] = [];\n let selections = rootSelections;\n while (true) {\n const selection = selections.find(\n (s): s is FieldNode | InlineFragmentNode =>\n (s.kind === Kind.FIELD || s.kind === Kind.INLINE_FRAGMENT) &&\n s.loc != null &&\n s.loc.start <= offset &&\n s.loc.end >= offset,\n );\n if (!selection) {\n break;\n }\n if (selection.kind === Kind.FIELD) {\n path.push(fieldSegment(selection.name.value));\n } else {\n const typeName = selection.typeCondition?.name.value;\n if (!typeName) {\n break;\n }\n path.push(inlineFragmentSegment(typeName));\n }\n if (!selection.selectionSet) {\n break;\n }\n selections = selection.selectionSet.selections;\n }\n return path;\n}\n\nexport function fieldPathAtOffset(\n doc: DocumentNode,\n offset: number,\n): PathSegment[] {\n const op = doc.definitions.find(\n d =>\n d.kind === Kind.OPERATION_DEFINITION &&\n d.loc != null &&\n d.loc.start <= offset &&\n d.loc.end >= offset,\n );\n if (!op || op.kind !== Kind.OPERATION_DEFINITION) {\n return [];\n }\n return pathWithinSelections(op.selectionSet.selections, offset);\n}\n\n/**\n * Resolves the definition the cursor sits in — an operation or a named\n * fragment — together with the field path within it. Returns `undefined` when\n * the cursor is not inside any editable definition. Drives cursor-following:\n * moving the editor cursor into a fragment switches the builder to editing it.\n */\nexport function cursorContextAtOffset(\n doc: DocumentNode,\n offset: number,\n): { target: DefinitionTarget; path: PathSegment[] } | undefined {\n const def = doc.definitions.find(\n d =>\n (d.kind === Kind.OPERATION_DEFINITION ||\n d.kind === Kind.FRAGMENT_DEFINITION) &&\n d.loc != null &&\n d.loc.start <= offset &&\n d.loc.end >= offset,\n );\n if (def?.kind === Kind.OPERATION_DEFINITION) {\n return {\n target: { kind: 'operation', name: def.name?.value },\n path: pathWithinSelections(def.selectionSet.selections, offset),\n };\n }\n if (def?.kind === Kind.FRAGMENT_DEFINITION) {\n return {\n target: { kind: 'fragment', name: def.name.value },\n path: pathWithinSelections(def.selectionSet.selections, offset),\n };\n }\n return undefined;\n}\n\n/**\n * Resolves the operation to read from: the one named `operationName` when given\n * and present, otherwise the first operation definition.\n */\nfunction findOperationDefinition(doc: DocumentNode, operationName?: string) {\n const byName = operationName\n ? doc.definitions.find(\n d =>\n d.kind === Kind.OPERATION_DEFINITION &&\n d.name?.value === operationName,\n )\n : undefined;\n const op =\n byName ?? doc.definitions.find(d => d.kind === Kind.OPERATION_DEFINITION);\n return op?.kind === Kind.OPERATION_DEFINITION ? op : undefined;\n}\n\n/**\n * Matches a path segment against a selection, by schema field name or, for an\n * inline-fragment segment, by type condition. Mirrors the document-mutator path\n * model so the cursor/path helpers agree with it.\n */\nfunction 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 * Extracts the raw, printable value string for an argument at `path` from the\n * document, e.g. `\"abc\"` for a string or `42` for an int. Returns an empty\n * string when the field, arg, or a value we can render is not found. Walks both\n * field and inline-fragment path segments.\n */\nexport function extractRawArgValue(\n doc: DocumentNode,\n path: PathSegment[],\n argName: string,\n operationName?: string,\n): string {\n const op = findOperationDefinition(doc, operationName);\n if (!op) {\n return '';\n }\n\n let selections: readonly SelectionNode[] = op.selectionSet.selections;\n for (let i = 0; i < path.length; i++) {\n const seg = path[i]!;\n const node = selections.find((s): s is FieldNode | InlineFragmentNode =>\n selectionMatchesSegment(s, seg),\n );\n if (!node) {\n return '';\n }\n if (i === path.length - 1) {\n if (node.kind !== Kind.FIELD) {\n return '';\n }\n const argNode = (node.arguments ?? []).find(\n a => a.name.value === argName,\n );\n if (!argNode) {\n return '';\n }\n const v = argNode.value;\n if (v.kind === Kind.STRING) {\n return `\"${v.value}\"`;\n }\n if (v.kind === Kind.INT || v.kind === Kind.FLOAT) {\n return v.value;\n }\n if (v.kind === Kind.BOOLEAN) {\n return String(v.value);\n }\n if (v.kind === Kind.ENUM) {\n return v.value;\n }\n return '';\n }\n if (!node.selectionSet) {\n return '';\n }\n selections = node.selectionSet.selections;\n }\n return '';\n}\n\n/** Counts the fields selected in the target operation (nested fields included). */\nexport function countSelectedFields(\n doc: DocumentNode,\n operationName?: string,\n): number {\n let count = 0;\n const op = findOperationDefinition(doc, operationName);\n if (op) {\n visit(op, {\n Field() {\n count++;\n },\n });\n }\n return count;\n}\n\n/**\n * Resolves the schema `GraphQLArgument` for the argument `argName` on the field\n * addressed by `path`, walking from the root type for `activeOpKind`. Returns\n * undefined when the field or arg can't be resolved against the schema.\n */\nexport function resolveSchemaArg(\n schema: GraphQLSchema,\n activeOpKind: OperationTypeNode | undefined,\n path: PathSegment[],\n argName: string,\n): GraphQLArgument | undefined {\n const rootType =\n schema.getRootType(activeOpKind ?? OperationTypeNode.QUERY) ??\n schema.getQueryType();\n return resolveSchemaArgFromRoot(schema, rootType ?? undefined, path, argName);\n}\n\n/**\n * Like {@link resolveSchemaArg} but walks from an explicit `rootType` rather\n * than an operation kind, so it resolves args inside a fragment (whose root is\n * its type condition) as well as inside an operation.\n */\nexport function resolveSchemaArgFromRoot(\n schema: GraphQLSchema,\n rootType: GraphQLNamedType | undefined,\n path: PathSegment[],\n argName: string,\n): GraphQLArgument | undefined {\n if (!rootType || path.length === 0) {\n return;\n }\n\n let currentType: GraphQLNamedType = rootType;\n let targetField: { args: readonly GraphQLArgument[] } | undefined;\n for (const seg of path) {\n if (seg.kind === 'inlineFragment') {\n const condition = schema.getType(seg.typeName);\n if (!condition) {\n return;\n }\n currentType = condition;\n targetField = undefined;\n continue;\n }\n if (!(isObjectType(currentType) || isInterfaceType(currentType))) {\n return;\n }\n const fields = currentType.getFields();\n const f = fields[seg.name] as\n | { type: unknown; args: readonly GraphQLArgument[] }\n | undefined;\n if (!f) {\n return;\n }\n targetField = f;\n const named = getNamedType(f.type as Parameters<typeof getNamedType>[0]);\n if (named && (isObjectType(named) || isInterfaceType(named))) {\n currentType = named;\n }\n }\n return targetField?.args.find(a => a.name === argName);\n}\n\n/**\n * Resolves the named type reached by walking `path` from the root type for\n * `activeOpKind`. Field segments descend into the field's type; `inlineFragment`\n * segments switch to that type condition. Returns undefined when a segment\n * doesn't resolve. Used to pick the type condition when extracting a field's\n * selection into a fragment.\n */\nexport function resolveFieldNamedType(\n schema: GraphQLSchema,\n activeOpKind: OperationTypeNode | undefined,\n path: PathSegment[],\n): GraphQLNamedType | undefined {\n const rootType =\n schema.getRootType(activeOpKind ?? OperationTypeNode.QUERY) ??\n schema.getQueryType();\n if (!rootType || path.length === 0) {\n return;\n }\n\n let currentType: GraphQLNamedType = rootType;\n let result: GraphQLNamedType | undefined;\n for (const seg of path) {\n if (seg.kind === 'inlineFragment') {\n const condition = schema.getType(seg.typeName);\n if (!condition) {\n return;\n }\n currentType = condition;\n result = condition;\n continue;\n }\n if (!(isObjectType(currentType) || isInterfaceType(currentType))) {\n return;\n }\n const fields = currentType.getFields();\n const f = fields[seg.name] as { type: unknown } | undefined;\n if (!f) {\n return;\n }\n const named = getNamedType(f.type as Parameters<typeof getNamedType>[0]);\n if (!named) {\n return;\n }\n result = named;\n currentType = named;\n }\n return result;\n}\n"],"names":["readVariables","text","trim","parsed","JSON","parse","Array","isArray","pathWithinSelections","rootSelections","offset","path","selections","selection","find","s","kind","Kind","FIELD","INLINE_FRAGMENT","loc","start","end","push","fieldSegment","name","value","typeName","typeCondition","inlineFragmentSegment","selectionSet","cursorContextAtOffset","doc","def","definitions","d","OPERATION_DEFINITION","FRAGMENT_DEFINITION","target","undefined","findOperationDefinition","operationName","byName","op","selectionMatchesSegment","segment","extractRawArgValue","argName","i","length","seg","node","argNode","arguments","a","v","STRING","INT","FLOAT","BOOLEAN","String","ENUM","countSelectedFields","count","visit","Field","resolveSchemaArg","schema","activeOpKind","rootType","getRootType","OperationTypeNode","QUERY","getQueryType","resolveSchemaArgFromRoot","currentType","targetField","condition","getType","isObjectType","isInterfaceType","fields","getFields","f","named","getNamedType","type","args"],"mappings":";;AA0BO,SAASA,cACdC,MACyB;AACzB,MAAI,CAACA,QAAQ,CAACA,KAAKC,QAAQ;AACzB,WAAO,CAAC;AAAA,EAAA;AAEN,MAAA;AACIC,UAAAA,SAASC,KAAKC,MAAMJ,IAAI;AACvBE,WAAAA,UAAU,OAAOA,WAAW,YAAY,CAACG,MAAMC,QAAQJ,MAAM,IAC/DA,SACD,CAAC;AAAA,EAAA,QACC;AACN,WAAO,CAAC;AAAA,EAAA;AAEZ;AAUA,SAASK,qBACPC,gBACAC,QACe;;AACf,QAAMC,OAAsB,CAAE;AAC9B,MAAIC,aAAaH;AACjB,SAAO,MAAM;AACLI,UAAAA,YAAYD,WAAWE,KAC3B,CAACC,OACEA,EAAEC,SAASC,KAAKC,SAASH,EAAEC,SAASC,KAAKE,oBAC1CJ,EAAEK,OAAO,QACTL,EAAEK,IAAIC,SAASX,UACfK,EAAEK,IAAIE,OAAOZ,MACjB;AACA,QAAI,CAACG,WAAW;AACd;AAAA,IAAA;AAEEA,QAAAA,UAAUG,SAASC,KAAKC,OAAO;AACjCP,WAAKY,KAAKC,aAAaX,UAAUY,KAAKC,KAAK,CAAC;AAAA,IAAA,OACvC;AACCC,YAAAA,YAAWd,eAAUe,kBAAVf,mBAAyBY,KAAKC;AAC/C,UAAI,CAACC,UAAU;AACb;AAAA,MAAA;AAE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zB,QAAI,CAAC0D,GAAG;AACN;AAAA,IAAA;AAEYA,kBAAAA;AACRC,UAAAA,QAAQC,aAAaF,EAAEG,IAA0C;AACvE,QAAIF,UAAUL,aAAaK,KAAK,KAAKJ,gBAAgBI,KAAK,IAAI;AAC9CA,oBAAAA;AAAAA,IAAAA;AAAAA,EAChB;AAEF,SAAOR,2CAAaW,KAAKzE,KAAKwC,CAAKA,MAAAA,EAAE7B,SAASsB;AAChD;"}
|
|
1
|
+
{"version":3,"file":"schema-walk.js","names":["Kind","OperationTypeNode","isInterfaceType","isObjectType","visit","DocumentNode","FieldNode","GraphQLArgument","GraphQLNamedType","GraphQLSchema","InlineFragmentNode","SelectionNode","getNamedType","fieldSegment","inlineFragmentSegment","DefinitionTarget","PathSegment","readVariables","text","Record","trim","parsed","JSON","parse","Array","isArray","pathWithinSelections","rootSelections","offset","path","selections","selection","find","s","kind","FIELD","INLINE_FRAGMENT","loc","start","end","push","name","value","typeName","typeCondition","selectionSet","fieldPathAtOffset","doc","op","definitions","d","OPERATION_DEFINITION","cursorContextAtOffset","target","def","FRAGMENT_DEFINITION","undefined","findOperationDefinition","operationName","byName","selectionMatchesSegment","segment","extractRawArgValue","argName","i","length","seg","node","argNode","arguments","a","v","STRING","INT","FLOAT","BOOLEAN","String","ENUM","countSelectedFields","count","Field","resolveSchemaArg","schema","activeOpKind","rootType","getRootType","QUERY","getQueryType","resolveSchemaArgFromRoot","currentType","targetField","args","condition","getType","fields","getFields","f","type","named","Parameters","resolveFieldNamedType","result"],"sources":["../../src/lib/schema-walk.ts"],"sourcesContent":["import {\n Kind,\n OperationTypeNode,\n isInterfaceType,\n isObjectType,\n visit,\n type DocumentNode,\n type FieldNode,\n type GraphQLArgument,\n type GraphQLNamedType,\n type GraphQLSchema,\n type InlineFragmentNode,\n type SelectionNode,\n getNamedType,\n} from 'graphql';\nimport {\n fieldSegment,\n inlineFragmentSegment,\n type DefinitionTarget,\n type PathSegment,\n} from './ast-path';\n\n/**\n * Safely parse the variables JSON text, returning a plain object or {} on\n * failure. Arrays and non-object values are treated as empty.\n */\nexport function readVariables(\n text: string | null | undefined,\n): Record<string, unknown> {\n if (!text || !text.trim()) {\n return {};\n }\n try {\n const parsed = JSON.parse(text);\n return parsed && typeof parsed === 'object' && !Array.isArray(parsed)\n ? (parsed as Record<string, unknown>)\n : {};\n } catch {\n return {};\n }\n}\n\n/**\n * Returns the tree path of the selection whose source range contains `offset`,\n * walking from the operation that contains the cursor down through nested\n * selection sets. Inline fragments contribute an `inlineFragment` path segment\n * (matching the builder's path model), so the cursor resolves to fields nested\n * inside union/interface type conditions too. Returns `[]` when the cursor\n * isn't inside a field or fragment.\n */\nfunction pathWithinSelections(\n rootSelections: readonly SelectionNode[],\n offset: number,\n): PathSegment[] {\n const path: PathSegment[] = [];\n let selections = rootSelections;\n while (true) {\n const selection = selections.find(\n (s): s is FieldNode | InlineFragmentNode =>\n (s.kind === Kind.FIELD || s.kind === Kind.INLINE_FRAGMENT) &&\n s.loc != null &&\n s.loc.start <= offset &&\n s.loc.end >= offset,\n );\n if (!selection) {\n break;\n }\n if (selection.kind === Kind.FIELD) {\n path.push(fieldSegment(selection.name.value));\n } else {\n const typeName = selection.typeCondition?.name.value;\n if (!typeName) {\n break;\n }\n path.push(inlineFragmentSegment(typeName));\n }\n if (!selection.selectionSet) {\n break;\n }\n selections = selection.selectionSet.selections;\n }\n return path;\n}\n\nexport function fieldPathAtOffset(\n doc: DocumentNode,\n offset: number,\n): PathSegment[] {\n const op = doc.definitions.find(\n d =>\n d.kind === Kind.OPERATION_DEFINITION &&\n d.loc != null &&\n d.loc.start <= offset &&\n d.loc.end >= offset,\n );\n if (!op || op.kind !== Kind.OPERATION_DEFINITION) {\n return [];\n }\n return pathWithinSelections(op.selectionSet.selections, offset);\n}\n\n/**\n * Resolves the definition the cursor sits in — an operation or a named\n * fragment — together with the field path within it. Returns `undefined` when\n * the cursor is not inside any editable definition. Drives cursor-following:\n * moving the editor cursor into a fragment switches the builder to editing it.\n */\nexport function cursorContextAtOffset(\n doc: DocumentNode,\n offset: number,\n): { target: DefinitionTarget; path: PathSegment[] } | undefined {\n const def = doc.definitions.find(\n d =>\n (d.kind === Kind.OPERATION_DEFINITION ||\n d.kind === Kind.FRAGMENT_DEFINITION) &&\n d.loc != null &&\n d.loc.start <= offset &&\n d.loc.end >= offset,\n );\n if (def?.kind === Kind.OPERATION_DEFINITION) {\n return {\n target: { kind: 'operation', name: def.name?.value },\n path: pathWithinSelections(def.selectionSet.selections, offset),\n };\n }\n if (def?.kind === Kind.FRAGMENT_DEFINITION) {\n return {\n target: { kind: 'fragment', name: def.name.value },\n path: pathWithinSelections(def.selectionSet.selections, offset),\n };\n }\n return undefined;\n}\n\n/**\n * Resolves the operation to read from: the one named `operationName` when given\n * and present, otherwise the first operation definition.\n */\nfunction findOperationDefinition(doc: DocumentNode, operationName?: string) {\n const byName = operationName\n ? doc.definitions.find(\n d =>\n d.kind === Kind.OPERATION_DEFINITION &&\n d.name?.value === operationName,\n )\n : undefined;\n const op =\n byName ?? doc.definitions.find(d => d.kind === Kind.OPERATION_DEFINITION);\n return op?.kind === Kind.OPERATION_DEFINITION ? op : undefined;\n}\n\n/**\n * Matches a path segment against a selection, by schema field name or, for an\n * inline-fragment segment, by type condition. Mirrors the document-mutator path\n * model so the cursor/path helpers agree with it.\n */\nfunction 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 * Extracts the raw, printable value string for an argument at `path` from the\n * document, e.g. `\"abc\"` for a string or `42` for an int. Returns an empty\n * string when the field, arg, or a value we can render is not found. Walks both\n * field and inline-fragment path segments.\n */\nexport function extractRawArgValue(\n doc: DocumentNode,\n path: PathSegment[],\n argName: string,\n operationName?: string,\n): string {\n const op = findOperationDefinition(doc, operationName);\n if (!op) {\n return '';\n }\n\n let selections: readonly SelectionNode[] = op.selectionSet.selections;\n for (let i = 0; i < path.length; i++) {\n const seg = path[i]!;\n const node = selections.find((s): s is FieldNode | InlineFragmentNode =>\n selectionMatchesSegment(s, seg),\n );\n if (!node) {\n return '';\n }\n if (i === path.length - 1) {\n if (node.kind !== Kind.FIELD) {\n return '';\n }\n const argNode = (node.arguments ?? []).find(\n a => a.name.value === argName,\n );\n if (!argNode) {\n return '';\n }\n const v = argNode.value;\n if (v.kind === Kind.STRING) {\n return `\"${v.value}\"`;\n }\n if (v.kind === Kind.INT || v.kind === Kind.FLOAT) {\n return v.value;\n }\n if (v.kind === Kind.BOOLEAN) {\n return String(v.value);\n }\n if (v.kind === Kind.ENUM) {\n return v.value;\n }\n return '';\n }\n if (!node.selectionSet) {\n return '';\n }\n selections = node.selectionSet.selections;\n }\n return '';\n}\n\n/** Counts the fields selected in the target operation (nested fields included). */\nexport function countSelectedFields(\n doc: DocumentNode,\n operationName?: string,\n): number {\n let count = 0;\n const op = findOperationDefinition(doc, operationName);\n if (op) {\n visit(op, {\n Field() {\n count++;\n },\n });\n }\n return count;\n}\n\n/**\n * Resolves the schema `GraphQLArgument` for the argument `argName` on the field\n * addressed by `path`, walking from the root type for `activeOpKind`. Returns\n * undefined when the field or arg can't be resolved against the schema.\n */\nexport function resolveSchemaArg(\n schema: GraphQLSchema,\n activeOpKind: OperationTypeNode | undefined,\n path: PathSegment[],\n argName: string,\n): GraphQLArgument | undefined {\n const rootType =\n schema.getRootType(activeOpKind ?? OperationTypeNode.QUERY) ??\n schema.getQueryType();\n return resolveSchemaArgFromRoot(schema, rootType ?? undefined, path, argName);\n}\n\n/**\n * Like {@link resolveSchemaArg} but walks from an explicit `rootType` rather\n * than an operation kind, so it resolves args inside a fragment (whose root is\n * its type condition) as well as inside an operation.\n */\nexport function resolveSchemaArgFromRoot(\n schema: GraphQLSchema,\n rootType: GraphQLNamedType | undefined,\n path: PathSegment[],\n argName: string,\n): GraphQLArgument | undefined {\n if (!rootType || path.length === 0) {\n return;\n }\n\n let currentType: GraphQLNamedType = rootType;\n let targetField: { args: readonly GraphQLArgument[] } | undefined;\n for (const seg of path) {\n if (seg.kind === 'inlineFragment') {\n const condition = schema.getType(seg.typeName);\n if (!condition) {\n return;\n }\n currentType = condition;\n targetField = undefined;\n continue;\n }\n if (!(isObjectType(currentType) || isInterfaceType(currentType))) {\n return;\n }\n const fields = currentType.getFields();\n const f = fields[seg.name] as\n | { type: unknown; args: readonly GraphQLArgument[] }\n | undefined;\n if (!f) {\n return;\n }\n targetField = f;\n const named = getNamedType(f.type as Parameters<typeof getNamedType>[0]);\n if (named && (isObjectType(named) || isInterfaceType(named))) {\n currentType = named;\n }\n }\n return targetField?.args.find(a => a.name === argName);\n}\n\n/**\n * Resolves the named type reached by walking `path` from the root type for\n * `activeOpKind`. Field segments descend into the field's type; `inlineFragment`\n * segments switch to that type condition. Returns undefined when a segment\n * doesn't resolve. Used to pick the type condition when extracting a field's\n * selection into a fragment.\n */\nexport function resolveFieldNamedType(\n schema: GraphQLSchema,\n activeOpKind: OperationTypeNode | undefined,\n path: PathSegment[],\n): GraphQLNamedType | undefined {\n const rootType =\n schema.getRootType(activeOpKind ?? OperationTypeNode.QUERY) ??\n schema.getQueryType();\n if (!rootType || path.length === 0) {\n return;\n }\n\n let currentType: GraphQLNamedType = rootType;\n let result: GraphQLNamedType | undefined;\n for (const seg of path) {\n if (seg.kind === 'inlineFragment') {\n const condition = schema.getType(seg.typeName);\n if (!condition) {\n return;\n }\n currentType = condition;\n result = condition;\n continue;\n }\n if (!(isObjectType(currentType) || isInterfaceType(currentType))) {\n return;\n }\n const fields = currentType.getFields();\n const f = fields[seg.name] as { type: unknown } | undefined;\n if (!f) {\n return;\n }\n const named = getNamedType(f.type as Parameters<typeof getNamedType>[0]);\n if (!named) {\n return;\n }\n result = named;\n currentType = named;\n }\n return result;\n}\n"],"mappings":";;;;;;;AA0BA,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AC7C,IAAI,CAACiD,WACH;GAEFH,cAAcG;GACdF,cAAclC,KAAAA;GACd;EACF;EACA,IAAI,EAAErD,aAAasF,WAAW,KAAKvF,gBAAgBuF,WAAW,IAC5D;EAGF,MAAMO,IADSP,YAAYM,UACjBD,CAAAA,CAAO5B,IAAIzB;EAGrB,IAAI,CAACuD,GACH;EAEFN,cAAcM;EACd,MAAME,QAAQtF,aAAaoF,EAAEC,IAA0C;EACvE,IAAIC,UAAU/F,aAAa+F,KAAK,KAAKhG,gBAAgBgG,KAAK,IACxDT,cAAcS;CAElB;CACA,OAAOR,aAAaC,KAAK3D,MAAKsC,MAAKA,EAAE7B,SAASsB,OAAO;AACvD"}
|
package/dist/lib/variables.js
CHANGED
|
@@ -1,100 +1,106 @@
|
|
|
1
|
-
import { Kind, visit } from "graphql";
|
|
2
1
|
import { mapOperation, setArgInSelectionSet } from "./ast-path.js";
|
|
3
2
|
import { rawToValueNode } from "./arg-value.js";
|
|
3
|
+
import { Kind, visit } from "graphql";
|
|
4
|
+
//#region src/lib/variables.ts
|
|
5
|
+
/**
|
|
6
|
+
* Suggests a variable name for `argName` that does not collide with any
|
|
7
|
+
* existing variable definition anywhere in `doc`. The check spans every
|
|
8
|
+
* operation because the variables editor is a single document-wide JSON
|
|
9
|
+
* object: two operations sharing a variable name would share its value.
|
|
10
|
+
* Returns `argName` when no collision exists, otherwise appends `_2`, `_3`, etc.
|
|
11
|
+
*/
|
|
4
12
|
function suggestVarName(doc, argName) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
if (!existingNames.has(argName)) {
|
|
14
|
-
return argName;
|
|
15
|
-
}
|
|
16
|
-
let i = 2;
|
|
17
|
-
while (existingNames.has(`${argName}_${i}`)) {
|
|
18
|
-
i++;
|
|
19
|
-
}
|
|
20
|
-
return `${argName}_${i}`;
|
|
13
|
+
const existingNames = /* @__PURE__ */ new Set();
|
|
14
|
+
for (const def of doc.definitions) if (def.kind === Kind.OPERATION_DEFINITION) for (const vd of def.variableDefinitions ?? []) existingNames.add(vd.variable.name.value);
|
|
15
|
+
if (!existingNames.has(argName)) return argName;
|
|
16
|
+
let i = 2;
|
|
17
|
+
while (existingNames.has(`${argName}_${i}`)) i++;
|
|
18
|
+
return `${argName}_${i}`;
|
|
21
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Promotes a scalar argument on the field at `path` to a variable reference.
|
|
22
|
+
* Adds `($varName: type = defaultRaw)` to the target operation's (by name, or
|
|
23
|
+
* the first operation when unspecified) variable definitions and replaces the
|
|
24
|
+
* inline argument value with `$varName`.
|
|
25
|
+
*
|
|
26
|
+
* When the operation is anonymous (shorthand query), it is converted to an
|
|
27
|
+
* explicit `query` operation so that variable definitions can be added.
|
|
28
|
+
*
|
|
29
|
+
* Returns a new `DocumentNode`; does not mutate `doc`.
|
|
30
|
+
*/
|
|
22
31
|
function promoteArgToVariable(doc, path, argName, varName, type, defaultRaw, operationName) {
|
|
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
|
-
});
|
|
32
|
+
const defaultValue = rawToValueNode(defaultRaw);
|
|
33
|
+
const varDef = {
|
|
34
|
+
kind: Kind.VARIABLE_DEFINITION,
|
|
35
|
+
variable: {
|
|
36
|
+
kind: Kind.VARIABLE,
|
|
37
|
+
name: {
|
|
38
|
+
kind: Kind.NAME,
|
|
39
|
+
value: varName
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
type: {
|
|
43
|
+
kind: Kind.NAMED_TYPE,
|
|
44
|
+
name: {
|
|
45
|
+
kind: Kind.NAME,
|
|
46
|
+
value: type
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
defaultValue,
|
|
50
|
+
directives: []
|
|
51
|
+
};
|
|
52
|
+
const varValueNode = {
|
|
53
|
+
kind: Kind.VARIABLE,
|
|
54
|
+
name: {
|
|
55
|
+
kind: Kind.NAME,
|
|
56
|
+
value: varName
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
return mapOperation(doc, operationName, (operation) => {
|
|
60
|
+
const newSelectionSet = setArgInSelectionSet(operation.selectionSet, path, argName, varValueNode);
|
|
61
|
+
if (newSelectionSet === operation.selectionSet) return operation;
|
|
62
|
+
const existingVarDefs = operation.variableDefinitions ?? [];
|
|
63
|
+
const variableDefinitions = existingVarDefs.some((vd) => vd.variable.name.value === varName) ? existingVarDefs : [...existingVarDefs, varDef];
|
|
64
|
+
return {
|
|
65
|
+
...operation,
|
|
66
|
+
variableDefinitions,
|
|
67
|
+
selectionSet: newSelectionSet
|
|
68
|
+
};
|
|
69
|
+
});
|
|
63
70
|
}
|
|
64
71
|
function replaceVariableInSelectionSet(selectionSet, varName, defaultValue) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
});
|
|
72
|
+
return visit(selectionSet, { Argument(arg) {
|
|
73
|
+
if (arg.value.kind === Kind.VARIABLE && arg.value.name.value === varName) return defaultValue === void 0 ? null : {
|
|
74
|
+
...arg,
|
|
75
|
+
value: defaultValue
|
|
76
|
+
};
|
|
77
|
+
} });
|
|
76
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Demotes a variable back to an inline literal. Removes the variable
|
|
81
|
+
* definition for `varName` from the target operation (by name, or the first
|
|
82
|
+
* operation when unspecified) and replaces every `$varName` argument reference
|
|
83
|
+
* with `inlineValue` when provided, otherwise the variable's default value
|
|
84
|
+
* (or removes the arg entirely when there is neither).
|
|
85
|
+
*
|
|
86
|
+
* Returns a new `DocumentNode`; does not mutate `doc`.
|
|
87
|
+
*/
|
|
77
88
|
function demoteVariable(doc, varName, operationName, inlineValue) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
};
|
|
92
|
-
});
|
|
89
|
+
return mapOperation(doc, operationName, (operation) => {
|
|
90
|
+
const varDefs = operation.variableDefinitions ?? [];
|
|
91
|
+
const targetDef = varDefs.find((vd) => vd.variable.name.value === varName);
|
|
92
|
+
if (!targetDef) return operation;
|
|
93
|
+
const value = inlineValue ?? targetDef.defaultValue;
|
|
94
|
+
const newVarDefs = varDefs.filter((vd) => vd.variable.name.value !== varName);
|
|
95
|
+
const newSelectionSet = replaceVariableInSelectionSet(operation.selectionSet, varName, value);
|
|
96
|
+
return {
|
|
97
|
+
...operation,
|
|
98
|
+
variableDefinitions: newVarDefs,
|
|
99
|
+
selectionSet: newSelectionSet
|
|
100
|
+
};
|
|
101
|
+
});
|
|
93
102
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
suggestVarName
|
|
99
|
-
};
|
|
100
|
-
//# sourceMappingURL=variables.js.map
|
|
103
|
+
//#endregion
|
|
104
|
+
export { demoteVariable, promoteArgToVariable, replaceVariableInSelectionSet, suggestVarName };
|
|
105
|
+
|
|
106
|
+
//# sourceMappingURL=variables.js.map
|