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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +1 @@
1
- {"version":3,"file":"ast-path.js","sources":["../../src/lib/ast-path.ts"],"sourcesContent":["import {\n Kind,\n type ArgumentNode,\n type DocumentNode,\n type FieldNode,\n type FragmentDefinitionNode,\n type InlineFragmentNode,\n type OperationDefinitionNode,\n type SelectionNode,\n type SelectionSetNode,\n type ValueNode,\n} from 'graphql';\n\nexport type PathSegment =\n | { kind: 'field'; name: string }\n | { kind: 'inlineFragment'; typeName: string };\n\nexport type Path = PathSegment[];\n\n/**\n * The definition the builder is currently editing: either an operation\n * (identified by name, or the first operation when unnamed) or a named\n * fragment. Every read/mutation helper resolves the selection set it works on\n * from this target, so the same tree and handlers drive operation and fragment\n * editing alike.\n */\nexport type DefinitionTarget =\n | { kind: 'operation'; name?: string }\n | { kind: 'fragment'; name: string };\n\n/** A definition the builder can edit: an operation or a named fragment. */\nexport type EditableDefinitionNode =\n | OperationDefinitionNode\n | FragmentDefinitionNode;\n\nexport function fieldSegment(name: string): PathSegment {\n return { kind: 'field', name };\n}\n\n/** Builds the path segment that addresses the `... on TypeName` inline fragment. */\nexport function inlineFragmentSegment(typeName: string): PathSegment {\n return { kind: 'inlineFragment', typeName };\n}\n\nexport function segmentsEqual(a: PathSegment, b: PathSegment): boolean {\n if (a.kind !== b.kind) {\n return false;\n }\n if (a.kind === 'field' && b.kind === 'field') {\n return a.name === b.name;\n }\n if (a.kind === 'inlineFragment' && b.kind === 'inlineFragment') {\n return a.typeName === b.typeName;\n }\n return false;\n}\n\n/**\n * True when `selection` is the field or inline fragment named by `segment`.\n *\n * Fields are matched by schema name, not response key: the builder's tree is\n * driven by the schema, so a path segment is a field name and never carries an\n * alias. A document the user hand-aliased (`a: hero b: hero`) therefore matches\n * the first selection with that name. Helpers that walk to a single node act\n * on that first match only (see `mapNodeAtPath`), so unchecking an aliased\n * field removes one occurrence rather than wiping every alias of it. Full\n * alias-aware editing would require a selection-driven tree and is out of scope.\n */\nexport function selectionMatchesSegment(\n selection: SelectionNode,\n segment: PathSegment,\n): boolean {\n switch (segment.kind) {\n case 'inlineFragment':\n return (\n selection.kind === Kind.INLINE_FRAGMENT &&\n selection.typeCondition?.name.value === segment.typeName\n );\n case 'field':\n return (\n selection.kind === Kind.FIELD && selection.name.value === segment.name\n );\n }\n}\n\n/**\n * Walks `path` through `selectionSet` via `selectionMatchesSegment` and\n * returns the node at the final segment, or `undefined` if any segment is\n * missing.\n */\nexport function findNodeAtPath(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n): FieldNode | InlineFragmentNode | undefined {\n let current: SelectionSetNode = selectionSet;\n for (let i = 0; i < path.length; i++) {\n const segment = path[i]!;\n const node = current.selections.find(s =>\n selectionMatchesSegment(s, segment),\n ) as FieldNode | InlineFragmentNode | undefined;\n if (!node) {\n return undefined;\n }\n if (i === path.length - 1) {\n return node;\n }\n if (!node.selectionSet) {\n return undefined;\n }\n current = node.selectionSet;\n }\n return undefined;\n}\n\n/**\n * Immutably rebuilds `selectionSet`, descending to the node at `path` via\n * `selectionMatchesSegment`, then applying `fn` to it. `fn` returns the\n * replacement node, or `undefined` to delete it. When `pruneEmptyParent` is\n * true (default), a non-leaf ancestor whose child selection set becomes empty\n * after the operation is itself pruned. Returns the original `selectionSet`\n * reference unchanged when `path` does not resolve.\n */\nexport function mapNodeAtPath(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n fn: (\n node: FieldNode | InlineFragmentNode,\n ) => FieldNode | InlineFragmentNode | null,\n pruneEmptyParent = true,\n): SelectionSetNode {\n const [segment, ...rest] = path as [PathSegment, ...PathSegment[]];\n const nodeIndex = selectionSet.selections.findIndex(s =>\n selectionMatchesSegment(s, segment),\n );\n if (nodeIndex === -1) {\n return selectionSet;\n }\n\n const node = selectionSet.selections[nodeIndex] as\n | FieldNode\n | InlineFragmentNode;\n\n if (rest.length === 0) {\n const replacement = fn(node);\n const newSelections = [...selectionSet.selections];\n if (replacement === null) {\n newSelections.splice(nodeIndex, 1);\n } else {\n newSelections[nodeIndex] = replacement;\n }\n return { ...selectionSet, selections: newSelections };\n }\n\n if (!node.selectionSet) {\n return selectionSet;\n }\n const newChildSet = mapNodeAtPath(\n node.selectionSet,\n rest,\n fn,\n pruneEmptyParent,\n );\n if (newChildSet === node.selectionSet) {\n return selectionSet;\n }\n\n if (pruneEmptyParent && newChildSet.selections.length === 0) {\n const newSelections = selectionSet.selections.filter(\n (_, i) => i !== nodeIndex,\n );\n return { ...selectionSet, selections: newSelections };\n }\n\n const updated = { ...node, selectionSet: newChildSet } as\n | FieldNode\n | InlineFragmentNode;\n const newSelections = [...selectionSet.selections];\n newSelections[nodeIndex] = updated;\n return { ...selectionSet, selections: newSelections };\n}\n\n/** Creates the field or inline fragment for `segment`, with the given child set. */\nexport function createSelectionForSegment(\n segment: PathSegment,\n selectionSet?: SelectionSetNode,\n): FieldNode | InlineFragmentNode {\n switch (segment.kind) {\n case 'inlineFragment':\n return {\n kind: Kind.INLINE_FRAGMENT,\n typeCondition: {\n kind: Kind.NAMED_TYPE,\n name: {\n kind: Kind.NAME,\n value: segment.typeName,\n },\n },\n directives: [],\n selectionSet: selectionSet ?? {\n kind: Kind.SELECTION_SET,\n selections: [],\n },\n };\n case 'field':\n return {\n kind: Kind.FIELD,\n name: { kind: Kind.NAME, value: segment.name },\n arguments: [],\n directives: [],\n selectionSet,\n };\n }\n}\n\nexport function addField(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n): SelectionSetNode {\n const [segment, ...rest] = path as [PathSegment, ...PathSegment[]];\n\n if (rest.length === 0) {\n const alreadyPresent = selectionSet.selections.some(s =>\n selectionMatchesSegment(s, segment),\n );\n if (alreadyPresent) {\n return selectionSet;\n }\n\n const newNode = createSelectionForSegment(segment);\n return {\n ...selectionSet,\n selections: [...selectionSet.selections, newNode],\n };\n }\n\n const existingIndex = selectionSet.selections.findIndex(s =>\n selectionMatchesSegment(s, segment),\n );\n\n if (existingIndex === -1) {\n // Parent node doesn't exist yet; create it and recurse to add the child.\n const emptySet: SelectionSetNode = {\n kind: Kind.SELECTION_SET,\n selections: [],\n };\n const parentNode = createSelectionForSegment(\n segment,\n addField(emptySet, rest),\n );\n return {\n ...selectionSet,\n selections: [...selectionSet.selections, parentNode],\n };\n }\n\n const existing = selectionSet.selections[existingIndex] as\n | FieldNode\n | InlineFragmentNode;\n const childSet: SelectionSetNode = existing.selectionSet ?? {\n kind: Kind.SELECTION_SET,\n selections: [],\n };\n const updated = {\n ...existing,\n selectionSet: addField(childSet, rest),\n } as FieldNode | InlineFragmentNode;\n const newSelections = [...selectionSet.selections];\n newSelections[existingIndex] = updated;\n return { ...selectionSet, selections: newSelections };\n}\n\nexport function removeField(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n): SelectionSetNode {\n // Delete the terminal node (fn returns null); pruneEmptyParent=true\n // ensures any ancestor whose child set became empty is also pruned.\n return mapNodeAtPath(selectionSet, path, () => null, true);\n}\n\nexport function replaceFieldSelectionSet(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n newSet: SelectionSetNode,\n): SelectionSetNode {\n return mapNodeAtPath(\n selectionSet,\n path,\n node =>\n ({ ...node, selectionSet: newSet }) as FieldNode | InlineFragmentNode,\n false,\n );\n}\n\nexport function setArgInSelectionSet(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n argName: string,\n value: ValueNode | undefined,\n): SelectionSetNode {\n return mapNodeAtPath(\n selectionSet,\n path,\n node => {\n // Arguments live on fields, not inline fragments.\n const field = node as FieldNode;\n const existingArgs: readonly ArgumentNode[] = field.arguments ?? [];\n let newArgs: ArgumentNode[];\n if (value === undefined) {\n newArgs = existingArgs.filter(a => a.name.value !== argName);\n } else {\n const existingIndex = existingArgs.findIndex(\n a => a.name.value === argName,\n );\n const newArg: ArgumentNode = {\n kind: Kind.ARGUMENT,\n name: { kind: Kind.NAME, value: argName },\n value,\n };\n if (existingIndex === -1) {\n newArgs = [...existingArgs, newArg];\n } else {\n newArgs = [...existingArgs];\n newArgs[existingIndex] = newArg;\n }\n }\n return { ...field, arguments: newArgs };\n },\n false, // do not prune empty parents when setting args\n );\n}\n\nexport function findSelectionSet(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n): SelectionSetNode | undefined {\n const node = findNodeAtPath(selectionSet, path);\n return node?.selectionSet;\n}\n\n/**\n * Returns the index in `doc.definitions` of the operation to operate on:\n * the operation whose name matches `operationName` when given and present,\n * otherwise the first operation definition. Returns -1 when the document has\n * no operation definitions.\n */\nexport function findOperationIndex(\n doc: DocumentNode,\n operationName?: string,\n): number {\n if (operationName) {\n const named = doc.definitions.findIndex(\n d =>\n d.kind === Kind.OPERATION_DEFINITION && d.name?.value === operationName,\n );\n if (named !== -1) {\n return named;\n }\n }\n return doc.definitions.findIndex(d => d.kind === Kind.OPERATION_DEFINITION);\n}\n\n/**\n * Resolves the target operation node (see {@link findOperationIndex}), or\n * `undefined` when the document has no matching operation.\n */\nexport function findOperation(\n doc: DocumentNode,\n operationName?: string,\n): OperationDefinitionNode | undefined {\n const index = findOperationIndex(doc, operationName);\n if (index === -1) {\n return undefined;\n }\n const operation = doc.definitions[index];\n return operation?.kind === Kind.OPERATION_DEFINITION ? operation : undefined;\n}\n\n/**\n * Finds the target operation, calls `fn` with it, and returns a new\n * `DocumentNode` with the result spliced back into `definitions`. When `fn`\n * returns `null` the operation is removed entirely (used when an operation\n * becomes empty and unprintable). Returns `doc` unchanged when no matching\n * operation definition exists.\n */\nexport function mapOperation(\n doc: DocumentNode,\n operationName: string | undefined,\n fn: (operation: OperationDefinitionNode) => OperationDefinitionNode | null,\n): DocumentNode {\n const index = findOperationIndex(doc, operationName);\n if (index === -1) {\n return doc;\n }\n const def = doc.definitions[index];\n if (def?.kind !== Kind.OPERATION_DEFINITION) {\n return doc;\n }\n const result = fn(def);\n // When fn returns the same reference, the operation is unchanged — return\n // the original doc so callers that rely on reference equality for no-op\n // detection (e.g. toBe checks) continue to work.\n if (result === def) {\n return doc;\n }\n const newDefinitions = [...doc.definitions];\n if (result === null) {\n newDefinitions.splice(index, 1);\n } else {\n newDefinitions[index] = result;\n }\n return { ...doc, definitions: newDefinitions };\n}\n\n/**\n * Returns the index in `doc.definitions` of the definition addressed by\n * `target` — the matching operation (see {@link findOperationIndex}) or the\n * named fragment. Returns -1 when no such definition exists.\n */\nexport function findDefinitionIndex(\n doc: DocumentNode,\n target: DefinitionTarget,\n): number {\n if (target.kind === 'operation') {\n return findOperationIndex(doc, target.name);\n }\n return doc.definitions.findIndex(\n d => d.kind === Kind.FRAGMENT_DEFINITION && d.name.value === target.name,\n );\n}\n\n/**\n * Resolves the definition node addressed by `target`, or `undefined` when the\n * document has no matching definition.\n */\nexport function findDefinition(\n doc: DocumentNode,\n target: DefinitionTarget,\n): EditableDefinitionNode | undefined {\n const index = findDefinitionIndex(doc, target);\n if (index === -1) {\n return undefined;\n }\n const def = doc.definitions[index];\n if (\n def?.kind === Kind.OPERATION_DEFINITION ||\n def?.kind === Kind.FRAGMENT_DEFINITION\n ) {\n return def;\n }\n return undefined;\n}\n\n/**\n * Finds the definition addressed by `target`, calls `fn` with it, and returns a\n * new `DocumentNode` with the result spliced back into `definitions`. When `fn`\n * returns `null` the definition is removed entirely. Returns `doc` unchanged\n * when no matching definition exists or `fn` returns the same reference.\n */\nexport function mapDefinition(\n doc: DocumentNode,\n target: DefinitionTarget,\n fn: (definition: EditableDefinitionNode) => EditableDefinitionNode | null,\n): DocumentNode {\n const index = findDefinitionIndex(doc, target);\n if (index === -1) {\n return doc;\n }\n const def = doc.definitions[index];\n if (\n def?.kind !== Kind.OPERATION_DEFINITION &&\n def?.kind !== Kind.FRAGMENT_DEFINITION\n ) {\n return doc;\n }\n const result = fn(def);\n if (result === def) {\n return doc;\n }\n const newDefinitions = [...doc.definitions];\n if (result === null) {\n newDefinitions.splice(index, 1);\n } else {\n newDefinitions[index] = result;\n }\n return { ...doc, definitions: newDefinitions };\n}\n\n/**\n * A source offset just inside the selection set of the definition addressed by\n * `target`, or `undefined` when it can't be located. Used to move the editor\n * cursor into a definition so the builder — which follows the cursor — switches\n * to editing it. Requires `doc` to carry location info (parse without\n * `noLocation`).\n */\nexport function definitionSelectionOffset(\n doc: DocumentNode,\n target: DefinitionTarget,\n): number | undefined {\n const loc = findDefinition(doc, target)?.selectionSet.loc;\n return loc ? loc.start + 1 : undefined;\n}\n"],"names":["fieldSegment","name","kind","inlineFragmentSegment","typeName","segmentsEqual","a","b","selectionMatchesSegment","selection","segment","Kind","INLINE_FRAGMENT","typeCondition","value","FIELD","findNodeAtPath","selectionSet","path","current","i","length","node","selections","find","s","undefined","mapNodeAtPath","fn","pruneEmptyParent","rest","nodeIndex","findIndex","replacement","newSelections","splice","newChildSet","filter","_","updated","createSelectionForSegment","NAMED_TYPE","NAME","directives","SELECTION_SET","arguments","addField","alreadyPresent","some","newNode","existingIndex","emptySet","parentNode","existing","childSet","removeField","replaceFieldSelectionSet","newSet","setArgInSelectionSet","argName","field","existingArgs","newArgs","newArg","ARGUMENT","findSelectionSet","findOperationIndex","doc","operationName","named","definitions","d","OPERATION_DEFINITION","findOperation","index","operation","mapOperation","def","result","newDefinitions","findDefinitionIndex","target","FRAGMENT_DEFINITION","findDefinition","mapDefinition","definitionSelectionOffset","loc","start"],"mappings":";AAmCO,SAASA,aAAaC,MAA2B;AAC/C,SA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I,KAAK;AACjC,OACEG,2BAAK3E,UAASS,KAAK6D,yBACnBK,2BAAK3E,UAASS,KAAKuE,qBACnB;AACOf,WAAAA;AAAAA,EAAAA;AAEHW,QAAAA,SAASlD,GAAGiD,GAAG;AACrB,MAAIC,WAAWD,KAAK;AACXV,WAAAA;AAAAA,EAAAA;AAET,QAAMY,iBAAiB,CAAC,GAAGZ,IAAIG,WAAW;AAC1C,MAAIQ,WAAW,MAAM;AACJ3C,mBAAAA,OAAOuC,OAAO,CAAC;AAAA,EAAA,OACzB;AACLK,mBAAeL,KAAK,IAAII;AAAAA,EAAAA;AAEnB,SAAA;AAAA,IAAE,GAAGX;AAAAA,IAAKG,aAAaS;AAAAA,EAAe;AAC/C;AASgBM,SAAAA,0BACdlB,KACAc,QACoB;;AACpB,QAAMK,OAAMH,oBAAehB,KAAKc,MAAM,MAA1BE,mBAA6BlE,aAAaqE;AAC/CA,SAAAA,MAAMA,IAAIC,QAAQ,IAAI7D;AAC/B;"}
1
+ {"version":3,"file":"ast-path.js","names":["Kind","ArgumentNode","DocumentNode","FieldNode","FragmentDefinitionNode","InlineFragmentNode","OperationDefinitionNode","SelectionNode","SelectionSetNode","ValueNode","PathSegment","kind","name","typeName","Path","DefinitionTarget","EditableDefinitionNode","fieldSegment","inlineFragmentSegment","segmentsEqual","a","b","selectionMatchesSegment","selection","segment","INLINE_FRAGMENT","typeCondition","value","FIELD","findNodeAtPath","selectionSet","path","current","i","length","node","selections","find","s","undefined","mapNodeAtPath","fn","pruneEmptyParent","rest","nodeIndex","findIndex","replacement","newSelections","splice","newChildSet","filter","_","updated","createSelectionForSegment","NAMED_TYPE","NAME","directives","SELECTION_SET","arguments","addField","alreadyPresent","some","newNode","existingIndex","emptySet","parentNode","existing","childSet","removeField","replaceFieldSelectionSet","newSet","setArgInSelectionSet","argName","field","existingArgs","newArgs","newArg","ARGUMENT","findSelectionSet","findOperationIndex","doc","operationName","named","definitions","d","OPERATION_DEFINITION","findOperation","index","operation","mapOperation","def","result","newDefinitions","findDefinitionIndex","target","FRAGMENT_DEFINITION","findDefinition","mapDefinition","definition","definitionSelectionOffset","loc","start"],"sources":["../../src/lib/ast-path.ts"],"sourcesContent":["import {\n Kind,\n type ArgumentNode,\n type DocumentNode,\n type FieldNode,\n type FragmentDefinitionNode,\n type InlineFragmentNode,\n type OperationDefinitionNode,\n type SelectionNode,\n type SelectionSetNode,\n type ValueNode,\n} from 'graphql';\n\nexport type PathSegment =\n | { kind: 'field'; name: string }\n | { kind: 'inlineFragment'; typeName: string };\n\nexport type Path = PathSegment[];\n\n/**\n * The definition the builder is currently editing: either an operation\n * (identified by name, or the first operation when unnamed) or a named\n * fragment. Every read/mutation helper resolves the selection set it works on\n * from this target, so the same tree and handlers drive operation and fragment\n * editing alike.\n */\nexport type DefinitionTarget =\n | { kind: 'operation'; name?: string }\n | { kind: 'fragment'; name: string };\n\n/** A definition the builder can edit: an operation or a named fragment. */\nexport type EditableDefinitionNode =\n | OperationDefinitionNode\n | FragmentDefinitionNode;\n\nexport function fieldSegment(name: string): PathSegment {\n return { kind: 'field', name };\n}\n\n/** Builds the path segment that addresses the `... on TypeName` inline fragment. */\nexport function inlineFragmentSegment(typeName: string): PathSegment {\n return { kind: 'inlineFragment', typeName };\n}\n\nexport function segmentsEqual(a: PathSegment, b: PathSegment): boolean {\n if (a.kind !== b.kind) {\n return false;\n }\n if (a.kind === 'field' && b.kind === 'field') {\n return a.name === b.name;\n }\n if (a.kind === 'inlineFragment' && b.kind === 'inlineFragment') {\n return a.typeName === b.typeName;\n }\n return false;\n}\n\n/**\n * True when `selection` is the field or inline fragment named by `segment`.\n *\n * Fields are matched by schema name, not response key: the builder's tree is\n * driven by the schema, so a path segment is a field name and never carries an\n * alias. A document the user hand-aliased (`a: hero b: hero`) therefore matches\n * the first selection with that name. Helpers that walk to a single node act\n * on that first match only (see `mapNodeAtPath`), so unchecking an aliased\n * field removes one occurrence rather than wiping every alias of it. Full\n * alias-aware editing would require a selection-driven tree and is out of scope.\n */\nexport function selectionMatchesSegment(\n selection: SelectionNode,\n segment: PathSegment,\n): boolean {\n switch (segment.kind) {\n case 'inlineFragment':\n return (\n selection.kind === Kind.INLINE_FRAGMENT &&\n selection.typeCondition?.name.value === segment.typeName\n );\n case 'field':\n return (\n selection.kind === Kind.FIELD && selection.name.value === segment.name\n );\n }\n}\n\n/**\n * Walks `path` through `selectionSet` via `selectionMatchesSegment` and\n * returns the node at the final segment, or `undefined` if any segment is\n * missing.\n */\nexport function findNodeAtPath(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n): FieldNode | InlineFragmentNode | undefined {\n let current: SelectionSetNode = selectionSet;\n for (let i = 0; i < path.length; i++) {\n const segment = path[i]!;\n const node = current.selections.find(s =>\n selectionMatchesSegment(s, segment),\n ) as FieldNode | InlineFragmentNode | undefined;\n if (!node) {\n return undefined;\n }\n if (i === path.length - 1) {\n return node;\n }\n if (!node.selectionSet) {\n return undefined;\n }\n current = node.selectionSet;\n }\n return undefined;\n}\n\n/**\n * Immutably rebuilds `selectionSet`, descending to the node at `path` via\n * `selectionMatchesSegment`, then applying `fn` to it. `fn` returns the\n * replacement node, or `undefined` to delete it. When `pruneEmptyParent` is\n * true (default), a non-leaf ancestor whose child selection set becomes empty\n * after the operation is itself pruned. Returns the original `selectionSet`\n * reference unchanged when `path` does not resolve.\n */\nexport function mapNodeAtPath(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n fn: (\n node: FieldNode | InlineFragmentNode,\n ) => FieldNode | InlineFragmentNode | null,\n pruneEmptyParent = true,\n): SelectionSetNode {\n const [segment, ...rest] = path as [PathSegment, ...PathSegment[]];\n const nodeIndex = selectionSet.selections.findIndex(s =>\n selectionMatchesSegment(s, segment),\n );\n if (nodeIndex === -1) {\n return selectionSet;\n }\n\n const node = selectionSet.selections[nodeIndex] as\n | FieldNode\n | InlineFragmentNode;\n\n if (rest.length === 0) {\n const replacement = fn(node);\n const newSelections = [...selectionSet.selections];\n if (replacement === null) {\n newSelections.splice(nodeIndex, 1);\n } else {\n newSelections[nodeIndex] = replacement;\n }\n return { ...selectionSet, selections: newSelections };\n }\n\n if (!node.selectionSet) {\n return selectionSet;\n }\n const newChildSet = mapNodeAtPath(\n node.selectionSet,\n rest,\n fn,\n pruneEmptyParent,\n );\n if (newChildSet === node.selectionSet) {\n return selectionSet;\n }\n\n if (pruneEmptyParent && newChildSet.selections.length === 0) {\n const newSelections = selectionSet.selections.filter(\n (_, i) => i !== nodeIndex,\n );\n return { ...selectionSet, selections: newSelections };\n }\n\n const updated = { ...node, selectionSet: newChildSet } as\n | FieldNode\n | InlineFragmentNode;\n const newSelections = [...selectionSet.selections];\n newSelections[nodeIndex] = updated;\n return { ...selectionSet, selections: newSelections };\n}\n\n/** Creates the field or inline fragment for `segment`, with the given child set. */\nexport function createSelectionForSegment(\n segment: PathSegment,\n selectionSet?: SelectionSetNode,\n): FieldNode | InlineFragmentNode {\n switch (segment.kind) {\n case 'inlineFragment':\n return {\n kind: Kind.INLINE_FRAGMENT,\n typeCondition: {\n kind: Kind.NAMED_TYPE,\n name: {\n kind: Kind.NAME,\n value: segment.typeName,\n },\n },\n directives: [],\n selectionSet: selectionSet ?? {\n kind: Kind.SELECTION_SET,\n selections: [],\n },\n };\n case 'field':\n return {\n kind: Kind.FIELD,\n name: { kind: Kind.NAME, value: segment.name },\n arguments: [],\n directives: [],\n selectionSet,\n };\n }\n}\n\nexport function addField(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n): SelectionSetNode {\n const [segment, ...rest] = path as [PathSegment, ...PathSegment[]];\n\n if (rest.length === 0) {\n const alreadyPresent = selectionSet.selections.some(s =>\n selectionMatchesSegment(s, segment),\n );\n if (alreadyPresent) {\n return selectionSet;\n }\n\n const newNode = createSelectionForSegment(segment);\n return {\n ...selectionSet,\n selections: [...selectionSet.selections, newNode],\n };\n }\n\n const existingIndex = selectionSet.selections.findIndex(s =>\n selectionMatchesSegment(s, segment),\n );\n\n if (existingIndex === -1) {\n // Parent node doesn't exist yet; create it and recurse to add the child.\n const emptySet: SelectionSetNode = {\n kind: Kind.SELECTION_SET,\n selections: [],\n };\n const parentNode = createSelectionForSegment(\n segment,\n addField(emptySet, rest),\n );\n return {\n ...selectionSet,\n selections: [...selectionSet.selections, parentNode],\n };\n }\n\n const existing = selectionSet.selections[existingIndex] as\n | FieldNode\n | InlineFragmentNode;\n const childSet: SelectionSetNode = existing.selectionSet ?? {\n kind: Kind.SELECTION_SET,\n selections: [],\n };\n const updated = {\n ...existing,\n selectionSet: addField(childSet, rest),\n } as FieldNode | InlineFragmentNode;\n const newSelections = [...selectionSet.selections];\n newSelections[existingIndex] = updated;\n return { ...selectionSet, selections: newSelections };\n}\n\nexport function removeField(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n): SelectionSetNode {\n // Delete the terminal node (fn returns null); pruneEmptyParent=true\n // ensures any ancestor whose child set became empty is also pruned.\n return mapNodeAtPath(selectionSet, path, () => null, true);\n}\n\nexport function replaceFieldSelectionSet(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n newSet: SelectionSetNode,\n): SelectionSetNode {\n return mapNodeAtPath(\n selectionSet,\n path,\n node =>\n ({ ...node, selectionSet: newSet }) as FieldNode | InlineFragmentNode,\n false,\n );\n}\n\nexport function setArgInSelectionSet(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n argName: string,\n value: ValueNode | undefined,\n): SelectionSetNode {\n return mapNodeAtPath(\n selectionSet,\n path,\n node => {\n // Arguments live on fields, not inline fragments.\n const field = node as FieldNode;\n const existingArgs: readonly ArgumentNode[] = field.arguments ?? [];\n let newArgs: ArgumentNode[];\n if (value === undefined) {\n newArgs = existingArgs.filter(a => a.name.value !== argName);\n } else {\n const existingIndex = existingArgs.findIndex(\n a => a.name.value === argName,\n );\n const newArg: ArgumentNode = {\n kind: Kind.ARGUMENT,\n name: { kind: Kind.NAME, value: argName },\n value,\n };\n if (existingIndex === -1) {\n newArgs = [...existingArgs, newArg];\n } else {\n newArgs = [...existingArgs];\n newArgs[existingIndex] = newArg;\n }\n }\n return { ...field, arguments: newArgs };\n },\n false, // do not prune empty parents when setting args\n );\n}\n\nexport function findSelectionSet(\n selectionSet: SelectionSetNode,\n path: PathSegment[],\n): SelectionSetNode | undefined {\n const node = findNodeAtPath(selectionSet, path);\n return node?.selectionSet;\n}\n\n/**\n * Returns the index in `doc.definitions` of the operation to operate on:\n * the operation whose name matches `operationName` when given and present,\n * otherwise the first operation definition. Returns -1 when the document has\n * no operation definitions.\n */\nexport function findOperationIndex(\n doc: DocumentNode,\n operationName?: string,\n): number {\n if (operationName) {\n const named = doc.definitions.findIndex(\n d =>\n d.kind === Kind.OPERATION_DEFINITION && d.name?.value === operationName,\n );\n if (named !== -1) {\n return named;\n }\n }\n return doc.definitions.findIndex(d => d.kind === Kind.OPERATION_DEFINITION);\n}\n\n/**\n * Resolves the target operation node (see {@link findOperationIndex}), or\n * `undefined` when the document has no matching operation.\n */\nexport function findOperation(\n doc: DocumentNode,\n operationName?: string,\n): OperationDefinitionNode | undefined {\n const index = findOperationIndex(doc, operationName);\n if (index === -1) {\n return undefined;\n }\n const operation = doc.definitions[index];\n return operation?.kind === Kind.OPERATION_DEFINITION ? operation : undefined;\n}\n\n/**\n * Finds the target operation, calls `fn` with it, and returns a new\n * `DocumentNode` with the result spliced back into `definitions`. When `fn`\n * returns `null` the operation is removed entirely (used when an operation\n * becomes empty and unprintable). Returns `doc` unchanged when no matching\n * operation definition exists.\n */\nexport function mapOperation(\n doc: DocumentNode,\n operationName: string | undefined,\n fn: (operation: OperationDefinitionNode) => OperationDefinitionNode | null,\n): DocumentNode {\n const index = findOperationIndex(doc, operationName);\n if (index === -1) {\n return doc;\n }\n const def = doc.definitions[index];\n if (def?.kind !== Kind.OPERATION_DEFINITION) {\n return doc;\n }\n const result = fn(def);\n // When fn returns the same reference, the operation is unchanged — return\n // the original doc so callers that rely on reference equality for no-op\n // detection (e.g. toBe checks) continue to work.\n if (result === def) {\n return doc;\n }\n const newDefinitions = [...doc.definitions];\n if (result === null) {\n newDefinitions.splice(index, 1);\n } else {\n newDefinitions[index] = result;\n }\n return { ...doc, definitions: newDefinitions };\n}\n\n/**\n * Returns the index in `doc.definitions` of the definition addressed by\n * `target` — the matching operation (see {@link findOperationIndex}) or the\n * named fragment. Returns -1 when no such definition exists.\n */\nexport function findDefinitionIndex(\n doc: DocumentNode,\n target: DefinitionTarget,\n): number {\n if (target.kind === 'operation') {\n return findOperationIndex(doc, target.name);\n }\n return doc.definitions.findIndex(\n d => d.kind === Kind.FRAGMENT_DEFINITION && d.name.value === target.name,\n );\n}\n\n/**\n * Resolves the definition node addressed by `target`, or `undefined` when the\n * document has no matching definition.\n */\nexport function findDefinition(\n doc: DocumentNode,\n target: DefinitionTarget,\n): EditableDefinitionNode | undefined {\n const index = findDefinitionIndex(doc, target);\n if (index === -1) {\n return undefined;\n }\n const def = doc.definitions[index];\n if (\n def?.kind === Kind.OPERATION_DEFINITION ||\n def?.kind === Kind.FRAGMENT_DEFINITION\n ) {\n return def;\n }\n return undefined;\n}\n\n/**\n * Finds the definition addressed by `target`, calls `fn` with it, and returns a\n * new `DocumentNode` with the result spliced back into `definitions`. When `fn`\n * returns `null` the definition is removed entirely. Returns `doc` unchanged\n * when no matching definition exists or `fn` returns the same reference.\n */\nexport function mapDefinition(\n doc: DocumentNode,\n target: DefinitionTarget,\n fn: (definition: EditableDefinitionNode) => EditableDefinitionNode | null,\n): DocumentNode {\n const index = findDefinitionIndex(doc, target);\n if (index === -1) {\n return doc;\n }\n const def = doc.definitions[index];\n if (\n def?.kind !== Kind.OPERATION_DEFINITION &&\n def?.kind !== Kind.FRAGMENT_DEFINITION\n ) {\n return doc;\n }\n const result = fn(def);\n if (result === def) {\n return doc;\n }\n const newDefinitions = [...doc.definitions];\n if (result === null) {\n newDefinitions.splice(index, 1);\n } else {\n newDefinitions[index] = result;\n }\n return { ...doc, definitions: newDefinitions };\n}\n\n/**\n * A source offset just inside the selection set of the definition addressed by\n * `target`, or `undefined` when it can't be located. Used to move the editor\n * cursor into a definition so the builder — which follows the cursor — switches\n * to editing it. Requires `doc` to carry location info (parse without\n * `noLocation`).\n */\nexport function definitionSelectionOffset(\n doc: DocumentNode,\n target: DefinitionTarget,\n): number | undefined {\n const loc = findDefinition(doc, target)?.selectionSet.loc;\n return loc ? loc.start + 1 : undefined;\n}\n"],"mappings":";;AAmCA,SAAgBiB,aAAaL,MAA2B;CACtD,OAAO;EAAED,MAAM;EAASC;CAAK;AAC/B;;AAGA,SAAgBM,sBAAsBL,UAA+B;CACnE,OAAO;EAAEF,MAAM;EAAkBE;CAAS;AAC5C;AAEA,SAAgBM,cAAcC,GAAgBC,GAAyB;CACrE,IAAID,EAAET,SAASU,EAAEV,MACf,OAAO;CAET,IAAIS,EAAET,SAAS,WAAWU,EAAEV,SAAS,SACnC,OAAOS,EAAER,SAASS,EAAET;CAEtB,IAAIQ,EAAET,SAAS,oBAAoBU,EAAEV,SAAS,kBAC5C,OAAOS,EAAEP,aAAaQ,EAAER;CAE1B,OAAO;AACT;;;;;;;;;;;;AAaA,SAAgBS,wBACdC,WACAC,SACS;CACT,QAAQA,QAAQb,MAAhB;EACE,KAAK,kBACH,OACEY,UAAUZ,SAASX,KAAKyB,mBACxBF,UAAUG,eAAed,KAAKe,UAAUH,QAAQX;EAEpD,KAAK,SACH,OACEU,UAAUZ,SAASX,KAAK4B,SAASL,UAAUX,KAAKe,UAAUH,QAAQZ;CAExE;AACF;;;;;;AAOA,SAAgBiB,eACdC,cACAC,MAC4C;CAC5C,IAAIC,UAA4BF;CAChC,KAAK,IAAIG,IAAI,GAAGA,IAAIF,KAAKG,QAAQD,KAAK;EACpC,MAAMT,UAAUO,KAAKE;EACrB,MAAME,OAAOH,QAAQI,WAAWC,MAAKC,MACnChB,wBAAwBgB,GAAGd,OAAO,CACpC;EACA,IAAI,CAACW,MACH;EAEF,IAAIF,MAAMF,KAAKG,SAAS,GACtB,OAAOC;EAET,IAAI,CAACA,KAAKL,cACR;EAEFE,UAAUG,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BAAoBb,KAAKc,MAAM;CAC7C,IAAIP,UAAU,IACZ,OAAOP;CAET,MAAMU,MAAMV,IAAIG,YAAYI;CAC5B,IACEG,KAAK/E,SAASX,KAAKqF,wBACnBK,KAAK/E,SAASX,KAAK+F,qBAEnB,OAAOf;CAET,MAAMW,SAASlD,GAAGiD,GAAG;CACrB,IAAIC,WAAWD,KACb,OAAOV;CAET,MAAMY,iBAAiB,CAAC,GAAGZ,IAAIG,WAAW;CAC1C,IAAIQ,WAAW,MACbC,eAAe5C,OAAOuC,OAAO,CAAC;MAE9BK,eAAeL,SAASI;CAE1B,OAAO;EAAE,GAAGX;EAAKG,aAAaS;CAAe;AAC/C;;;;;;;;AASA,SAAgBO,0BACdnB,KACAc,QACoB;CACpB,MAAMM,MAAMJ,eAAehB,KAAKc,MAAM,CAAC,EAAEhE,aAAasE;CACtD,OAAOA,MAAMA,IAAIC,QAAQ,IAAI9D,KAAAA;AAC/B"}
@@ -1,47 +1,37 @@
1
1
  import { getNamedType } from "graphql";
