@graphiql/plugin-query-builder 1.0.0-beta.1 → 1.0.0-beta.3

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 (53) hide show
  1. package/LICENSE +21 -0
  2. package/dist/components/arg-input/arg-input-by-type.js +441 -444
  3. package/dist/components/arg-input/arg-input-by-type.js.map +1 -1
  4. package/dist/components/arg-input/enum-arg-control.js +49 -52
  5. package/dist/components/arg-input/enum-arg-control.js.map +1 -1
  6. package/dist/components/arg-input/index.js +44 -36
  7. package/dist/components/arg-input/index.js.map +1 -1
  8. package/dist/components/arg-input/scalar-arg-control.js +58 -63
  9. package/dist/components/arg-input/scalar-arg-control.js.map +1 -1
  10. package/dist/components/field-row.js +212 -197
  11. package/dist/components/field-row.js.map +1 -1
  12. package/dist/components/field-tree-context.js +23 -27
  13. package/dist/components/field-tree-context.js.map +1 -1
  14. package/dist/components/field-tree-node.js +128 -105
  15. package/dist/components/field-tree-node.js.map +1 -1
  16. package/dist/components/field-tree.js +178 -199
  17. package/dist/components/field-tree.js.map +1 -1
  18. package/dist/components/fragment-editor.js +157 -151
  19. package/dist/components/fragment-editor.js.map +1 -1
  20. package/dist/components/fragment-section.js +215 -203
  21. package/dist/components/fragment-section.js.map +1 -1
  22. package/dist/components/query-builder.js +205 -181
  23. package/dist/components/query-builder.js.map +1 -1
  24. package/dist/components/type-condition-selector.js +417 -462
  25. package/dist/components/type-condition-selector.js.map +1 -1
  26. package/dist/components/use-cursor-path.js +69 -70
  27. package/dist/components/use-cursor-path.js.map +1 -1
  28. package/dist/components/use-cursor-reveal.js +83 -89
  29. package/dist/components/use-cursor-reveal.js.map +1 -1
  30. package/dist/components/use-working-document.js +432 -527
  31. package/dist/components/use-working-document.js.map +1 -1
  32. package/dist/index.js +11 -11
  33. package/dist/index.js.map +1 -1
  34. package/dist/lib/arg-value.js +132 -139
  35. package/dist/lib/arg-value.js.map +1 -1
  36. package/dist/lib/arguments.js +54 -55
  37. package/dist/lib/arguments.js.map +1 -1
  38. package/dist/lib/ast-path.js +266 -291
  39. package/dist/lib/ast-path.js.map +1 -1
  40. package/dist/lib/field-list-view.js +33 -43
  41. package/dist/lib/field-list-view.js.map +1 -1
  42. package/dist/lib/field-selection.js +63 -67
  43. package/dist/lib/field-selection.js.map +1 -1
  44. package/dist/lib/fragments.js +166 -152
  45. package/dist/lib/fragments.js.map +1 -1
  46. package/dist/lib/inline-fragments.js +64 -62
  47. package/dist/lib/inline-fragments.js.map +1 -1
  48. package/dist/lib/schema-walk.js +141 -158
  49. package/dist/lib/schema-walk.js.map +1 -1
  50. package/dist/lib/variables.js +96 -90
  51. package/dist/lib/variables.js.map +1 -1
  52. package/dist/style.css +4 -22
  53. package/package.json +21 -18
@@ -1,165 +1,179 @@
1
- import { Kind, visit } from "graphql";
2
- import { mapDefinition, findDefinition, findSelectionSet, replaceFieldSelectionSet, removeField } from "./ast-path.js";
1
+ import { findDefinition, findSelectionSet, mapDefinition, removeField, replaceFieldSelectionSet } from "./ast-path.js";
3
2
  import { pruneUnusedVariableDefinitions } from "./field-selection.js";
