@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,165 @@
1
+ import { Kind, visit } from "graphql";
2
+ import { mapDefinition, findDefinition, findSelectionSet, replaceFieldSelectionSet, removeField } from "./ast-path.js";
3
+ import { pruneUnusedVariableDefinitions } from "./field-selection.js";
4
+ function listFragments(doc) {
5
+ return doc.definitions.filter((d) => d.kind === Kind.FRAGMENT_DEFINITION).map((f) => f.name.value);
6
+ }
7
+ function listFragmentInfos(doc) {
8
+ return doc.definitions.filter((d) => d.kind === Kind.FRAGMENT_DEFINITION).map((f) => ({
9
+ name: f.name.value,
10
+ typeName: f.typeCondition.name.value
11
+ }));
12
+ }
13
+ function createFragmentFromSelection(doc, path, fragmentName, typeName, target) {
14
+ const definition = findDefinition(doc, target);
15
+ if (!definition) {
16
+ return doc;
17
+ }
18
+ const targetSelectionSet = findSelectionSet(definition.selectionSet, path);
19
+ if (!targetSelectionSet || targetSelectionSet.selections.length === 0) {
20
+ return doc;
21
+ }
22
+ const fragmentDef = {
23
+ kind: Kind.FRAGMENT_DEFINITION,
24
+ name: {
25
+ kind: Kind.NAME,
26
+ value: fragmentName
27
+ },
28
+ typeCondition: {
29
+ kind: Kind.NAMED_TYPE,
30
+ name: {
31
+ kind: Kind.NAME,
32
+ value: typeName
33
+ }
34
+ },
35
+ directives: [],
36
+ selectionSet: targetSelectionSet
37
+ };
38
+ const spread = {
39
+ kind: Kind.FRAGMENT_SPREAD,
40
+ name: {
41
+ kind: Kind.NAME,
42
+ value: fragmentName
43
+ },
44
+ directives: []
45
+ };
46
+ const spreadSet = {
47
+ kind: Kind.SELECTION_SET,
48
+ selections: [spread]
49
+ };
50
+ let changed = false;
51
+ const updatedDoc = mapDefinition(doc, target, (def) => {
52
+ const newSelectionSet = replaceFieldSelectionSet(def.selectionSet, path, spreadSet);
53
+ if (newSelectionSet === def.selectionSet) {
54
+ return def;
55
+ }
56
+ changed = true;
57
+ return {
58
+ ...def,
59
+ selectionSet: newSelectionSet
60
+ };
61
+ });
62
+ if (!changed) {
63
+ return doc;
64
+ }
65
+ return {
66
+ ...updatedDoc,
67
+ definitions: [...updatedDoc.definitions, fragmentDef]
68
+ };
69
+ }
70
+ function removeFragmentSpread(doc, fieldPath, fragmentName, target) {
71
+ return mapDefinition(doc, target, (def) => {
72
+ const targetSet = fieldPath.length === 0 ? def.selectionSet : findSelectionSet(def.selectionSet, fieldPath);
73
+ if (!targetSet) {
74
+ return def;
75
+ }
76
+ const remaining = targetSet.selections.filter((s) => !(s.kind === Kind.FRAGMENT_SPREAD && s.name.value === fragmentName));
77
+ if (remaining.length === targetSet.selections.length) {
78
+ return def;
79
+ }
80
+ let newSelectionSet;
81
+ if (fieldPath.length === 0) {
82
+ newSelectionSet = {
83
+ ...def.selectionSet,
84
+ selections: remaining
85
+ };
86
+ } else if (remaining.length === 0) {
87
+ newSelectionSet = removeField(def.selectionSet, fieldPath);
88
+ } else {
89
+ newSelectionSet = replaceFieldSelectionSet(def.selectionSet, fieldPath, {
90
+ kind: Kind.SELECTION_SET,
91
+ selections: remaining
92
+ });
93
+ }
94
+ if (newSelectionSet.selections.length === 0) {
95
+ return def.kind === Kind.OPERATION_DEFINITION ? null : def;
96
+ }
97
+ if (def.kind === Kind.FRAGMENT_DEFINITION) {
98
+ return {
99
+ ...def,
100
+ selectionSet: newSelectionSet
101
+ };
102
+ }
103
+ return pruneUnusedVariableDefinitions({
104
+ ...def,
105
+ selectionSet: newSelectionSet
106
+ }, doc);
107
+ });
108
+ }
109
+ function inlineFragment(doc, fragmentName) {
110
+ const fragment = doc.definitions.find((d) => d.kind === Kind.FRAGMENT_DEFINITION && d.name.value === fragmentName);
111
+ if (!fragment) {
112
+ return doc;
113
+ }
114
+ const inlined = fragment.selectionSet.selections;
115
+ const isTargetSpread = (s) => s.kind === Kind.FRAGMENT_SPREAD && s.name.value === fragmentName;
116
+ const rewritten = visit(doc, {
117
+ SelectionSet(node) {
118
+ if (!node.selections.some(isTargetSpread)) {
119
+ return;
120
+ }
121
+ return {
122
+ ...node,
123
+ selections: node.selections.flatMap((s) => isTargetSpread(s) ? inlined : [s])
124
+ };
125
+ }
126
+ });
127
+ return {
128
+ ...rewritten,
129
+ definitions: rewritten.definitions.filter((d) => !(d.kind === Kind.FRAGMENT_DEFINITION && d.name.value === fragmentName))
130
+ };
131
+ }
132
+ function renameFragment(doc, oldName, newName) {
133
+ if (!newName || oldName === newName) {
134
+ return doc;
135
+ }
136
+ const names = new Set(listFragments(doc));
137
+ if (!names.has(oldName) || names.has(newName)) {
138
+ return doc;
139
+ }
140
+ const rename = (node) => {
141
+ if (node.name.value !== oldName) {
142
+ return;
143
+ }
144
+ return {
145
+ ...node,
146
+ name: {
147
+ ...node.name,
148
+ value: newName
149
+ }
150
+ };
151
+ };
152
+ return visit(doc, {
153
+ FragmentDefinition: rename,
154
+ FragmentSpread: rename
155
+ });
156
+ }
157
+ export {
158
+ createFragmentFromSelection,
159
+ inlineFragment,
160
+ listFragmentInfos,
161
+ listFragments,
162
+ removeFragmentSpread,
163
+ renameFragment
164
+ };
165
+ //# sourceMappingURL=fragments.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fragments.js","sources":["../../src/lib/fragments.ts"],"sourcesContent":["import {\n Kind,\n visit,\n type DocumentNode,\n type FragmentDefinitionNode,\n type FragmentSpreadNode,\n type SelectionSetNode,\n} from 'graphql';\n\nimport {\n findDefinition,\n findSelectionSet,\n mapDefinition,\n removeField,\n replaceFieldSelectionSet,\n type DefinitionTarget,\n type PathSegment,\n} from './ast-path';\nimport { pruneUnusedVariableDefinitions } from './field-selection';\n\n/**\n * Returns the names of all named fragment definitions in `doc`, in document\n * order.\n */\nexport function listFragments(doc: DocumentNode): string[] {\n return doc.definitions\n .filter(\n (d): d is FragmentDefinitionNode => d.kind === Kind.FRAGMENT_DEFINITION,\n )\n .map(f => f.name.value);\n}\n\n/** A named fragment's name paired with its type condition. */\nexport type FragmentInfo = {\n name: string;\n typeName: string;\n};\n\n/**\n * Returns the name and type condition of every named fragment definition in\n * `doc`, in document order.\n */\nexport function listFragmentInfos(doc: DocumentNode): FragmentInfo[] {\n return doc.definitions\n .filter(\n (d): d is FragmentDefinitionNode => d.kind === Kind.FRAGMENT_DEFINITION,\n )\n .map(f => ({ name: f.name.value, typeName: f.typeCondition.name.value }));\n}\n\n/**\n * Extracts the selection set of the field at `path` into a new named fragment\n * definition (`fragmentName on typeName`), and replaces the field's selections\n * with a single fragment spread (`...fragmentName`).\n *\n * Returns a new `DocumentNode`; does not mutate `doc`. Returns the original\n * document unchanged when the path does not resolve to a field with a\n * selection set.\n */\nexport function createFragmentFromSelection(\n doc: DocumentNode,\n path: PathSegment[],\n fragmentName: string,\n typeName: string,\n target: DefinitionTarget,\n): DocumentNode {\n // Resolve the target selection set before entering mapDefinition so we can\n // bail early and avoid building the fragment def when the path is missing.\n const definition = findDefinition(doc, target);\n if (!definition) {\n return doc;\n }\n\n const targetSelectionSet = findSelectionSet(definition.selectionSet, path);\n if (!targetSelectionSet || targetSelectionSet.selections.length === 0) {\n return doc;\n }\n\n const fragmentDef: FragmentDefinitionNode = {\n kind: Kind.FRAGMENT_DEFINITION,\n name: { kind: Kind.NAME, value: fragmentName },\n typeCondition: {\n kind: Kind.NAMED_TYPE,\n name: { kind: Kind.NAME, value: typeName },\n },\n directives: [],\n selectionSet: targetSelectionSet,\n };\n\n const spread: FragmentSpreadNode = {\n kind: Kind.FRAGMENT_SPREAD,\n name: { kind: Kind.NAME, value: fragmentName },\n directives: [],\n };\n\n const spreadSet: SelectionSetNode = {\n kind: Kind.SELECTION_SET,\n selections: [spread],\n };\n\n let changed = false;\n const updatedDoc = mapDefinition(doc, target, def => {\n const newSelectionSet = replaceFieldSelectionSet(\n def.selectionSet,\n path,\n spreadSet,\n );\n if (newSelectionSet === def.selectionSet) {\n return def;\n }\n changed = true;\n return { ...def, selectionSet: newSelectionSet };\n });\n\n if (!changed) {\n return doc;\n }\n\n return {\n ...updatedDoc,\n definitions: [...updatedDoc.definitions, fragmentDef],\n };\n}\n\n/**\n * Removes the `...fragmentName` spread from the selection set of the field at\n * `fieldPath` (or the definition root when `fieldPath` is empty) in the\n * definition addressed by `target`. If that leaves the field with no\n * selections, the field itself is pruned; if it empties an operation, the\n * operation is dropped.\n *\n * Returns a new `DocumentNode`; does not mutate `doc`. Returns the original\n * document unchanged when no such spread is present.\n */\nexport function removeFragmentSpread(\n doc: DocumentNode,\n fieldPath: PathSegment[],\n fragmentName: string,\n target: DefinitionTarget,\n): DocumentNode {\n return mapDefinition(doc, target, def => {\n const targetSet =\n fieldPath.length === 0\n ? def.selectionSet\n : findSelectionSet(def.selectionSet, fieldPath);\n if (!targetSet) {\n return def;\n }\n\n const remaining = targetSet.selections.filter(\n s => !(s.kind === Kind.FRAGMENT_SPREAD && s.name.value === fragmentName),\n );\n if (remaining.length === targetSet.selections.length) {\n return def;\n }\n\n let newSelectionSet: SelectionSetNode;\n if (fieldPath.length === 0) {\n newSelectionSet = { ...def.selectionSet, selections: remaining };\n } else if (remaining.length === 0) {\n // The spread was the field's only selection; drop the field too.\n newSelectionSet = removeField(def.selectionSet, fieldPath);\n } else {\n newSelectionSet = replaceFieldSelectionSet(def.selectionSet, fieldPath, {\n kind: Kind.SELECTION_SET,\n selections: remaining,\n });\n }\n\n if (newSelectionSet.selections.length === 0) {\n // An operation with no selection set is unprintable; a fragment can't be\n // emptied without orphaning its own spreads. Drop the operation; leave\n // the fragment intact.\n return def.kind === Kind.OPERATION_DEFINITION ? null : def;\n }\n if (def.kind === Kind.FRAGMENT_DEFINITION) {\n return { ...def, selectionSet: newSelectionSet };\n }\n // Removing the spread can orphan a variable a field inside it referenced.\n return pruneUnusedVariableDefinitions(\n { ...def, selectionSet: newSelectionSet },\n doc,\n );\n });\n}\n\n/**\n * Deletes the named fragment, inlining it: every `...fragmentName` spread\n * across the document (operations and other fragments alike) is replaced in\n * place by the fragment's own selections, and the fragment definition is\n * removed.\n *\n * Assumes each spread site is type-compatible with the fragment's type\n * condition — the builder only creates spreads on matching-type fields — so the\n * selections are inlined directly rather than wrapped in an `... on Type`\n * inline fragment. Overlapping selections are left as-is (GraphQL merges\n * identically-named fields).\n *\n * Returns a new `DocumentNode`; does not mutate `doc`. Returns the original\n * document unchanged when no fragment named `fragmentName` exists.\n */\nexport function inlineFragment(\n doc: DocumentNode,\n fragmentName: string,\n): DocumentNode {\n const fragment = doc.definitions.find(\n (d): d is FragmentDefinitionNode =>\n d.kind === Kind.FRAGMENT_DEFINITION && d.name.value === fragmentName,\n );\n if (!fragment) {\n return doc;\n }\n const inlined = fragment.selectionSet.selections;\n\n const isTargetSpread = (s: { kind: Kind }) =>\n s.kind === Kind.FRAGMENT_SPREAD &&\n (s as FragmentSpreadNode).name.value === fragmentName;\n\n const rewritten = visit(doc, {\n SelectionSet(node) {\n if (!node.selections.some(isTargetSpread)) {\n return;\n }\n return {\n ...node,\n selections: node.selections.flatMap(s =>\n isTargetSpread(s) ? inlined : [s],\n ),\n };\n },\n });\n\n return {\n ...rewritten,\n definitions: rewritten.definitions.filter(\n d =>\n !(d.kind === Kind.FRAGMENT_DEFINITION && d.name.value === fragmentName),\n ),\n };\n}\n\n/**\n * Renames the named fragment `oldName` to `newName`, updating the definition\n * and every `...oldName` spread across the document (operations and other\n * fragments alike). Returns the original document unchanged when no fragment\n * named `oldName` exists or when `newName` is empty or already taken.\n *\n * Returns a new `DocumentNode`; does not mutate `doc`.\n */\nexport function renameFragment(\n doc: DocumentNode,\n oldName: string,\n newName: string,\n): DocumentNode {\n if (!newName || oldName === newName) {\n return doc;\n }\n const names = new Set(listFragments(doc));\n if (!names.has(oldName) || names.has(newName)) {\n return doc;\n }\n\n const rename = (node: { name: { value: string } }) => {\n if (node.name.value !== oldName) {\n return;\n }\n return { ...node, name: { ...node.name, value: newName } };\n };\n\n return visit(doc, {\n FragmentDefinition: rename,\n FragmentSpread: rename,\n });\n}\n"],"names":["listFragments","doc","definitions","filter","d","kind","Kind","FRAGMENT_DEFINITION","map","f","name","value","listFragmentInfos","typeName","typeCondition","createFragmentFromSelection","path","fragmentName","target","definition","findDefinition","targetSelectionSet","findSelectionSet","selectionSet","selections","length","fragmentDef","NAME","NAMED_TYPE","directives","spread","FRAGMENT_SPREAD","spreadSet","SELECTION_SET","changed","updatedDoc","mapDefinition","def","newSelectionSet","replaceFieldSelectionSet","removeFragmentSpread","fieldPath","targetSet","remaining","s","removeField","OPERATION_DEFINITION","pruneUnusedVariableDefinitions","inlineFragment","fragment","find","inlined","isTargetSpread","rewritten","visit","SelectionSet","node","some","flatMap","renameFragment","oldName","newName","names","Set","has","rename","FragmentDefinition","FragmentSpread"],"mappings":";;;AAwBO,SAASA,cAAcC,KAA6B;AACzD,SAAOA,IAAIC,YACRC,OACC,CAACC,MAAmCA,EAAEC,SAASC,KAAKC,mBACtD,EACCC,IAAIC,CAAKA,MAAAA,EAAEC,KAAKC,KAAK;AAC1B;AAYO,SAASC,kBAAkBX,KAAmC;AAC5DA,SAAAA,IAAIC,YACRC,OACC,CAACC,MAAmCA,EAAEC,SAASC,KAAKC,mBACtD,EACCC,IAAIC,CAAM,OAAA;AAAA,IAAEC,MAAMD,EAAEC,KAAKC;AAAAA,IAAOE,UAAUJ,EAAEK,cAAcJ,KAAKC;AAAAA,EAAAA,EAAQ;AAC5E;AAWO,SAASI,4BACdd,KACAe,MACAC,cACAJ,UACAK,QACc;AAGRC,QAAAA,aAAaC,eAAenB,KAAKiB,MAAM;AAC7C,MAAI,CAACC,YAAY;AACRlB,WAAAA;AAAAA,EAAAA;AAGT,QAAMoB,qBAAqBC,iBAAiBH,WAAWI,cAAcP,IAAI;AACzE,MAAI,CAACK,sBAAsBA,mBAAmBG,WAAWC,WAAW,GAAG;AAC9DxB,WAAAA;AAAAA,EAAAA;AAGT,QAAMyB,cAAsC;AAAA,IAC1CrB,MAAMC,KAAKC;AAAAA,IACXG,MAAM;AAAA,MAAEL,MA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rD,KAAK;AAAA,IAC3BsD,aAAaC,MAAM;AACjB,UAAI,CAACA,KAAKhC,WAAWiC,KAAKL,cAAc,GAAG;AACzC;AAAA,MAAA;AAEK,aAAA;AAAA,QACL,GAAGI;AAAAA,QACHhC,YAAYgC,KAAKhC,WAAWkC,QAAQd,CAAAA,MAClCQ,eAAeR,CAAC,IAAIO,UAAU,CAACP,CAAC,CAClC;AAAA,MACF;AAAA,IAAA;AAAA,EACF,CACD;AAEM,SAAA;AAAA,IACL,GAAGS;AAAAA,IACHnD,aAAamD,UAAUnD,YAAYC,OACjCC,CACE,MAAA,EAAEA,EAAEC,SAASC,KAAKC,uBAAuBH,EAAEM,KAAKC,UAAUM,aAC9D;AAAA,EACF;AACF;AAUgB0C,SAAAA,eACd1D,KACA2D,SACAC,SACc;AACV,MAAA,CAACA,WAAWD,YAAYC,SAAS;AAC5B5D,WAAAA;AAAAA,EAAAA;AAET,QAAM6D,QAAQ,IAAIC,IAAI/D,cAAcC,GAAG,CAAC;AACpC,MAAA,CAAC6D,MAAME,IAAIJ,OAAO,KAAKE,MAAME,IAAIH,OAAO,GAAG;AACtC5D,WAAAA;AAAAA,EAAAA;AAGHgE,QAAAA,SAASA,CAACT,SAAsC;AAChDA,QAAAA,KAAK9C,KAAKC,UAAUiD,SAAS;AAC/B;AAAA,IAAA;AAEK,WAAA;AAAA,MAAE,GAAGJ;AAAAA,MAAM9C,MAAM;AAAA,QAAE,GAAG8C,KAAK9C;AAAAA,QAAMC,OAAOkD;AAAAA,MAAAA;AAAAA,IAAU;AAAA,EAC3D;AAEA,SAAOP,MAAMrD,KAAK;AAAA,IAChBiE,oBAAoBD;AAAAA,IACpBE,gBAAgBF;AAAAA,EAAAA,CACjB;AACH;"}
@@ -0,0 +1,26 @@
1
+ import { DocumentNode } from 'graphql';
2
+ import { DefinitionTarget, PathSegment } from './ast-path';
3
+ /**
4
+ * Returns `true` when the field at `path` in the definition addressed by
5
+ * `target` (an operation or a named fragment) of `doc` already contains an
6
+ * `... on TypeName` inline fragment with the given `typeName`.
7
+ */
8
+ export declare function isInlineFragmentPresent(doc: DocumentNode, path: PathSegment[], typeName: string, target: DefinitionTarget): boolean;
9
+ /**
10
+ * Adds an `... on TypeName { __typename }` inline fragment to the field at
11
+ * `path` in the definition addressed by `target` (an operation or a named
12
+ * fragment) of `doc`. If the fragment is already present this is a no-op.
13
+ *
14
+ * Creates the field's selection set when the field currently has none.
15
+ *
16
+ * Returns a new `DocumentNode`; does not mutate `doc`.
17
+ */
18
+ export declare function addInlineFragment(doc: DocumentNode, path: PathSegment[], typeName: string, target: DefinitionTarget): DocumentNode;
19
+ /**
20
+ * Removes the `... on TypeName` inline fragment from the field at `path` in
21
+ * the definition addressed by `target` (an operation or a named fragment) of
22
+ * `doc`. If no such fragment exists this is a no-op.
23
+ *
24
+ * Returns a new `DocumentNode`; does not mutate `doc`.
25
+ */
26
+ export declare function removeInlineFragment(doc: DocumentNode, path: PathSegment[], typeName: string, target: DefinitionTarget): DocumentNode;
@@ -0,0 +1,71 @@
1
+ import { Kind } from "graphql";
2
+ import { inlineFragmentSegment, fieldSegment, mapDefinition, findDefinition, findNodeAtPath, addField, removeField } from "./ast-path.js";
3
+ import { pruneUnusedVariableDefinitions } from "./field-selection.js";
4
+ function findFieldSelectionSet(doc, path, target) {
5
+ const definition = findDefinition(doc, target);
6
+ if (!definition) {
7
+ return void 0;
8
+ }
9
+ if (path.length === 0) {
10
+ return definition.selectionSet;
11
+ }
12
+ const node = findNodeAtPath(definition.selectionSet, path);
13
+ return node == null ? void 0 : node.selectionSet;
14
+ }
15
+ function isInlineFragmentPresent(doc, path, typeName, target) {
16
+ const selectionSet = findFieldSelectionSet(doc, path, target);
17
+ if (!selectionSet) {
18
+ return false;
19
+ }
20
+ return selectionSet.selections.some((s) => {
21
+ var _a;
22
+ return s.kind === Kind.INLINE_FRAGMENT && ((_a = s.typeCondition) == null ? void 0 : _a.name.value) === typeName;
23
+ });
24
+ }
25
+ function addInlineFragment(doc, path, typeName, target) {
26
+ if (path.length === 0) {
27
+ return doc;
28
+ }
29
+ if (isInlineFragmentPresent(doc, path, typeName, target)) {
30
+ return doc;
31
+ }
32
+ const fragmentPath = [...path, inlineFragmentSegment(typeName), fieldSegment("__typename")];
33
+ return mapDefinition(doc, target, (definition) => ({
34
+ ...definition,
35
+ selectionSet: addField(definition.selectionSet, fragmentPath)
36
+ }));
37
+ }
38
+ function removeInlineFragment(doc, path, typeName, target) {
39
+ if (path.length === 0) {
40
+ return doc;
41
+ }
42
+ const fragmentPath = [...path, inlineFragmentSegment(typeName)];
43
+ return mapDefinition(doc, target, (definition) => {
44
+ const newSelectionSet = removeField(definition.selectionSet, fragmentPath);
45
+ if (newSelectionSet === definition.selectionSet) {
46
+ return definition;
47
+ }
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
+ return pruneUnusedVariableDefinitions({
61
+ ...definition,
62
+ selectionSet: newSelectionSet
63
+ }, doc);
64
+ });
65
+ }
66
+ export {
67
+ addInlineFragment,
68
+ isInlineFragmentPresent,
69
+ removeInlineFragment
70
+ };
71
+ //# sourceMappingURL=inline-fragments.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inline-fragments.js","sources":["../../src/lib/inline-fragments.ts"],"sourcesContent":["import {\n Kind,\n type DocumentNode,\n type InlineFragmentNode,\n type SelectionSetNode,\n} from 'graphql';\n\nimport {\n addField,\n fieldSegment,\n findNodeAtPath,\n findDefinition,\n inlineFragmentSegment,\n mapDefinition,\n removeField,\n type DefinitionTarget,\n type PathSegment,\n} from './ast-path';\nimport { pruneUnusedVariableDefinitions } from './field-selection';\n\nfunction findFieldSelectionSet(\n doc: DocumentNode,\n path: PathSegment[],\n target: DefinitionTarget,\n): SelectionSetNode | undefined {\n const definition = findDefinition(doc, target);\n if (!definition) {\n return undefined;\n }\n\n if (path.length === 0) {\n return definition.selectionSet;\n }\n\n const node = findNodeAtPath(definition.selectionSet, path);\n return node?.selectionSet;\n}\n\n/**\n * Returns `true` when the field at `path` in the definition addressed by\n * `target` (an operation or a named fragment) of `doc` already contains an\n * `... on TypeName` inline fragment with the given `typeName`.\n */\nexport function isInlineFragmentPresent(\n doc: DocumentNode,\n path: PathSegment[],\n typeName: string,\n target: DefinitionTarget,\n): boolean {\n const selectionSet = findFieldSelectionSet(doc, path, target);\n if (!selectionSet) {\n return false;\n }\n return selectionSet.selections.some(\n (s): s is InlineFragmentNode =>\n s.kind === Kind.INLINE_FRAGMENT &&\n s.typeCondition?.name.value === typeName,\n );\n}\n\n/**\n * Adds an `... on TypeName { __typename }` inline fragment to the field at\n * `path` in the definition addressed by `target` (an operation or a named\n * fragment) of `doc`. If the fragment is already present this is a no-op.\n *\n * Creates the field's selection set when the field currently has none.\n *\n * Returns a new `DocumentNode`; does not mutate `doc`.\n */\nexport function addInlineFragment(\n doc: DocumentNode,\n path: PathSegment[],\n typeName: string,\n target: DefinitionTarget,\n): DocumentNode {\n if (path.length === 0) {\n return doc;\n }\n\n if (isInlineFragmentPresent(doc, path, typeName, target)) {\n return doc;\n }\n\n // Add `... on TypeName { __typename }` by treating the fragment as an\n // addressable path segment and seeding it with __typename. addField creates\n // the parent field(s) when absent, so this works even with no prior\n // selection on the abstract field.\n const fragmentPath = [\n ...path,\n inlineFragmentSegment(typeName),\n fieldSegment('__typename'),\n ];\n return mapDefinition(doc, target, definition => ({\n ...definition,\n selectionSet: addField(definition.selectionSet, fragmentPath),\n }));\n}\n\n/**\n * Removes the `... on TypeName` inline fragment from the field at `path` in\n * the definition addressed by `target` (an operation or a named fragment) of\n * `doc`. If no such fragment exists this is a no-op.\n *\n * Returns a new `DocumentNode`; does not mutate `doc`.\n */\nexport function removeInlineFragment(\n doc: DocumentNode,\n path: PathSegment[],\n typeName: string,\n target: DefinitionTarget,\n): DocumentNode {\n if (path.length === 0) {\n return doc;\n }\n\n // Remove the entire inline fragment by treating it as a leaf path segment.\n const fragmentPath = [...path, inlineFragmentSegment(typeName)];\n return mapDefinition(doc, target, definition => {\n const newSelectionSet = removeField(definition.selectionSet, fragmentPath);\n if (newSelectionSet === definition.selectionSet) {\n return definition;\n }\n\n if (newSelectionSet.selections.length === 0) {\n if (definition.kind === Kind.OPERATION_DEFINITION) {\n // Removing the fragment emptied the operation; drop it (an operation\n // with no selection set is unprintable).\n return null;\n }\n // Emptying a named fragment would orphan its spreads; keep it intact.\n return definition;\n }\n\n if (definition.kind === Kind.FRAGMENT_DEFINITION) {\n return { ...definition, selectionSet: newSelectionSet };\n }\n\n // Dropping the fragment can orphan a variable a field inside it used.\n return pruneUnusedVariableDefinitions(\n { ...definition, selectionSet: newSelectionSet },\n doc,\n );\n });\n}\n"],"names":["findFieldSelectionSet","doc","path","target","definition","findDefinition","undefined","length","selectionSet","node","findNodeAtPath","isInlineFragmentPresent","typeName","selections","some","s","kind","Kind","INLINE_FRAGMENT","typeCondition","name","value","addInlineFragment","fragmentPath","inlineFragmentSegment","fieldSegment","mapDefinition","addField","removeInlineFragment","newSelectionSet","removeField","OPERATION_DEFINITION","FRAGMENT_DEFINITION","pruneUnusedVariableDefinitions"],"mappings":";;;AAoBA,SAASA,sBACPC,KACAC,MACAC,QAC8B;AACxBC,QAAAA,aAAaC,eAAeJ,KAAKE,MAAM;AAC7C,MAAI,CAACC,YAAY;AACRE,WAAAA;AAAAA,EAAAA;AAGLJ,MAAAA,KAAKK,WAAW,GAAG;AACrB,WAAOH,WAAWI;AAAAA,EAAAA;AAGpB,QAAMC,OAAOC,eAAeN,WAAWI,cAAcN,IAAI;AACzD,SAAOO,6BAAMD;AACf;AAOO,SAASG,wBACdV,KACAC,MACAU,UACAT,QACS;AACT,QAAMK,eAAeR,sBAAsBC,KAAKC,MAAMC,MAAM;AAC5D,MAAI,CAACK,cAAc;AACV,WAAA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,MAAYI,cAAcqB;AAAAA,OAC/B5B,GACF;AAAA,EAAA,CACD;AACH;"}
@@ -0,0 +1,47 @@
1
+ import { OperationTypeNode, DocumentNode, GraphQLArgument, GraphQLNamedType, GraphQLSchema } from 'graphql';
2
+ import { DefinitionTarget, PathSegment } from './ast-path';
3
+ /**
4
+ * Safely parse the variables JSON text, returning a plain object or {} on
5
+ * failure. Arrays and non-object values are treated as empty.
6
+ */
7
+ export declare function readVariables(text: string | null | undefined): Record<string, unknown>;
8
+ export declare function fieldPathAtOffset(doc: DocumentNode, offset: number): PathSegment[];
9
+ /**
10
+ * Resolves the definition the cursor sits in — an operation or a named
11
+ * fragment — together with the field path within it. Returns `undefined` when
12
+ * the cursor is not inside any editable definition. Drives cursor-following:
13
+ * moving the editor cursor into a fragment switches the builder to editing it.
14
+ */
15
+ export declare function cursorContextAtOffset(doc: DocumentNode, offset: number): {
16
+ target: DefinitionTarget;
17
+ path: PathSegment[];
18
+ } | undefined;
19
+ /**
20
+ * Extracts the raw, printable value string for an argument at `path` from the
21
+ * document, e.g. `"abc"` for a string or `42` for an int. Returns an empty
22
+ * string when the field, arg, or a value we can render is not found. Walks both
23
+ * field and inline-fragment path segments.
24
+ */
25
+ export declare function extractRawArgValue(doc: DocumentNode, path: PathSegment[], argName: string, operationName?: string): string;
26
+ /** Counts the fields selected in the target operation (nested fields included). */
27
+ export declare function countSelectedFields(doc: DocumentNode, operationName?: string): number;
28
+ /**
29
+ * Resolves the schema `GraphQLArgument` for the argument `argName` on the field
30
+ * addressed by `path`, walking from the root type for `activeOpKind`. Returns
31
+ * undefined when the field or arg can't be resolved against the schema.
32
+ */
33
+ export declare function resolveSchemaArg(schema: GraphQLSchema, activeOpKind: OperationTypeNode | undefined, path: PathSegment[], argName: string): GraphQLArgument | undefined;
34
+ /**
35
+ * Like {@link resolveSchemaArg} but walks from an explicit `rootType` rather
36
+ * than an operation kind, so it resolves args inside a fragment (whose root is
37
+ * its type condition) as well as inside an operation.
38
+ */
39
+ export declare function resolveSchemaArgFromRoot(schema: GraphQLSchema, rootType: GraphQLNamedType | undefined, path: PathSegment[], argName: string): GraphQLArgument | undefined;
40
+ /**
41
+ * Resolves the named type reached by walking `path` from the root type for
42
+ * `activeOpKind`. Field segments descend into the field's type; `inlineFragment`
43
+ * segments switch to that type condition. Returns undefined when a segment
44
+ * doesn't resolve. Used to pick the type condition when extracting a field's
45
+ * selection into a fragment.
46
+ */
47
+ export declare function resolveFieldNamedType(schema: GraphQLSchema, activeOpKind: OperationTypeNode | undefined, path: PathSegment[]): GraphQLNamedType | undefined;
@@ -0,0 +1,177 @@
1
+ import { visit, Kind, OperationTypeNode, isObjectType, isInterfaceType, getNamedType } from "graphql";
2
+ import { fieldSegment, inlineFragmentSegment } from "./ast-path.js";
3
+ function readVariables(text) {
4
+ if (!text || !text.trim()) {
5
+ return {};
6
+ }
7
+ try {
8
+ const parsed = JSON.parse(text);
9
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
10
+ } catch {
11
+ return {};
12
+ }
13
+ }
14
+ function pathWithinSelections(rootSelections, offset) {
15
+ var _a;
16
+ const path = [];
17
+ let selections = rootSelections;
18
+ while (true) {
19
+ 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);
20
+ if (!selection) {
21
+ break;
22
+ }
23
+ if (selection.kind === Kind.FIELD) {
24
+ path.push(fieldSegment(selection.name.value));
25
+ } else {
26
+ const typeName = (_a = selection.typeCondition) == null ? void 0 : _a.name.value;
27
+ if (!typeName) {
28
+ break;
29
+ }
30
+ path.push(inlineFragmentSegment(typeName));
31
+ }
32
+ if (!selection.selectionSet) {
33
+ break;
34
+ }
35
+ selections = selection.selectionSet.selections;
36
+ }
37
+ return path;
38
+ }
39
+ function cursorContextAtOffset(doc, offset) {
40
+ var _a;
41
+ 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);
42
+ if ((def == null ? void 0 : def.kind) === Kind.OPERATION_DEFINITION) {
43
+ return {
44
+ target: {
45
+ kind: "operation",
46
+ name: (_a = def.name) == null ? void 0 : _a.value
47
+ },
48
+ path: pathWithinSelections(def.selectionSet.selections, offset)
49
+ };
50
+ }
51
+ if ((def == null ? void 0 : def.kind) === Kind.FRAGMENT_DEFINITION) {
52
+ return {
53
+ target: {
54
+ kind: "fragment",
55
+ name: def.name.value
56
+ },
57
+ path: pathWithinSelections(def.selectionSet.selections, offset)
58
+ };
59
+ }
60
+ return void 0;
61
+ }
62
+ function findOperationDefinition(doc, operationName) {
63
+ const byName = operationName ? doc.definitions.find((d) => {
64
+ var _a;
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;
69
+ }
70
+ function selectionMatchesSegment(selection, segment) {
71
+ var _a;
72
+ switch (segment.kind) {
73
+ case "inlineFragment":
74
+ return selection.kind === Kind.INLINE_FRAGMENT && ((_a = selection.typeCondition) == null ? void 0 : _a.name.value) === segment.typeName;
75
+ case "field":
76
+ return selection.kind === Kind.FIELD && selection.name.value === segment.name;
77
+ }
78
+ }
79
+ function extractRawArgValue(doc, path, argName, operationName) {
80
+ const op = findOperationDefinition(doc, operationName);
81
+ if (!op) {
82
+ return "";
83
+ }
84
+ let selections = op.selectionSet.selections;
85
+ for (let i = 0; i < path.length; i++) {
86
+ const seg = path[i];
87
+ const node = selections.find((s) => selectionMatchesSegment(s, seg));
88
+ if (!node) {
89
+ return "";
90
+ }
91
+ if (i === path.length - 1) {
92
+ if (node.kind !== Kind.FIELD) {
93
+ return "";
94
+ }
95
+ const argNode = (node.arguments ?? []).find((a) => a.name.value === argName);
96
+ if (!argNode) {
97
+ return "";
98
+ }
99
+ const v = argNode.value;
100
+ if (v.kind === Kind.STRING) {
101
+ return `"${v.value}"`;
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 "";
120
+ }
121
+ function countSelectedFields(doc, operationName) {
122
+ let count = 0;
123
+ const op = findOperationDefinition(doc, operationName);
124
+ if (op) {
125
+ visit(op, {
126
+ Field() {
127
+ count++;
128
+ }
129
+ });
130
+ }
131
+ return count;
132
+ }
133
+ function resolveSchemaArg(schema, activeOpKind, path, argName) {
134
+ const rootType = schema.getRootType(activeOpKind ?? OperationTypeNode.QUERY) ?? schema.getQueryType();
135
+ return resolveSchemaArgFromRoot(schema, rootType ?? void 0, path, argName);
136
+ }
137
+ function resolveSchemaArgFromRoot(schema, rootType, path, argName) {
138
+ if (!rootType || path.length === 0) {
139
+ return;
140
+ }
141
+ let currentType = rootType;
142
+ let targetField;
143
+ for (const seg of path) {
144
+ if (seg.kind === "inlineFragment") {
145
+ const condition = schema.getType(seg.typeName);
146
+ if (!condition) {
147
+ return;
148
+ }
149
+ currentType = condition;
150
+ targetField = void 0;
151
+ continue;
152
+ }
153
+ if (!(isObjectType(currentType) || isInterfaceType(currentType))) {
154
+ return;
155
+ }
156
+ const fields = currentType.getFields();
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);
168
+ }
169
+ export {
170
+ countSelectedFields,
171
+ cursorContextAtOffset,
172
+ extractRawArgValue,
173
+ readVariables,
174
+ resolveSchemaArg,
175
+ resolveSchemaArgFromRoot
176
+ };
177
+ //# sourceMappingURL=schema-walk.js.map
@@ -0,0 +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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AOO,SAAS2B,yBACdP,QACAE,UACA1D,MACAoC,SAC6B;AAC7B,MAAI,CAACsB,YAAY1D,KAAKsC,WAAW,GAAG;AAClC;AAAA,EAAA;AAGF,MAAI0B,cAAgCN;AAChCO,MAAAA;AACJ,aAAW1B,OAAOvC,MAAM;AAClBuC,QAAAA,IAAIlC,SAAS,kBAAkB;AACjC,YAAM6D,YAAYV,OAAOW,QAAQ5B,IAAIvB,QAAQ;AAC7C,UAAI,CAACkD,WAAW;AACd;AAAA,MAAA;AAEYA,oBAAAA;AACAtC,oBAAAA;AACd;AAAA,IAAA;AAEF,QAAI,EAAEwC,aAAaJ,WAAW,KAAKK,gBAAgBL,WAAW,IAAI;AAChE;AAAA,IAAA;AAEIM,UAAAA,SAASN,YAAYO,UAAU;AAC/BC,UAAAA,IAAIF,OAAO/B,IAAIzB,IAAI;AAGzB,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;"}
@@ -0,0 +1,33 @@
1
+ import { DocumentNode, SelectionSetNode, ValueNode } from 'graphql';
2
+ import { PathSegment } from './ast-path';
3
+ /**
4
+ * Suggests a variable name for `argName` that does not collide with any
5
+ * existing variable definition anywhere in `doc`. The check spans every
6
+ * operation because the variables editor is a single document-wide JSON
7
+ * object: two operations sharing a variable name would share its value.
8
+ * Returns `argName` when no collision exists, otherwise appends `_2`, `_3`, etc.
9
+ */
10
+ export declare function suggestVarName(doc: DocumentNode, argName: string): string;
11
+ /**
12
+ * Promotes a scalar argument on the field at `path` to a variable reference.
13
+ * Adds `($varName: type = defaultRaw)` to the target operation's (by name, or
14
+ * the first operation when unspecified) variable definitions and replaces the
15
+ * inline argument value with `$varName`.
16
+ *
17
+ * When the operation is anonymous (shorthand query), it is converted to an
18
+ * explicit `query` operation so that variable definitions can be added.
19
+ *
20
+ * Returns a new `DocumentNode`; does not mutate `doc`.
21
+ */
22
+ export declare function promoteArgToVariable(doc: DocumentNode, path: PathSegment[], argName: string, varName: string, type: string, defaultRaw: string, operationName?: string): DocumentNode;
23
+ export declare function replaceVariableInSelectionSet(selectionSet: SelectionSetNode, varName: string, defaultValue: ValueNode | undefined): SelectionSetNode;
24
+ /**
25
+ * Demotes a variable back to an inline literal. Removes the variable
26
+ * definition for `varName` from the target operation (by name, or the first
27
+ * operation when unspecified) and replaces every `$varName` argument reference
28
+ * with `inlineValue` when provided, otherwise the variable's default value
29
+ * (or removes the arg entirely when there is neither).
30
+ *
31
+ * Returns a new `DocumentNode`; does not mutate `doc`.
32
+ */
33
+ export declare function demoteVariable(doc: DocumentNode, varName: string, operationName?: string, inlineValue?: ValueNode): DocumentNode;