2
- const FIELD_LIST_THRESHOLD = 20;
2
+ //#region src/lib/field-list-view.ts
3
+ /**
4
+ * Case-insensitive substring match over a field's name, description, and named
5
+ * type. An empty/whitespace query matches everything (treated as "no filter").
6
+ */
3
7
  function fieldMatchesFilter(field, query) {
4
- var _a;
5
- const q = query.trim().toLowerCase();
6
- if (q === "") {
7
- return true;
8
- }
9
- if (field.name.toLowerCase().includes(q)) {
10
- return true;
11
- }
12
- if ((_a = field.description) == null ? void 0 : _a.toLowerCase().includes(q)) {
13
- return true;
14
- }
15
- return getNamedType(field.type).name.toLowerCase().includes(q);
8
+ const q = query.trim().toLowerCase();
9
+ if (q === "") return true;
10
+ if (field.name.toLowerCase().includes(q)) return true;
11
+ if (field.description?.toLowerCase().includes(q)) return true;
12
+ return getNamedType(field.type).name.toLowerCase().includes(q);
16
13
  }
17
- function selectVisibleFields({
18
- fields,
19
- isSelected,
20
- threshold,
21
- expanded,
22
- filter
23
- }) {
24
- if (filter.trim() !== "") {
25
- return {
26
- visible: fields.filter((f) => fieldMatchesFilter(f, filter)),
27
- hiddenCount: 0
28
- };
29
- }
30
- if (expanded || fields.length <= threshold) {
31
- return {
32
- visible: fields,
33
- hiddenCount: 0
34
- };
35
- }
36
- const visible = fields.filter((field, index) => index < threshold || isSelected(field.name));
37
- return {
38
- visible,
39
- hiddenCount: fields.length - visible.length
40
- };
14
+ /**
15
+ * Decide which fields of a list to render. A filter (when non-empty) bypasses the
16
+ * cap and shows all matches. Otherwise the first `threshold` fields show in schema
17
+ * order, plus any selected field beyond the cap (selected fields never hide).
18
+ */
19
+ function selectVisibleFields({ fields, isSelected, threshold, expanded, filter }) {
20
+ if (filter.trim() !== "") return {
21
+ visible: fields.filter((f) => fieldMatchesFilter(f, filter)),
22
+ hiddenCount: 0
23
+ };
24
+ if (expanded || fields.length <= threshold) return {
25
+ visible: fields,
26
+ hiddenCount: 0
27
+ };
28
+ const visible = fields.filter((field, index) => index < threshold || isSelected(field.name));
29
+ return {
30
+ visible,
31
+ hiddenCount: fields.length - visible.length
32
+ };
41
33
  }
42
- export {
43
- FIELD_LIST_THRESHOLD,
44
- fieldMatchesFilter,
45
- selectVisibleFields
46
- };
47
- //# sourceMappingURL=field-list-view.js.map
34
+ //#endregion
35
+ export { fieldMatchesFilter, selectVisibleFields };
36
+
37
+ //# sourceMappingURL=field-list-view.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"field-list-view.js","sources":["../../src/lib/field-list-view.ts"],"sourcesContent":["import { getNamedType, type GraphQLField } from 'graphql';\n\n/** Field lists longer than this are capped by default and gain a filter input. */\nexport const FIELD_LIST_THRESHOLD = 20;\n\n/**\n * Case-insensitive substring match over a field's name, description, and named\n * type. An empty/whitespace query matches everything (treated as \"no filter\").\n */\nexport function fieldMatchesFilter(\n field: GraphQLField<unknown, unknown>,\n query: string,\n): boolean {\n const q = query.trim().toLowerCase();\n if (q === '') {\n return true;\n }\n if (field.name.toLowerCase().includes(q)) {\n return true;\n }\n if (field.description?.toLowerCase().includes(q)) {\n return true;\n }\n return getNamedType(field.type).name.toLowerCase().includes(q);\n}\n\ninterface SelectVisibleFieldsInput {\n fields: GraphQLField<unknown, unknown>[];\n /** True when the field (by name) is already selected in the query at this path. */\n isSelected: (fieldName: string) => boolean;\n threshold: number;\n /** True when the user clicked \"N more\" to reveal the full list. */\n expanded: boolean;\n /** Active filter text; empty string means no filter. */\n filter: string;\n}\n\ninterface VisibleFields {\n visible: GraphQLField<unknown, unknown>[];\n /** Count of fields hidden only by the cap (0 while filtering or expanded). */\n hiddenCount: number;\n}\n\n/**\n * Decide which fields of a list to render. A filter (when non-empty) bypasses the\n * cap and shows all matches. Otherwise the first `threshold` fields show in schema\n * order, plus any selected field beyond the cap (selected fields never hide).\n */\nexport function selectVisibleFields({\n fields,\n isSelected,\n threshold,\n expanded,\n filter,\n}: SelectVisibleFieldsInput): VisibleFields {\n if (filter.trim() !== '') {\n return {\n visible: fields.filter(f => fieldMatchesFilter(f, filter)),\n hiddenCount: 0,\n };\n }\n\n if (expanded || fields.length <= threshold) {\n return { visible: fields, hiddenCount: 0 };\n }\n\n const visible = fields.filter(\n (field, index) => index < threshold || isSelected(field.name),\n );\n return { visible, hiddenCount: fields.length - visible.length };\n}\n"],"names":["FIELD_LIST_THRESHOLD","fieldMatchesFilter","field","query","q","trim","toLowerCase","name","includes","description","getNamedType","type","selectVisibleFields","fields","isSelected","threshold","expanded","filter","visible","f","hiddenCount","length","index"],"mappings":";AAGO,MAAMA,uBAAuB;AAMpBC,SAAAA,mBACdC,OACAC,OACS;;AACT,QAAMC,IAAID,MAAME,KAAK,EAAEC,YAAY;AACnC,MAAIF,MAAM,IAAI;AACL,WAAA;AAAA,EAAA;AAET,MAAIF,MAAMK,KAAKD,YAAcE,EAAAA,SAASJ,CAAC,GAAG;AACjC,WAAA;AAAA,EAAA;AAET,OAAIF,WAAMO,gBAANP,mBAAmBI,cAAcE,SAASJ,IAAI;AACzC,WAAA;AAAA,EAAA;AAEFM,SAAAA,aAAaR,MAAMS,IAAI,EAAEJ,KAAKD,YAAY,EAAEE,SAASJ,CAAC;AAC/D;AAwBO,SAASQ,oBAAoB;AAAA,EAClCC;AAAAA,EACAC;AAAAA,EACAC;AAAAA,EACAC;AAAAA,EACAC;AACwB,GAAkB;AACtCA,MAAAA,OAAOZ,KAAK,MAAM,IAAI;AACjB,WAAA;AAAA,MACLa,SAASL,OAAOI,OAAOE,OAAKlB,mBAAmBkB,GAAGF,MAAM,CAAC;AAAA,MACzDG,aAAa;AAAA,IACf;AAAA,EAAA;AAGEJ,MAAAA,YAAYH,OAAOQ,UAAUN,WAAW;AACnC,WAAA;AAAA,MAAEG,SAASL;AAAAA,MAAQO,aAAa;AAAA,IAAE;AAAA,EAAA;AAGrCF,QAAAA,UAAUL,OAAOI,OACrB,CAACf,OAAOoB,UAAUA,QAAQP,aAAaD,WAAWZ,MAAMK,IAAI,CAC9D;AACO,SAAA;AAAA,IAAEW;AAAAA,IAASE,aAAaP,OAAOQ,SAASH,QAAQG;AAAAA,EAAO;AAChE;"}
1
+ {"version":3,"file":"field-list-view.js","names":["getNamedType","GraphQLField","FIELD_LIST_THRESHOLD","fieldMatchesFilter","field","query","q","trim","toLowerCase","name","includes","description","type","SelectVisibleFieldsInput","fields","isSelected","fieldName","threshold","expanded","filter","VisibleFields","visible","hiddenCount","selectVisibleFields","f","length","index"],"sources":["../../src/lib/field-list-view.ts"],"sourcesContent":["import { getNamedType, type GraphQLField } from 'graphql';\n\n/** Field lists longer than this are capped by default and gain a filter input. */\nexport const FIELD_LIST_THRESHOLD = 20;\n\n/**\n * Case-insensitive substring match over a field's name, description, and named\n * type. An empty/whitespace query matches everything (treated as \"no filter\").\n */\nexport function fieldMatchesFilter(\n field: GraphQLField<unknown, unknown>,\n query: string,\n): boolean {\n const q = query.trim().toLowerCase();\n if (q === '') {\n return true;\n }\n if (field.name.toLowerCase().includes(q)) {\n return true;\n }\n if (field.description?.toLowerCase().includes(q)) {\n return true;\n }\n return getNamedType(field.type).name.toLowerCase().includes(q);\n}\n\ninterface SelectVisibleFieldsInput {\n fields: GraphQLField<unknown, unknown>[];\n /** True when the field (by name) is already selected in the query at this path. */\n isSelected: (fieldName: string) => boolean;\n threshold: number;\n /** True when the user clicked \"N more\" to reveal the full list. */\n expanded: boolean;\n /** Active filter text; empty string means no filter. */\n filter: string;\n}\n\ninterface VisibleFields {\n visible: GraphQLField<unknown, unknown>[];\n /** Count of fields hidden only by the cap (0 while filtering or expanded). */\n hiddenCount: number;\n}\n\n/**\n * Decide which fields of a list to render. A filter (when non-empty) bypasses the\n * cap and shows all matches. Otherwise the first `threshold` fields show in schema\n * order, plus any selected field beyond the cap (selected fields never hide).\n */\nexport function selectVisibleFields({\n fields,\n isSelected,\n threshold,\n expanded,\n filter,\n}: SelectVisibleFieldsInput): VisibleFields {\n if (filter.trim() !== '') {\n return {\n visible: fields.filter(f => fieldMatchesFilter(f, filter)),\n hiddenCount: 0,\n };\n }\n\n if (expanded || fields.length <= threshold) {\n return { visible: fields, hiddenCount: 0 };\n }\n\n const visible = fields.filter(\n (field, index) => index < threshold || isSelected(field.name),\n );\n return { visible, hiddenCount: fields.length - visible.length };\n}\n"],"mappings":";;;;;;AASA,SAAgBG,mBACdC,OACAC,OACS;CACT,MAAMC,IAAID,MAAME,KAAK,CAAC,CAACC,YAAY;CACnC,IAAIF,MAAM,IACR,OAAO;CAET,IAAIF,MAAMK,KAAKD,YAAY,CAAC,CAACE,SAASJ,CAAC,GACrC,OAAO;CAET,IAAIF,MAAMO,aAAaH,YAAY,CAAC,CAACE,SAASJ,CAAC,GAC7C,OAAO;CAET,OAAON,aAAaI,MAAMQ,IAAI,CAAC,CAACH,KAAKD,YAAY,CAAC,CAACE,SAASJ,CAAC;AAC/D;;;;;;AAwBA,SAAgBiB,oBAAoB,EAClCT,QACAC,YACAE,WACAC,UACAC,UAC0C;CAC1C,IAAIA,OAAOZ,KAAK,MAAM,IACpB,OAAO;EACLc,SAASP,OAAOK,QAAOK,MAAKrB,mBAAmBqB,GAAGL,MAAM,CAAC;EACzDG,aAAa;CACf;CAGF,IAAIJ,YAAYJ,OAAOW,UAAUR,WAC/B,OAAO;EAAEI,SAASP;EAAQQ,aAAa;CAAE;CAG3C,MAAMD,UAAUP,OAAOK,QACpBf,OAAOsB,UAAUA,QAAQT,aAAaF,WAAWX,MAAMK,IAAI,CAC9D;CACA,OAAO;EAAEY;EAASC,aAAaR,OAAOW,SAASJ,QAAQI;CAAO;AAChE"}
@@ -1,76 +1,72 @@
1
+ import { addField, findDefinition, findNodeAtPath, mapDefinition, removeField } from "./ast-path.js";
1
2
  import { Kind, visit } from "graphql";
2
- import { mapDefinition, findDefinition, findNodeAtPath, removeField, addField } from "./ast-path.js";
3
+ //#region src/lib/field-selection.ts
4
+ /** Collects every variable name referenced anywhere within `selectionSet`. */
3
5
  function collectVariableReferences(selectionSet, into) {
4
- visit(selectionSet, {
5
- Variable(node) {
6
- into.add(node.name.value);
7
- }
8
- });
6
+ visit(selectionSet, { Variable(node) {
7
+ into.add(node.name.value);
8
+ } });
9
9
  }
10
+ /**
11
+ * Drops variable definitions that are no longer referenced by the operation,
12
+ * so removing the last field that used a promoted variable doesn't leave a
13
+ * dangling (invalid) `$var` in the operation header. Named fragments in `doc`
14
+ * are scanned too, so a variable used only through a fragment spread is kept.
15
+ */
10
16
  function pruneUnusedVariableDefinitions(operation, doc) {
11
- const varDefs = operation.variableDefinitions;
12
- if (!varDefs) {
13
- return operation;
14
- }
15
- const used = /* @__PURE__ */ new Set();
16
- collectVariableReferences(operation.selectionSet, used);
17
- for (const def of doc.definitions) {
18
- if (def.kind === Kind.FRAGMENT_DEFINITION) {
19
- collectVariableReferences(def.selectionSet, used);
20
- }
21
- }
22
- const kept = varDefs.filter((vd) => used.has(vd.variable.name.value));
23
- if (kept.length === varDefs.length) {
24
- return operation;
25
- }
26
- return {
27
- ...operation,
28
- variableDefinitions: kept
29
- };
17
+ const varDefs = operation.variableDefinitions;
18
+ if (!varDefs) return operation;
19
+ const used = /* @__PURE__ */ new Set();
20
+ collectVariableReferences(operation.selectionSet, used);
21
+ for (const def of doc.definitions) if (def.kind === Kind.FRAGMENT_DEFINITION) collectVariableReferences(def.selectionSet, used);
22
+ const kept = varDefs.filter((vd) => used.has(vd.variable.name.value));
23
+ if (kept.length === varDefs.length) return operation;
24
+ return {
25
+ ...operation,
26
+ variableDefinitions: kept
27
+ };
30
28
  }
29
+ /**
30
+ * Returns whether the field at the given path is present in the definition
31
+ * addressed by `target` (an operation or a named fragment) of `doc`.
32
+ */
31
33
  function isFieldSelected(doc, path, target) {
32
- if (path.length === 0) {
33
- return false;
34
- }
35
- const definition = findDefinition(doc, target);
36
- if (!definition) {
37
- return false;
38
- }
39
- return findNodeAtPath(definition.selectionSet, path) !== void 0;
34
+ if (path.length === 0) return false;
35
+ const definition = findDefinition(doc, target);
36
+ if (!definition) return false;
37
+ return findNodeAtPath(definition.selectionSet, path) !== void 0;
40
38
  }
39
+ /**
40
+ * Toggles the field at `path` in the definition addressed by `target` (an
41
+ * operation or a named fragment) of `doc`:
42
+ * - If the field is already selected, removes it (along with its children).
43
+ * - If the field is not selected, adds it (creating intermediate selection
44
+ * sets as needed).
45
+ *
46
+ * Returns a new `DocumentNode`; does not mutate `doc`.
47
+ */
41
48
  function toggleFieldSelection(doc, path, target) {
42
- if (path.length === 0) {
43
- return doc;
44
- }
45
- const selected = isFieldSelected(doc, path, target);
46
- return mapDefinition(doc, target, (definition) => {
47
- const newSelectionSet = selected ? removeField(definition.selectionSet, path) : addField(definition.selectionSet, path);
48
- if (newSelectionSet.selections.length === 0) {
49
- if (definition.kind === Kind.OPERATION_DEFINITION) {
50
- return null;
51
- }
52
- return definition;
53
- }
54
- if (definition.kind === Kind.FRAGMENT_DEFINITION) {
55
- return {
56
- ...definition,
57
- selectionSet: newSelectionSet
58
- };
59
- }
60
- let newOperation = {
61
- ...definition,
62
- selectionSet: newSelectionSet
63
- };
64
- if (selected) {
65
- newOperation = pruneUnusedVariableDefinitions(newOperation, doc);
66
- }
67
- return newOperation;
68
- });
49
+ if (path.length === 0) return doc;
50
+ const selected = isFieldSelected(doc, path, target);
51
+ return mapDefinition(doc, target, (definition) => {
52
+ const newSelectionSet = selected ? removeField(definition.selectionSet, path) : addField(definition.selectionSet, path);
53
+ if (newSelectionSet.selections.length === 0) {
54
+ if (definition.kind === Kind.OPERATION_DEFINITION) return null;
55
+ return definition;
56
+ }
57
+ if (definition.kind === Kind.FRAGMENT_DEFINITION) return {
58
+ ...definition,
59
+ selectionSet: newSelectionSet
60
+ };
61
+ let newOperation = {
62
+ ...definition,
63
+ selectionSet: newSelectionSet
64
+ };
65
+ if (selected) newOperation = pruneUnusedVariableDefinitions(newOperation, doc);
66
+ return newOperation;
67
+ });
69
68
  }
70
- export {
71
- collectVariableReferences,
72
- isFieldSelected,
73
- pruneUnusedVariableDefinitions,
74
- toggleFieldSelection
75
- };
76
- //# sourceMappingURL=field-selection.js.map
69
+ //#endregion
70
+ export { collectVariableReferences, isFieldSelected, pruneUnusedVariableDefinitions, toggleFieldSelection };
71
+
72
+ //# sourceMappingURL=field-selection.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"field-selection.js","sources":["../../src/lib/field-selection.ts"],"sourcesContent":["import {\n Kind,\n visit,\n type DocumentNode,\n type OperationDefinitionNode,\n type SelectionSetNode,\n} from 'graphql';\n\nimport {\n addField,\n findDefinition,\n mapDefinition,\n findNodeAtPath,\n removeField,\n type DefinitionTarget,\n type PathSegment,\n} from './ast-path';\n\n/** Collects every variable name referenced anywhere within `selectionSet`. */\nexport function collectVariableReferences(\n selectionSet: SelectionSetNode,\n into: Set<string>,\n): void {\n visit(selectionSet, {\n Variable(node) {\n into.add(node.name.value);\n },\n });\n}\n\n/**\n * Drops variable definitions that are no longer referenced by the operation,\n * so removing the last field that used a promoted variable doesn't leave a\n * dangling (invalid) `$var` in the operation header. Named fragments in `doc`\n * are scanned too, so a variable used only through a fragment spread is kept.\n */\nexport function pruneUnusedVariableDefinitions(\n operation: OperationDefinitionNode,\n doc: DocumentNode,\n): OperationDefinitionNode {\n const varDefs = operation.variableDefinitions;\n if (!varDefs) {\n return operation;\n }\n\n const used = new Set<string>();\n collectVariableReferences(operation.selectionSet, used);\n for (const def of doc.definitions) {\n if (def.kind === Kind.FRAGMENT_DEFINITION) {\n collectVariableReferences(def.selectionSet, used);\n }\n }\n\n const kept = varDefs.filter(vd => used.has(vd.variable.name.value));\n if (kept.length === varDefs.length) {\n return operation;\n }\n return { ...operation, variableDefinitions: kept };\n}\n\n/**\n * Returns whether the field at the given path is present in the definition\n * addressed by `target` (an operation or a named fragment) of `doc`.\n */\nexport function isFieldSelected(\n doc: DocumentNode,\n path: PathSegment[],\n target: DefinitionTarget,\n): boolean {\n if (path.length === 0) {\n return false;\n }\n\n const definition = findDefinition(doc, target);\n if (!definition) {\n return false;\n }\n\n return findNodeAtPath(definition.selectionSet, path) !== undefined;\n}\n\n/**\n * Toggles the field at `path` in the definition addressed by `target` (an\n * operation or a named fragment) of `doc`:\n * - If the field is already selected, removes it (along with its children).\n * - If the field is not selected, adds it (creating intermediate selection\n * sets as needed).\n *\n * Returns a new `DocumentNode`; does not mutate `doc`.\n */\nexport function toggleFieldSelection(\n doc: DocumentNode,\n path: PathSegment[],\n target: DefinitionTarget,\n): DocumentNode {\n if (path.length === 0) {\n return doc;\n }\n\n const selected = isFieldSelected(doc, path, target);\n return mapDefinition(doc, target, definition => {\n const newSelectionSet = selected\n ? removeField(definition.selectionSet, path)\n : addField(definition.selectionSet, path);\n\n if (newSelectionSet.selections.length === 0) {\n if (definition.kind === Kind.OPERATION_DEFINITION) {\n // Removing the last field empties the operation. An operation with no\n // selection set is unprintable (it would fail to parse and leave the\n // builder out of sync), so drop the whole operation definition instead.\n return null;\n }\n // A fragment can't be emptied without becoming invalid and orphaning its\n // spreads, so removing its last field is a no-op. Deleting a fragment\n // outright is a separate action.\n return definition;\n }\n\n if (definition.kind === Kind.FRAGMENT_DEFINITION) {\n return { ...definition, selectionSet: newSelectionSet };\n }\n\n let newOperation: OperationDefinitionNode = {\n ...definition,\n selectionSet: newSelectionSet,\n };\n // Removing a field can orphan a promoted variable; drop any now-unused\n // variable definitions so the operation stays valid.\n if (selected) {\n newOperation = pruneUnusedVariableDefinitions(newOperation, doc);\n }\n return newOperation;\n });\n}\n"],"names":["collectVariableReferences","selectionSet","into","visit","Variable","node","add","name","value","pruneUnusedVariableDefinitions","operation","doc","varDefs","variableDefinitions","used","Set","def","definitions","kind","Kind","FRAGMENT_DEFINITION","kept","filter","vd","has","variable","length","isFieldSelected","path","target","definition","findDefinition","findNodeAtPath","undefined","toggleFieldSelection","selected","mapDefinition","newSelectionSet","removeField","addField","selections","OPERATION_DEFINITION","newOperation"],"mappings":";;AAmBgBA,SAAAA,0BACdC,cACAC,MACM;AACNC,QAAMF,cAAc;AAAA,IAClBG,SAASC,MAAM;AACRC,WAAAA,IAAID,KAAKE,KAAKC,KAAK;AAAA,IAAA;AAAA,EAC1B,CACD;AACH;AAQgBC,SAAAA,+BACdC,WACAC,KACyB;AACzB,QAAMC,UAAUF,UAAUG;AAC1B,MAAI,CAACD,SAAS;AACLF,WAAAA;AAAAA,EAAAA;AAGHI,QAAAA,2BAAWC,IAAY;AACHL,4BAAAA,UAAUT,cAAca,IAAI;AAC3CE,aAAAA,OAAOL,IAAIM,aAAa;AAC7BD,QAAAA,IAAIE,SAASC,KAAKC,qBAAqB;AACfJ,gCAAAA,IAAIf,cAAca,IAAI;AAAA,IAAA;AAAA,EAClD;AAGIO,QAAAA,OAAOT,QAAQU,OAAOC,CAAMT,OAAAA,KAAKU,IAAID,GAAGE,SAASlB,KAAKC,KAAK,CAAC;AAC9Da,MAAAA,KAAKK,WAAWd,QAAQc,QAAQ;AAC3BhB,WAAAA;AAAAA,EAAAA;AAEF,SAAA;AAAA,IAAE,GAAGA;AAAAA,IAAWG,qBAAqBQ;AAAAA,EAAK;AACnD;AAMgBM,SAAAA,gBACdhB,KACAiB,MACAC,QACS;AACLD,MAAAA,KAAKF,WAAW,GAAG;AACd,WAAA;AAAA,EAAA;AAGHI,QAAAA,aAAaC,eAAepB,KAAKkB,MAAM;AAC7C,MAAI,CAACC,YAAY;AACR,WAAA;AAAA,EAAA;AAGT,SAAOE,eAAeF,WAAW7B,cAAc2B,IAAI,MAAMK;AAC3D;AAWgBC,SAAAA,qBACdvB,KACAiB,MACAC,QACc;AACVD,MAAAA,KAAKF,WAAW,GAAG;AACdf,WAAAA;AAAAA,EAAAA;AAGT,QAAMwB,WAAWR,gBAAgBhB,KAAKiB,MAAMC,MAAM;AAC3CO,SAAAA,cAAczB,KAAKkB,QAAQC,CAAc,eAAA;AACxCO,UAAAA,kBAAkBF,WACpBG,YAAYR,WAAW7B,cAAc2B,IAAI,IACzCW,SAAST,WAAW7B,cAAc2B,IAAI;AAEtCS,QAAAA,gBAAgBG,WAAWd,WAAW,GAAG;AACvCI,UAAAA,WAAWZ,SAASC,KAAKsB,sBAAsB;AAI1C,eAAA;AAAA,MAAA;AAKFX,aAAAA;AAAAA,IAAAA;AAGLA,QAAAA,WAAWZ,SAASC,KAAKC,qBAAqB;AACzC,aAAA;AAAA,QAAE,GAAGU;AAAAA,QAAY7B,cAAcoC;AAAAA,MAAgB;AAAA,IAAA;AAGxD,QAAIK,eAAwC;AAAA,MAC1C,GAAGZ;AAAAA,MACH7B,cAAcoC;AAAAA,IAChB;AAGA,QAAIF,UAAU;AACG1B,qBAAAA,+BAA+BiC,cAAc/B,GAAG;AAAA,IAAA;AAE1D+B,WAAAA;AAAAA,EAAAA,CACR;AACH;"}
1
+ {"version":3,"file":"field-selection.js","names":["Kind","visit","DocumentNode","OperationDefinitionNode","SelectionSetNode","addField","findDefinition","mapDefinition","findNodeAtPath","removeField","DefinitionTarget","PathSegment","collectVariableReferences","selectionSet","into","Set","Variable","node","add","name","value","pruneUnusedVariableDefinitions","operation","doc","varDefs","variableDefinitions","used","def","definitions","kind","FRAGMENT_DEFINITION","kept","filter","vd","has","variable","length","isFieldSelected","path","target","definition","undefined","toggleFieldSelection","selected","newSelectionSet","selections","OPERATION_DEFINITION","newOperation"],"sources":["../../src/lib/field-selection.ts"],"sourcesContent":["import {\n Kind,\n visit,\n type DocumentNode,\n type OperationDefinitionNode,\n type SelectionSetNode,\n} from 'graphql';\n\nimport {\n addField,\n findDefinition,\n mapDefinition,\n findNodeAtPath,\n removeField,\n type DefinitionTarget,\n type PathSegment,\n} from './ast-path';\n\n/** Collects every variable name referenced anywhere within `selectionSet`. */\nexport function collectVariableReferences(\n selectionSet: SelectionSetNode,\n into: Set<string>,\n): void {\n visit(selectionSet, {\n Variable(node) {\n into.add(node.name.value);\n },\n });\n}\n\n/**\n * Drops variable definitions that are no longer referenced by the operation,\n * so removing the last field that used a promoted variable doesn't leave a\n * dangling (invalid) `$var` in the operation header. Named fragments in `doc`\n * are scanned too, so a variable used only through a fragment spread is kept.\n */\nexport function pruneUnusedVariableDefinitions(\n operation: OperationDefinitionNode,\n doc: DocumentNode,\n): OperationDefinitionNode {\n const varDefs = operation.variableDefinitions;\n if (!varDefs) {\n return operation;\n }\n\n const used = new Set<string>();\n collectVariableReferences(operation.selectionSet, used);\n for (const def of doc.definitions) {\n if (def.kind === Kind.FRAGMENT_DEFINITION) {\n collectVariableReferences(def.selectionSet, used);\n }\n }\n\n const kept = varDefs.filter(vd => used.has(vd.variable.name.value));\n if (kept.length === varDefs.length) {\n return operation;\n }\n return { ...operation, variableDefinitions: kept };\n}\n\n/**\n * Returns whether the field at the given path is present in the definition\n * addressed by `target` (an operation or a named fragment) of `doc`.\n */\nexport function isFieldSelected(\n doc: DocumentNode,\n path: PathSegment[],\n target: DefinitionTarget,\n): boolean {\n if (path.length === 0) {\n return false;\n }\n\n const definition = findDefinition(doc, target);\n if (!definition) {\n return false;\n }\n\n return findNodeAtPath(definition.selectionSet, path) !== undefined;\n}\n\n/**\n * Toggles the field at `path` in the definition addressed by `target` (an\n * operation or a named fragment) of `doc`:\n * - If the field is already selected, removes it (along with its children).\n * - If the field is not selected, adds it (creating intermediate selection\n * sets as needed).\n *\n * Returns a new `DocumentNode`; does not mutate `doc`.\n */\nexport function toggleFieldSelection(\n doc: DocumentNode,\n path: PathSegment[],\n target: DefinitionTarget,\n): DocumentNode {\n if (path.length === 0) {\n return doc;\n }\n\n const selected = isFieldSelected(doc, path, target);\n return mapDefinition(doc, target, definition => {\n const newSelectionSet = selected\n ? removeField(definition.selectionSet, path)\n : addField(definition.selectionSet, path);\n\n if (newSelectionSet.selections.length === 0) {\n if (definition.kind === Kind.OPERATION_DEFINITION) {\n // Removing the last field empties the operation. An operation with no\n // selection set is unprintable (it would fail to parse and leave the\n // builder out of sync), so drop the whole operation definition instead.\n return null;\n }\n // A fragment can't be emptied without becoming invalid and orphaning its\n // spreads, so removing its last field is a no-op. Deleting a fragment\n // outright is a separate action.\n return definition;\n }\n\n if (definition.kind === Kind.FRAGMENT_DEFINITION) {\n return { ...definition, selectionSet: newSelectionSet };\n }\n\n let newOperation: OperationDefinitionNode = {\n ...definition,\n selectionSet: newSelectionSet,\n };\n // Removing a field can orphan a promoted variable; drop any now-unused\n // variable definitions so the operation stays valid.\n if (selected) {\n newOperation = pruneUnusedVariableDefinitions(newOperation, doc);\n }\n return newOperation;\n });\n}\n"],"mappings":";;;;AAmBA,SAAgBY,0BACdC,cACAC,MACM;CACNb,MAAMY,cAAc,EAClBG,SAASC,MAAM;EACbH,KAAKI,IAAID,KAAKE,KAAKC,KAAK;CAC1B,EACF,CAAC;AACH;;;;;;;AAQA,SAAgBC,+BACdC,WACAC,KACyB;CACzB,MAAMC,UAAUF,UAAUG;CAC1B,IAAI,CAACD,SACH,OAAOF;CAGT,MAAMI,uBAAO,IAAIX,IAAY;CAC7BH,0BAA0BU,UAAUT,cAAca,IAAI;CACtD,KAAK,MAAMC,OAAOJ,IAAIK,aACpB,IAAID,IAAIE,SAAS7B,KAAK8B,qBACpBlB,0BAA0Be,IAAId,cAAca,IAAI;CAIpD,MAAMK,OAAOP,QAAQQ,QAAOC,OAA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