3
+ import { Kind, visit } from "graphql";
4
+ //#region src/lib/fragments.ts
5
+ /**
6
+ * Returns the names of all named fragment definitions in `doc`, in document
7
+ * order.
8
+ */
4
9
  function listFragments(doc) {
5
- return doc.definitions.filter((d) => d.kind === Kind.FRAGMENT_DEFINITION).map((f) => f.name.value);
10
+ return doc.definitions.filter((d) => d.kind === Kind.FRAGMENT_DEFINITION).map((f) => f.name.value);
6
11
  }
12
+ /**
13
+ * Returns the name and type condition of every named fragment definition in
14
+ * `doc`, in document order.
15
+ */
7
16
  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
- }));
17
+ return doc.definitions.filter((d) => d.kind === Kind.FRAGMENT_DEFINITION).map((f) => ({
18
+ name: f.name.value,
19
+ typeName: f.typeCondition.name.value
20
+ }));
12
21
  }
22
+ /**
23
+ * Extracts the selection set of the field at `path` into a new named fragment
24
+ * definition (`fragmentName on typeName`), and replaces the field's selections
25
+ * with a single fragment spread (`...fragmentName`).
26
+ *
27
+ * Returns a new `DocumentNode`; does not mutate `doc`. Returns the original
28
+ * document unchanged when the path does not resolve to a field with a
29
+ * selection set.
30
+ */
13
31
  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
- };
32
+ const definition = findDefinition(doc, target);
33
+ if (!definition) return doc;
34
+ const targetSelectionSet = findSelectionSet(definition.selectionSet, path);
35
+ if (!targetSelectionSet || targetSelectionSet.selections.length === 0) return doc;
36
+ const fragmentDef = {
37
+ kind: Kind.FRAGMENT_DEFINITION,
38
+ name: {
39
+ kind: Kind.NAME,
40
+ value: fragmentName
41
+ },
42
+ typeCondition: {
43
+ kind: Kind.NAMED_TYPE,
44
+ name: {
45
+ kind: Kind.NAME,
46
+ value: typeName
47
+ }
48
+ },
49
+ directives: [],
50
+ selectionSet: targetSelectionSet
51
+ };
52
+ const spread = {
53
+ kind: Kind.FRAGMENT_SPREAD,
54
+ name: {
55
+ kind: Kind.NAME,
56
+ value: fragmentName
57
+ },
58
+ directives: []
59
+ };
60
+ const spreadSet = {
61
+ kind: Kind.SELECTION_SET,
62
+ selections: [spread]
63
+ };
64
+ let changed = false;
65
+ const updatedDoc = mapDefinition(doc, target, (def) => {
66
+ const newSelectionSet = replaceFieldSelectionSet(def.selectionSet, path, spreadSet);
67
+ if (newSelectionSet === def.selectionSet) return def;
68
+ changed = true;
69
+ return {
70
+ ...def,
71
+ selectionSet: newSelectionSet
72
+ };
73
+ });
74
+ if (!changed) return doc;
75
+ return {
76
+ ...updatedDoc,
77
+ definitions: [...updatedDoc.definitions, fragmentDef]
78
+ };
69
79
  }
80
+ /**
81
+ * Removes the `...fragmentName` spread from the selection set of the field at
82
+ * `fieldPath` (or the definition root when `fieldPath` is empty) in the
83
+ * definition addressed by `target`. If that leaves the field with no
84
+ * selections, the field itself is pruned; if it empties an operation, the
85
+ * operation is dropped.
86
+ *
87
+ * Returns a new `DocumentNode`; does not mutate `doc`. Returns the original
88
+ * document unchanged when no such spread is present.
89
+ */
70
90
  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
