@sortlist/cli 0.40.0 → 0.41.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -873,7 +873,7 @@ function ucfirst(s) {
873
873
  return s.charAt(0).toUpperCase() + s.slice(1);
874
874
  }
875
875
  var PAYLOAD_TYPE_PATTERN = /(?:Response|Payload|MutationPayload)$/;
876
- function buildSelections(type, schema, depth, exclude, prefix, visited) {
876
+ function buildSelections(type, schema, depth, exclude, prefix, totalCount, visited) {
877
877
  if (!isObjectType(type)) return [];
878
878
  if (visited.has(type.name)) return [];
879
879
  visited.add(type.name);
@@ -882,8 +882,16 @@ function buildSelections(type, schema, depth, exclude, prefix, visited) {
882
882
  const isPayload = PAYLOAD_TYPE_PATTERN.test(type.name);
883
883
  if (isConnection(type)) {
884
884
  const nodesType = unwrap(fields.nodes.type);
885
- const nodeSelections = buildSelections(nodesType, schema, depth, exclude, `${prefix}nodes.`, new Set(visited));
886
- return connectionSelections(type, nodeSelections);
885
+ const nodeSelections = buildSelections(
886
+ nodesType,
887
+ schema,
888
+ depth,
889
+ exclude,
890
+ `${prefix}nodes.`,
891
+ totalCount,
892
+ new Set(visited)
893
+ );
894
+ return connectionSelections(type, nodeSelections, totalCount);
887
895
  }
888
896
  for (const [name, field] of Object.entries(fields)) {
889
897
  if (name.startsWith("__")) continue;
@@ -896,7 +904,15 @@ function buildSelections(type, schema, depth, exclude, prefix, visited) {
896
904
  if (isLeaf(namedType)) {
897
905
  lines.push(name);
898
906
  } else if (isObjectType(namedType) && depth > 0) {
899
- const subSelections = buildSelections(namedType, schema, depth - 1, exclude, `${fullPath}.`, new Set(visited));
907
+ const subSelections = buildSelections(
908
+ namedType,
909
+ schema,
910
+ depth - 1,
911
+ exclude,
912
+ `${fullPath}.`,
913
+ totalCount,
914
+ new Set(visited)
915
+ );
900
916
  if (subSelections.length > 0) {
901
917
  lines.push(`${fieldCall(name, field)} {`);
902
918
  for (const sel of subSelections) lines.push(` ${sel}`);
@@ -939,7 +955,7 @@ function edgeExtras(connection) {
939
955
  }
940
956
  return lines.length === 0 ? null : { edge, lines };
941
957
  }
942
- function connectionSelections(connection, nodeSelections) {
958
+ function connectionSelections(connection, nodeSelections, totalCount) {
943
959
  if (nodeSelections.length === 0) return [];
944
960
  const lines = [];
945
961
  const extras = edgeExtras(connection);
@@ -959,7 +975,7 @@ function connectionSelections(connection, nodeSelections) {
959
975
  lines.push(" hasNextPage");
960
976
  lines.push(" endCursor");
961
977
  lines.push("}");
962
- if (connection.getFields().totalCount) lines.push("totalCount");
978
+ if (totalCount && connection.getFields().totalCount) lines.push("totalCount");
963
979
  for (const extra of connectionExtras(connection)) lines.push(extra);
964
980
  return lines;
965
981
  }
@@ -1002,6 +1018,7 @@ function buildQuery(schema, kind, fieldName, variables, options = {}) {
1002
1018
  if (!field) throw new Error(`Unknown ${kind} field: ${fieldName}`);
1003
1019
  const depth = options.depth ?? 0;
1004
1020
  const exclude = new Set(options.exclude ?? []);
1021
+ const wantsTotalCount = options.totalCount === true;
1005
1022
  const returnType = unwrap(field.type);
1006
1023
  const allArgs = field.args;
1007
1024
  const varDefsInner = allArgs.map((a) => `$${a.name}: ${a.type.toString()}`).join(", ");
@@ -1014,9 +1031,9 @@ function buildQuery(schema, kind, fieldName, variables, options = {}) {
1014
1031
  } else {
1015
1032
  let selections;
1016
1033
  if (options.fields && options.fields.length > 0) {
1017
- selections = buildFieldsFromList(returnType, schema, options.fields);
1034
+ selections = buildFieldsFromList(returnType, schema, options.fields, wantsTotalCount);
1018
1035
  } else {
1019
- selections = buildSelections(returnType, schema, depth, exclude, "", /* @__PURE__ */ new Set());
1036
+ selections = buildSelections(returnType, schema, depth, exclude, "", wantsTotalCount, /* @__PURE__ */ new Set());
1020
1037
  }
1021
1038
  if (selections.length === 0) {
1022
1039
  selectionBody = ` ${fieldName}${fieldArgs} {
@@ -1036,14 +1053,19 @@ ${selectionBody}
1036
1053
  }`;
1037
1054
  return { query, variables, fieldName };
1038
1055
  }
1039
- function buildFieldsFromList(type, schema, fieldPaths) {
1056
+ function buildFieldsFromList(type, schema, fieldPaths, totalCount) {
1040
1057
  if (!isObjectType(type)) return [];
1041
1058
  const fields = type.getFields();
1042
1059
  if (isConnection(type)) {
1043
1060
  const nodesType = unwrap(fields.nodes.type);
1044
1061
  const withId = isObjectType(nodesType) && nodesType.getFields().id && !fieldPaths.includes("id");
1045
- const nodeSelections = buildFieldsFromList(nodesType, schema, withId ? ["id", ...fieldPaths] : fieldPaths);
1046
- return connectionSelections(type, nodeSelections);
1062
+ const nodeSelections = buildFieldsFromList(
1063
+ nodesType,
1064
+ schema,
1065
+ withId ? ["id", ...fieldPaths] : fieldPaths,
1066
+ totalCount
1067
+ );
1068
+ return connectionSelections(type, nodeSelections, totalCount);
1047
1069
  }
1048
1070
  const lines = [];
1049
1071
  const grouped = /* @__PURE__ */ new Map();
@@ -1059,7 +1081,7 @@ function buildFieldsFromList(type, schema, fieldPaths) {
1059
1081
  if (isLeaf(namedType)) {
1060
1082
  lines.push(name);
1061
1083
  } else if (isObjectType(namedType) && subPaths.length > 0) {
1062
- const subSelections = buildFieldsFromList(namedType, schema, subPaths);
1084
+ const subSelections = buildFieldsFromList(namedType, schema, subPaths, totalCount);
1063
1085
  if (subSelections.length > 0) {
1064
1086
  lines.push(`${fieldCall(name, field)} {`);
1065
1087
  for (const sel of subSelections) lines.push(` ${sel}`);
@@ -1402,7 +1424,7 @@ function buildCommand(schema, fieldName, kind, description, service) {
1402
1424
  cmd.option(flag, describeArg(a));
1403
1425
  }
1404
1426
  }
1405
- cmd.option("--depth <depth>", "Selection depth (default: 0)", parseInt).option("--fields <fields>", "Comma-separated fields to include").option("--exclude <fields>", "Comma-separated fields to exclude");
1427
+ cmd.option("--depth <depth>", "Selection depth (default: 0)", parseInt).option("--fields <fields>", "Comma-separated fields to include").option("--exclude <fields>", "Comma-separated fields to exclude").option("--total-count", "Also count every match, not just this page (slow on a broad filter)");
1406
1428
  cmd.action(async function() {
1407
1429
  const opts = this.optsWithGlobals();
1408
1430
  const ctx = getUserContext();
@@ -1437,7 +1459,8 @@ function buildCommand(schema, fieldName, kind, description, service) {
1437
1459
  built = buildQuery(schema, kind, fieldName, variables, {
1438
1460
  depth: opts.depth,
1439
1461
  fields: opts.fields?.split(","),
1440
- exclude: opts.exclude?.split(",")
1462
+ exclude: opts.exclude?.split(","),
1463
+ totalCount: opts.totalCount === true
1441
1464
  });
1442
1465
  } catch (buildError) {
1443
1466
  if (!(buildError instanceof UnknownFieldError)) throw buildError;