- });
91
+ return mapDefinition(doc, target, (def) => {
92
+ const targetSet = fieldPath.length === 0 ? def.selectionSet : findSelectionSet(def.selectionSet, fieldPath);
93
+ if (!targetSet) return def;
94
+ const remaining = targetSet.selections.filter((s) => !(s.kind === Kind.FRAGMENT_SPREAD && s.name.value === fragmentName));
95
+ if (remaining.length === targetSet.selections.length) return def;
96
+ let newSelectionSet;
97
+ if (fieldPath.length === 0) newSelectionSet = {
98
+ ...def.selectionSet,
99
+ selections: remaining
100
+ };
101
+ else if (remaining.length === 0) newSelectionSet = removeField(def.selectionSet, fieldPath);
102
+ else newSelectionSet = replaceFieldSelectionSet(def.selectionSet, fieldPath, {
103
+ kind: Kind.SELECTION_SET,
104
+ selections: remaining
105
+ });
106
+ if (newSelectionSet.selections.length === 0) return def.kind === Kind.OPERATION_DEFINITION ? null : def;
107
+ if (def.kind === Kind.FRAGMENT_DEFINITION) return {
108
+ ...def,
109
+ selectionSet: newSelectionSet
110
+ };
111
+ return pruneUnusedVariableDefinitions({
112
+ ...def,
113
+ selectionSet: newSelectionSet
114
+ }, doc);
115
+ });
108
116
  }
117
+ /**
118
+ * Deletes the named fragment, inlining it: every `...fragmentName` spread
119
+ * across the document (operations and other fragments alike) is replaced in
120
+ * place by the fragment's own selections, and the fragment definition is
121
+ * removed.
122
+ *
123
+ * Assumes each spread site is type-compatible with the fragment's type
124
+ * condition — the builder only creates spreads on matching-type fields — so the
125
+ * selections are inlined directly rather than wrapped in an `... on Type`
126
+ * inline fragment. Overlapping selections are left as-is (GraphQL merges
127
+ * identically-named fields).
128
+ *
129
+ * Returns a new `DocumentNode`; does not mutate `doc`. Returns the original
130
+ * document unchanged when no fragment named `fragmentName` exists.
131
+ */
109
132
  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
- };
133
+ const fragment = doc.definitions.find((d) => d.kind === Kind.FRAGMENT_DEFINITION && d.name.value === fragmentName);
134
+ if (!fragment) return doc;
135
+ const inlined = fragment.selectionSet.selections;
136
+ const isTargetSpread = (s) => s.kind === Kind.FRAGMENT_SPREAD && s.name.value === fragmentName;
137
+ const rewritten = visit(doc, { SelectionSet(node) {
138
+ if (!node.selections.some(isTargetSpread)) return;
139
+ return {
140
+ ...node,
141
+ selections: node.selections.flatMap((s) => isTargetSpread(s) ? inlined : [s])
142
+ };
143
+ } });
144
+ return {
145
+ ...rewritten,
146
+ definitions: rewritten.definitions.filter((d) => !(d.kind === Kind.FRAGMENT_DEFINITION && d.name.value === fragmentName))
147
+ };
131
148
  }
149
+ /**
150
+ * Renames the named fragment `oldName` to `newName`, updating the definition
151
+ * and every `...oldName` spread across the document (operations and other
152
+ * fragments alike). Returns the original document unchanged when no fragment
153
+ * named `oldName` exists or when `newName` is empty or already taken.
154
+ *
155
+ * Returns a new `DocumentNode`; does not mutate `doc`.
156
+ */
132
157
  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
- });
158
+ if (!newName || oldName === newName) return doc;
159
+ const names = new Set(listFragments(doc));
160
+ if (!names.has(oldName) || names.has(newName)) return doc;
161
+ const rename = (node) => {
162
+ if (node.name.value !== oldName) return;
163
+ return {
164
+ ...node,
165
+ name: {
166
+ ...node.name,
167
+ value: newName
168
+ }
169
+ };
170
+ };
171
+ return visit(doc, {
172
+ FragmentDefinition: rename,
173
+ FragmentSpread: rename
174
+ });
156
175
  }
157
- export {
158
- createFragmentFromSelection,
159
- inlineFragment,
160
- listFragmentInfos,
161
- listFragments,
162
- removeFragmentSpread,
163
- renameFragment
164
- };
165
- //# sourceMappingURL=fragments.js.map
176
+ //#endregion
177
+ export { createFragmentFromSelection, inlineFragment, listFragmentInfos, listFragments, removeFragmentSpread, renameFragment };
178
+
179
+ //# sourceMappingURL=fragments.js.map
@@ -1 +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,qBA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AAAA,IACpBE,gBAAgBF;AAAAA,EAAAA,CACjB;AACH;"}
1
+ {"version":3,"file":"fragments.js","names":["Kind","visit","DocumentNode","FragmentDefinitionNode","FragmentSpreadNode","SelectionSetNode","findDefinition","findSelectionSet","mapDefinition","removeField","replaceFieldSelectionSet","DefinitionTarget","PathSegment","pruneUnusedVariableDefinitions","listFragments","doc","definitions","filter","d","kind","FRAGMENT_DEFINITION","map","f","name","value","FragmentInfo","typeName","listFragmentInfos","typeCondition","createFragmentFromSelection","path","fragmentName","target","definition","targetSelectionSet","selectionSet","selections","length","fragmentDef","NAME","NAMED_TYPE","directives","spread","FRAGMENT_SPREAD","spreadSet","SELECTION_SET","changed","updatedDoc","def","newSelectionSet","removeFragmentSpread","fieldPath","targetSet","remaining","s","OPERATION_DEFINITION","inlineFragment","fragment","find","inlined","isTargetSpread","rewritten","SelectionSet","node","some","flatMap","renameFragment","oldName","newName","names","Set","has","rename","FragmentDefinition","FragmentSpread"],"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"],"mappings":";;;;;;;;AAwBA,SAAgBc,cAAcC,KAA6B;CACzD,OAAOA,IAAIC,YACRC,QACEC,MAAmCA,EAAEC,SAASnB,KAAKoB,mBACtD,CAAC,CACAC,KAAIC,MAAKA,EAAEC,KAAKC,KAAK;AAC1B;;;;;AAYA,SAAgBG,k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kBN,MACtBA,EAAEnC,SAASnB,KAAK2C,mBACfW,EAAyB/B,KAAKC,UAAUO;CAE3C,MAAM8B,YAAY5D,MAAMc,KAAK,EAC3B+C,aAAaC,MAAM;EACjB,IAAI,CAACA,KAAK3B,WAAW4B,KAAKJ,cAAc,GACtC;EAEF,OAAO;GACL,GAAGG;GACH3B,YAAY2B,KAAK3B,WAAW6B,SAAQX,MAClCM,eAAeN,CAAC,IAAIK,UAAU,CAACL,CAAC,CAClC;EACF;CACF,EACF,CAAC;CAED,OAAO;EACL,GAAGO;EACH7C,aAAa6C,UAAU7C,YAAYC,QACjCC,MACE,EAAEA,EAAEC,SAASnB,KAAKoB,uBAAuBF,EAAEK,KAAKC,UAAUO,aAC9D;CACF;AACF;;;;;;;;;AAUA,SAAgBmC,eACdnD,KACAoD,SACAC,SACc;CACd,IAAI,CAACA,WAAWD,YAAYC,SAC1B,OAAOrD;CAET,MAAMsD,QAAQ,IAAIC,IAAIxD,cAAcC,GAAG,CAAC;CACxC,IAAI,CAACsD,MAAME,IAAIJ,OAAO,KAAKE,MAAME,IAAIH,OAAO,GAC1C,OAAOrD;CAGT,MAAMyD,UAAUT,SAAsC;EACpD,IAAIA,KAAKxC,KAAKC,UAAU2C,SACtB;EAEF,OAAO;GAAE,GAAGJ;GAAMxC,MAAM;IAAE,GAAGwC,KAAKxC;IAAMC,OAAO4C;GAAQ;EAAE;CAC3D;CAEA,OAAOnE,MAAMc,KAAK;EAChB0D,oBAAoBD;EACpBE,gBAAgBF;CAClB,CAAC;AACH"}
@@ -1,71 +1,73 @@
1
- import { Kind } from "graphql";
2
- import { inlineFragmentSegment, fieldSegment, mapDefinition, findDefinition, findNodeAtPath, addField, removeField } from "./ast-path.js";
1
+ import { addField, fieldSegment, findDefinition, findNodeAtPath, inlineFragmentSegment, mapDefinition, removeField } from "./ast-path.js";
3
2
  import { pruneUnusedVariableDefinitions } from "./field-selection.js";
3
+ import { Kind } from "graphql";
4
+ //#region src/lib/inline-fragments.ts
4
5
  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;
6
+ const definition = findDefinition(doc, target);
7
+ if (!definition) return;
8
+ if (path.length === 0) return definition.selectionSet;
9
+ return findNodeAtPath(definition.selectionSet, path)?.selectionSet;
14
10
  }
11
+ /**
12
+ * Returns `true` when the field at `path` in the definition addressed by
13
+ * `target` (an operation or a named fragment) of `doc` already contains an
14
+ * `... on TypeName` inline fragment with the given `typeName`.
15
+ */
15
16
  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
- });
17
+ const selectionSet = findFieldSelectionSet(doc, path, target);
18
+ if (!selectionSet) return false;
19
+ return selectionSet.selections.some((s) => s.kind === Kind.INLINE_FRAGMENT && s.typeCondition?.name.value === typeName);
24
20
  }
21
+ /**
22
+ * Adds an `... on TypeName { __typename }` inline fragment to the field at
23
+ * `path` in the definition addressed by `target` (an operation or a named
24
+ * fragment) of `doc`. If the fragment is already present this is a no-op.
25
+ *
26
+ * Creates the field's selection set when the field currently has none.
27
+ *
28
+ * Returns a new `DocumentNode`; does not mutate `doc`.
29
+ */
25
30
  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
- }));
31
+ if (path.length === 0) return doc;
32
+ if (isInlineFragmentPresent(doc, path, typeName, target)) return doc;
33
+ const fragmentPath = [
34
+ ...path,
35
+ inlineFragmentSegment(typeName),
36
+ fieldSegment("__typename")
37
+ ];
38
+ return mapDefinition(doc, target, (definition) => ({
39
+ ...definition,
40
+ selectionSet: addField(definition.selectionSet, fragmentPath)
41
+ }));
37
42
  }
43
+ /**
44
+ * Removes the `... on TypeName` inline fragment from the field at `path` in
45
+ * the definition addressed by `target` (an operation or a named fragment) of
46
+ * `doc`. If no such fragment exists this is a no-op.
47
+ *
48
+ * Returns a new `DocumentNode`; does not mutate `doc`.
49
+ */
38
50
  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
- });
51
+ if (path.length === 0) return doc;
52
+ const fragmentPath = [...path, inlineFragmentSegment(typeName)];
53
+ return mapDefinition(doc, target, (definition) => {
54
+ const newSelectionSet = removeField(definition.selectionSet, fragmentPath);
55
+ if (newSelectionSet === definition.selectionSet) return definition;
56
+ if (newSelectionSet.selections.length === 0) {
57
+ if (definition.kind === Kind.OPERATION_DEFINITION) return null;
58
+ return definition;
59
+ }
60
+ if (definition.kind === Kind.FRAGMENT_DEFINITION) return {
61
+ ...definition,
62
+ selectionSet: newSelectionSet
63
+ };
64
+ return pruneUnusedVariableDefinitions({
65
+ ...definition,
66
+ selectionSet: newSelectionSet
67
+ }, doc);
68
+ });
65
69
  }
66
- export {
67
- addInlineFragment,
68
- isInlineFragmentPresent,
69
- removeInlineFragment
70
- };
71
- //# sourceMappingURL=inline-fragments.js.map
70
+ //#endregion
71
+ export { addInlineFragment, isInlineFragmentPresent, removeInlineFragment };
72
+
73
+ //# sourceMappingURL=inline-fragments.js.map
@@ -1 +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;AAAA,EAAA;AAET,SAAOA,aAAaK,WAAWC,KAC7B,CAACC,MACCA;;AAAAA,aAAEC,SAASC,KAAKC,qBAChBH,OAAEI,kBAAFJ,mBAAiBK,KAAKC,WAAUT;AAAAA,GACpC;AACF;AAWO,SAASU,kBACdrB,KACAC,MACAU,UACAT,QACc;AACVD,MAAAA,KAAKK,WAAW,GAAG;AACdN,WAAAA;AAAAA,EAAAA;AAGT,MAAIU,wBAAwBV,KAAKC,MAAMU,UAAUT,MAAM,GAAG;AACjDF,WAAAA;AAAAA,EAAAA;AAOHsB,QAAAA,eAAe,CACnB,GAAGrB,MACHsB,sBAAsBZ,QAAQ,GAC9Ba,aAAa,YAAY,CAAC;AAErBC,SAAAA,cAAczB,KAAKE,QAAQC,CAAe,gBAAA;AAAA,IAC/C,GAAGA;AAAAA,IACHI,cAAcmB,SAASvB,WAAWI,cAAce,YAAY;AAAA,EAAA,EAC5D;AACJ;AASO,SAASK,qBACd3B,KACAC,MACAU,UACAT,QACc;AACVD,MAAAA,KAAKK,WAAW,GAAG;AACdN,WAAAA;AAAAA,EAAAA;AAIT,QAAMsB,eAAe,CAAC,GAAGrB,MAAMsB,sBAAsBZ,QAAQ,CAAC;AACvDc,SAAAA,cAAczB,KAAKE,QAAQC,CAAc,eAAA;AAC9C,UAAMyB,kBAAkBC,YAAY1B,WAAWI,cAAce,YAAY;AACrEM,QAAAA,oBAAoBzB,WAAWI,cAAc;AACxCJ,aAAAA;AAAAA,IAAAA;AAGLyB,QAAAA,gBAAgBhB,WAAWN,WAAW,GAAG;AACvCH,UAAAA,WAAWY,SAASC,KAAKc,sBAAsB;AAG1C,eAAA;AAAA,MAAA;AAGF3B,aAAAA;AAAAA,IAAAA;AAGLA,QAAAA,WAAWY,SAASC,KAAKe,qBAAqB;AACzC,aAAA;AAAA,QAAE,GAAG5B;AAAAA,QAAYI,cAAcqB;AAAAA,MAAgB;AAAA,IAAA;AAIxD,WAAOI,+BACL;AAAA,MAAE,GAAG7B;AAAAA,MAAYI,cAAcqB;AAAAA,OAC/B5B,GACF;AAAA,EAAA,CACD;AACH;"}
1
+ {"version":3,"file":"inline-fragments.js","names":["Kind","DocumentNode","InlineFragmentNode","SelectionSetNode","addField","fieldSegment","findNodeAtPath","findDefinition","inlineFragmentSegment","mapDefinition","removeField","DefinitionTarget","PathSegment","pruneUnusedVariableDefinitions","findFieldSelectionSet","doc","path","target","definition","undefined","length","selectionSet","node","isInlineFragmentPresent","typeName","selections","some","s","kind","INLINE_FRAGMENT","typeCondition","name","value","addInlineFragment","fragmentPath","removeInlineFragment","newSelectionSet","OPERATION_DEFINITION","FRAGMENT_DEFINITION"],"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"],"mappings":";;;;AAoBA,SAASc,sBACPC,KACAC,MACAC,QAC8B;CAC9B,MAAMC,aAAaX,eAAeQ,KAAKE,MAAM;CAC7C,IAAI,CAACC,YACH;CAGF,IAAIF,KAAKI,WAAW,GAClB,OAAOF,WAAWG;CAIpB,OADaf,eAAeY,WAAWG,cAAcL,IAC9CM,CAAAA,EAAMD;AACf;;;;;;AAOA,SAAgBE,wBACdR,KACAC,MACAQ,UACAP,QACS;CACT,MAAMI,eAAeP,sBAAsBC,KAAKC,MAAMC,MAAM;CAC5D,IAAI,CAACI,cACH,OAAO;CAET,OAAOA,aAAaI,WAAWC,MAC5BC,MACCA,EAAEC,SAAS5B,KAAK6B,mBAChBF,EAAEG,eAAeC,KAAKC,UAAUR,QACpC;AACF;;;;;;;;;;AAWA,SAAgBS,kBACdlB,KACAC,MACAQ,UACAP,QACc;CACd,IAAID,KAAKI,WAAW,GAClB,OAAOL;CAG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