@tinacms/graphql 1.5.9 → 1.5.11

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.mjs CHANGED
@@ -68,6 +68,15 @@ var SysFieldDefinition = {
68
68
  selectionSet: {
69
69
  kind: "SelectionSet",
70
70
  selections: [
71
+ // {
72
+ // kind: 'Field' as const,
73
+ // name: {
74
+ // kind: 'Name' as const,
75
+ // value: 'title',
76
+ // },
77
+ // arguments: [],
78
+ // directives: [],
79
+ // },
71
80
  {
72
81
  kind: "Field",
73
82
  name: {
@@ -135,6 +144,10 @@ var SysFieldDefinition = {
135
144
  }
136
145
  };
137
146
  var astBuilder = {
147
+ /**
148
+ * `FormFieldBuilder` acts as a shortcut to building an entire `ObjectTypeDefinition`, we use this
149
+ * because all Tina field objects share a common set of fields ('name', 'label', 'component')
150
+ */
138
151
  FormFieldBuilder: ({
139
152
  name,
140
153
  additionalFields
@@ -358,6 +371,8 @@ var astBuilder = {
358
371
  kind: "Name",
359
372
  value: name
360
373
  },
374
+ // @ts-ignore FIXME; this is being handled properly but we're lying to
375
+ // ts and then fixing it in the `extractInlineTypes` function
361
376
  fields
362
377
  }),
363
378
  UnionTypeDefinition: ({
@@ -370,6 +385,8 @@ var astBuilder = {
370
385
  value: name
371
386
  },
372
387
  directives: [],
388
+ // @ts-ignore FIXME; this is being handled properly but we're lying to
389
+ // ts and then fixing it in the `extractInlineTypes` function
373
390
  types: types.map((name2) => ({
374
391
  kind: "NamedType",
375
392
  name: {
@@ -466,8 +483,11 @@ var astBuilder = {
466
483
  string: "String",
467
484
  boolean: "Boolean",
468
485
  number: "Float",
486
+ // FIXME - needs to be float or int
469
487
  datetime: "String",
488
+ // FIXME
470
489
  image: "String",
490
+ // FIXME
471
491
  text: "String"
472
492
  };
473
493
  return scalars[type];
@@ -966,8 +986,7 @@ var astBuilder = {
966
986
  }
967
987
  };
968
988
  var capitalize = (s) => {
969
- if (typeof s !== "string")
970
- return "";
989
+ if (typeof s !== "string") return "";
971
990
  return s.charAt(0).toUpperCase() + s.slice(1);
972
991
  };
973
992
  var extractInlineTypes = (item) => {
@@ -1010,41 +1029,6 @@ function* walk(maybeNode, visited = /* @__PURE__ */ new WeakSet()) {
1010
1029
  yield maybeNode;
1011
1030
  visited.add(maybeNode);
1012
1031
  }
1013
- function addNamespaceToSchema(maybeNode, namespace = []) {
1014
- if (typeof maybeNode === "string") {
1015
- return maybeNode;
1016
- }
1017
- if (typeof maybeNode === "boolean") {
1018
- return maybeNode;
1019
- }
1020
- const newNode = maybeNode;
1021
- const keys = Object.keys(maybeNode);
1022
- Object.values(maybeNode).map((m, index) => {
1023
- const key = keys[index];
1024
- if (Array.isArray(m)) {
1025
- newNode[key] = m.map((element) => {
1026
- if (!element) {
1027
- return;
1028
- }
1029
- if (!element.hasOwnProperty("name")) {
1030
- return element;
1031
- }
1032
- const value = element.name || element.value;
1033
- return addNamespaceToSchema(element, [...namespace, value]);
1034
- });
1035
- } else {
1036
- if (!m) {
1037
- return;
1038
- }
1039
- if (!m.hasOwnProperty("name")) {
1040
- newNode[key] = m;
1041
- } else {
1042
- newNode[key] = addNamespaceToSchema(m, [...namespace, m.name]);
1043
- }
1044
- }
1045
- });
1046
- return { ...newNode, namespace };
1047
- }
1048
1032
  var generateNamespacedFieldName = (names, suffix = "") => {
1049
1033
  return (suffix ? [...names, suffix] : names).map(capitalize).join("");
1050
1034
  };
@@ -1422,6 +1406,19 @@ var Builder = class {
1422
1406
  this.addToLookupMap = (lookup) => {
1423
1407
  this.lookupMap[lookup.type] = lookup;
1424
1408
  };
1409
+ /**
1410
+ * ```graphql
1411
+ * # ex.
1412
+ * {
1413
+ * getCollection(collection: $collection) {
1414
+ * name
1415
+ * documents {...}
1416
+ * }
1417
+ * }
1418
+ * ```
1419
+ *
1420
+ * @param collections
1421
+ */
1425
1422
  this.buildCollectionDefinition = async (collections) => {
1426
1423
  const name = "collection";
1427
1424
  const typeName = "Collection";
@@ -1492,6 +1489,19 @@ var Builder = class {
1492
1489
  required: true
1493
1490
  });
1494
1491
  };
1492
+ /**
1493
+ * ```graphql
1494
+ * # ex.
1495
+ * {
1496
+ * getCollections {
1497
+ * name
1498
+ * documents {...}
1499
+ * }
1500
+ * }
1501
+ * ```
1502
+ *
1503
+ * @param collections
1504
+ */
1495
1505
  this.buildMultiCollectionDefinition = async (collections) => {
1496
1506
  const name = "collections";
1497
1507
  const typeName = "Collection";
@@ -1502,6 +1512,17 @@ var Builder = class {
1502
1512
  required: true
1503
1513
  });
1504
1514
  };
1515
+ /**
1516
+ * ```graphql
1517
+ * # ex.
1518
+ * {
1519
+ * node(id: $id) {
1520
+ * id
1521
+ * data {...}
1522
+ * }
1523
+ * }
1524
+ * ```
1525
+ */
1505
1526
  this.multiNodeDocument = async () => {
1506
1527
  const name = "node";
1507
1528
  const args = [
@@ -1522,6 +1543,19 @@ var Builder = class {
1522
1543
  required: true
1523
1544
  });
1524
1545
  };
1546
+ /**
1547
+ * ```graphql
1548
+ * # ex.
1549
+ * {
1550
+ * getDocument(collection: $collection, relativePath: $relativePath) {
1551
+ * id
1552
+ * data {...}
1553
+ * }
1554
+ * }
1555
+ * ```
1556
+ *
1557
+ * @param collections
1558
+ */
1525
1559
  this.multiCollectionDocument = async (collections) => {
1526
1560
  const name = "document";
1527
1561
  const args = [
@@ -1547,6 +1581,19 @@ var Builder = class {
1547
1581
  required: true
1548
1582
  });
1549
1583
  };
1584
+ /**
1585
+ * ```graphql
1586
+ * # ex.
1587
+ * {
1588
+ * addPendingDocument(collection: $collection, relativePath: $relativePath, params: $params) {
1589
+ * id
1590
+ * data {...}
1591
+ * }
1592
+ * }
1593
+ * ```
1594
+ *
1595
+ * @param collections
1596
+ */
1550
1597
  this.addMultiCollectionDocumentMutation = async () => {
1551
1598
  return astBuilder.FieldDefinition({
1552
1599
  name: "addPendingDocument",
@@ -1571,6 +1618,19 @@ var Builder = class {
1571
1618
  type: astBuilder.TYPES.MultiCollectionDocument
1572
1619
  });
1573
1620
  };
1621
+ /**
1622
+ * ```graphql
1623
+ * # ex.
1624
+ * {
1625
+ * createDocument(relativePath: $relativePath, params: $params) {
1626
+ * id
1627
+ * data {...}
1628
+ * }
1629
+ * }
1630
+ * ```
1631
+ *
1632
+ * @param collections
1633
+ */
1574
1634
  this.buildCreateCollectionDocumentMutation = async (collections) => {
1575
1635
  return astBuilder.FieldDefinition({
1576
1636
  name: "createDocument",
@@ -1598,6 +1658,19 @@ var Builder = class {
1598
1658
  type: astBuilder.TYPES.MultiCollectionDocument
1599
1659
  });
1600
1660
  };
1661
+ /**
1662
+ * ```graphql
1663
+ * # ex.
1664
+ * {
1665
+ * updateDocument(relativePath: $relativePath, params: $params) {
1666
+ * id
1667
+ * data {...}
1668
+ * }
1669
+ * }
1670
+ * ```
1671
+ *
1672
+ * @param collections
1673
+ */
1601
1674
  this.buildUpdateCollectionDocumentMutation = async (collections) => {
1602
1675
  return astBuilder.FieldDefinition({
1603
1676
  name: "updateDocument",
@@ -1625,6 +1698,19 @@ var Builder = class {
1625
1698
  type: astBuilder.TYPES.MultiCollectionDocument
1626
1699
  });
1627
1700
  };
1701
+ /**
1702
+ * ```graphql
1703
+ * # ex.
1704
+ * {
1705
+ * deleteDocument(relativePath: $relativePath, params: $params) {
1706
+ * id
1707
+ * data {...}
1708
+ * }
1709
+ * }
1710
+ * ```
1711
+ *
1712
+ * @param collections
1713
+ */
1628
1714
  this.buildDeleteCollectionDocumentMutation = async (collections) => {
1629
1715
  return astBuilder.FieldDefinition({
1630
1716
  name: "deleteDocument",
@@ -1644,6 +1730,19 @@ var Builder = class {
1644
1730
  type: astBuilder.TYPES.MultiCollectionDocument
1645
1731
  });
1646
1732
  };
1733
+ /**
1734
+ * ```graphql
1735
+ * # ex.
1736
+ * {
1737
+ * createFolder(folderName: $folderName, params: $params) {
1738
+ * id
1739
+ * data {...}
1740
+ * }
1741
+ * }
1742
+ * ```
1743
+ *
1744
+ * @param collections
1745
+ */
1647
1746
  this.buildCreateCollectionFolderMutation = async () => {
1648
1747
  return astBuilder.FieldDefinition({
1649
1748
  name: "createFolder",
@@ -1663,6 +1762,19 @@ var Builder = class {
1663
1762
  type: astBuilder.TYPES.MultiCollectionDocument
1664
1763
  });
1665
1764
  };
1765
+ /**
1766
+ * ```graphql
1767
+ * # ex.
1768
+ * {
1769
+ * getPostDocument(relativePath: $relativePath) {
1770
+ * id
1771
+ * data {...}
1772
+ * }
1773
+ * }
1774
+ * ```
1775
+ *
1776
+ * @param collection
1777
+ */
1666
1778
  this.collectionDocument = async (collection) => {
1667
1779
  const name = NAMER.queryName([collection.name]);
1668
1780
  const type = await this._buildCollectionDocumentType(collection);
@@ -1723,6 +1835,20 @@ var Builder = class {
1723
1835
  const args = [];
1724
1836
  return astBuilder.FieldDefinition({ type, name, args, required: false });
1725
1837
  };
1838
+ /**
1839
+ * Turns a collection into a fragment that gets updated on build. This fragment does not resolve references
1840
+ * ```graphql
1841
+ * # ex.
1842
+ * fragment AuthorsParts on Authors {
1843
+ * name
1844
+ * avatar
1845
+ * ...
1846
+ * }
1847
+ * ```
1848
+ *
1849
+ * @public
1850
+ * @param collection a Tina Cloud collection
1851
+ */
1726
1852
  this.collectionFragment = async (collection) => {
1727
1853
  const name = NAMER.dataTypeName(collection.namespace);
1728
1854
  const fragmentName = NAMER.fragmentName(collection.namespace);
@@ -1736,6 +1862,20 @@ var Builder = class {
1736
1862
  selections: filterSelections(selections)
1737
1863
  });
1738
1864
  };
1865
+ /**
1866
+ * Given a collection this function returns its selections set. For example for Post this would return
1867
+ *
1868
+ * "
1869
+ * body
1870
+ * title
1871
+ * ... on Author {
1872
+ * name
1873
+ * heroImg
1874
+ * }
1875
+ *
1876
+ * But in the AST format
1877
+ *
1878
+ * */
1739
1879
  this._getCollectionFragmentSelections = async (collection, depth) => {
1740
1880
  const selections = [];
1741
1881
  selections.push({
@@ -1817,9 +1957,9 @@ var Builder = class {
1817
1957
  ]
1818
1958
  });
1819
1959
  }
1960
+ // TODO: Should we throw here?
1820
1961
  case "reference":
1821
- if (depth >= this.maxDepth)
1822
- return false;
1962
+ if (depth >= this.maxDepth) return false;
1823
1963
  if (!("collections" in field)) {
1824
1964
  return false;
1825
1965
  }
@@ -1851,6 +1991,7 @@ var Builder = class {
1851
1991
  name: field.name,
1852
1992
  selections: [
1853
1993
  ...selections,
1994
+ // This is ... on Document { id }
1854
1995
  {
1855
1996
  kind: "InlineFragment",
1856
1997
  typeCondition: {
@@ -1881,6 +2022,19 @@ var Builder = class {
1881
2022
  });
1882
2023
  }
1883
2024
  };
2025
+ /**
2026
+ * ```graphql
2027
+ * # ex.
2028
+ * mutation {
2029
+ * updatePostDocument(relativePath: $relativePath, params: $params) {
2030
+ * id
2031
+ * data {...}
2032
+ * }
2033
+ * }
2034
+ * ```
2035
+ *
2036
+ * @param collection
2037
+ */
1884
2038
  this.updateCollectionDocumentMutation = async (collection) => {
1885
2039
  return astBuilder.FieldDefinition({
1886
2040
  type: await this._buildCollectionDocumentType(collection),
@@ -1900,6 +2054,19 @@ var Builder = class {
1900
2054
  ]
1901
2055
  });
1902
2056
  };
2057
+ /**
2058
+ * ```graphql
2059
+ * # ex.
2060
+ * mutation {
2061
+ * createPostDocument(relativePath: $relativePath, params: $params) {
2062
+ * id
2063
+ * data {...}
2064
+ * }
2065
+ * }
2066
+ * ```
2067
+ *
2068
+ * @param collection
2069
+ */
1903
2070
  this.createCollectionDocumentMutation = async (collection) => {
1904
2071
  return astBuilder.FieldDefinition({
1905
2072
  type: await this._buildCollectionDocumentType(collection),
@@ -1919,6 +2086,22 @@ var Builder = class {
1919
2086
  ]
1920
2087
  });
1921
2088
  };
2089
+ /**
2090
+ * ```graphql
2091
+ * # ex.
2092
+ * {
2093
+ * getPostList(first: 10) {
2094
+ * edges {
2095
+ * node {
2096
+ * id
2097
+ * }
2098
+ * }
2099
+ * }
2100
+ * }
2101
+ * ```
2102
+ *
2103
+ * @param collection
2104
+ */
1922
2105
  this.collectionDocumentList = async (collection) => {
1923
2106
  const connectionName = NAMER.referenceConnectionType(collection.namespace);
1924
2107
  this.addToLookupMap({
@@ -1934,6 +2117,10 @@ var Builder = class {
1934
2117
  collection
1935
2118
  });
1936
2119
  };
2120
+ /**
2121
+ * GraphQL type definitions which remain unchanged regardless
2122
+ * of the supplied Tina schema. Ex. "node" interface
2123
+ */
1937
2124
  this.buildStaticDefinitions = () => staticDefinitions;
1938
2125
  this._buildCollectionDocumentType = async (collection, suffix = "", extraFields = [], extraInterfaces = []) => {
1939
2126
  const documentTypeName = NAMER.documentTypeName(collection.namespace);
@@ -2438,6 +2625,7 @@ var Builder = class {
2438
2625
  name: NAMER.dataFilterTypeName(namespace),
2439
2626
  fields: await sequential(collections, async (collection2) => {
2440
2627
  return astBuilder.InputValueDefinition({
2628
+ // @ts-ignore
2441
2629
  name: collection2.name,
2442
2630
  type: NAMER.dataFilterTypeName(collection2.namespace)
2443
2631
  });
@@ -2626,7 +2814,8 @@ Visit https://tina.io/docs/errors/ui-not-supported/ for more information
2626
2814
  ]
2627
2815
  });
2628
2816
  };
2629
- this.maxDepth = config?.tinaSchema.schema?.config?.client?.referenceDepth ?? 2;
2817
+ this.maxDepth = // @ts-ignore
2818
+ config?.tinaSchema.schema?.config?.client?.referenceDepth ?? 2;
2630
2819
  this.tinaSchema = config.tinaSchema;
2631
2820
  this.lookupMap = {};
2632
2821
  }
@@ -2637,8 +2826,7 @@ Visit https://tina.io/docs/errors/ui-not-supported/ for more information
2637
2826
  selections.push(field);
2638
2827
  });
2639
2828
  const filteredSelections = filterSelections(selections);
2640
- if (!filteredSelections.length)
2641
- return false;
2829
+ if (!filteredSelections.length) return false;
2642
2830
  return astBuilder.InlineFragmentDefinition({
2643
2831
  selections: filteredSelections,
2644
2832
  name: NAMER.dataTypeName(template.namespace)
@@ -2675,6 +2863,7 @@ var filterSelections = (arr) => {
2675
2863
  import { TinaSchema } from "@tinacms/schema-tools";
2676
2864
 
2677
2865
  // src/schema/validate.ts
2866
+ import { addNamespaceToSchema } from "@tinacms/schema-tools";
2678
2867
  import deepClone from "lodash.clonedeep";
2679
2868
  import * as yup2 from "yup";
2680
2869
  import {
@@ -2721,6 +2910,7 @@ var validationCollectionsPathAndMatch = (collections) => {
2721
2910
  }).map((x) => `${x.path}${x.format || "md"}`);
2722
2911
  if (noMatchCollections.length !== new Set(noMatchCollections).size) {
2723
2912
  throw new Error(
2913
+ // TODO: add a link to the docs
2724
2914
  "Two collections without match can not have the same `path`. Please make the `path` unique or add a matches property to the collection."
2725
2915
  );
2726
2916
  }
@@ -2829,7 +3019,7 @@ var validateField = async (field) => {
2829
3019
  // package.json
2830
3020
  var package_default = {
2831
3021
  name: "@tinacms/graphql",
2832
- version: "1.5.9",
3022
+ version: "1.5.11",
2833
3023
  main: "dist/index.js",
2834
3024
  module: "dist/index.mjs",
2835
3025
  typings: "dist/index.d.ts",
@@ -2856,8 +3046,8 @@ var package_default = {
2856
3046
  build: "tinacms-scripts build",
2857
3047
  docs: "pnpm typedoc",
2858
3048
  serve: "pnpm nodemon dist/server.js",
2859
- test: "jest",
2860
- "test-watch": "jest --watch"
3049
+ test: "vitest run",
3050
+ "test-watch": "vitest"
2861
3051
  },
2862
3052
  dependencies: {
2863
3053
  "@iarna/toml": "^2.2.5",
@@ -2898,7 +3088,6 @@ var package_default = {
2898
3088
  "@types/estree": "^0.0.50",
2899
3089
  "@types/express": "^4.17.21",
2900
3090
  "@types/fs-extra": "^9.0.13",
2901
- "@types/jest": "^26.0.24",
2902
3091
  "@types/js-yaml": "^3.12.10",
2903
3092
  "@types/lodash.camelcase": "^4.3.9",
2904
3093
  "@types/lodash.upperfirst": "^4.3.9",
@@ -2909,13 +3098,13 @@ var package_default = {
2909
3098
  "@types/normalize-path": "^3.0.2",
2910
3099
  "@types/ws": "^7.4.7",
2911
3100
  "@types/yup": "^0.29.14",
2912
- jest: "^29.7.0",
2913
- "jest-diff": "^29.7.0",
2914
3101
  "jest-file-snapshot": "^0.5.0",
2915
- "jest-matcher-utils": "^29.7.0",
2916
3102
  "memory-level": "^1.0.0",
2917
3103
  nodemon: "3.1.4",
2918
- typescript: "^5.6.3"
3104
+ typescript: "^5.6.3",
3105
+ vite: "^4.3.9",
3106
+ vitest: "^0.32.2",
3107
+ zod: "^3.23.8"
2919
3108
  }
2920
3109
  };
2921
3110
 
@@ -2986,6 +3175,7 @@ var _buildFragments = async (builder, tinaSchema) => {
2986
3175
  const fragDoc = {
2987
3176
  kind: "Document",
2988
3177
  definitions: uniqBy2(
3178
+ // @ts-ignore
2989
3179
  extractInlineTypes(fragmentDefinitionsFields),
2990
3180
  (node) => node.name.value
2991
3181
  )
@@ -3008,6 +3198,7 @@ var _buildQueries = async (builder, tinaSchema) => {
3008
3198
  fragName,
3009
3199
  queryName: queryListName,
3010
3200
  filterType: queryFilterTypeName,
3201
+ // look for flag to see if the data layer is enabled
3011
3202
  dataLayer: Boolean(
3012
3203
  tinaSchema.config?.meta?.flags?.find((x) => x === "experimentalData")
3013
3204
  )
@@ -3017,6 +3208,7 @@ var _buildQueries = async (builder, tinaSchema) => {
3017
3208
  const queryDoc = {
3018
3209
  kind: "Document",
3019
3210
  definitions: uniqBy2(
3211
+ // @ts-ignore
3020
3212
  extractInlineTypes(operationsDefinitions),
3021
3213
  (node) => node.name.value
3022
3214
  )
@@ -3105,6 +3297,7 @@ var _buildSchema = async (builder, tinaSchema) => {
3105
3297
  return {
3106
3298
  kind: "Document",
3107
3299
  definitions: uniqBy2(
3300
+ // @ts-ignore
3108
3301
  extractInlineTypes(definitions),
3109
3302
  (node) => node.name.value
3110
3303
  )
@@ -3309,8 +3502,7 @@ var resolveMediaCloudToRelative = (value, config = { useRelativeMedia: true }, s
3309
3502
  }
3310
3503
  if (Array.isArray(value)) {
3311
3504
  return value.map((v) => {
3312
- if (!v || typeof v !== "string")
3313
- return v;
3505
+ if (!v || typeof v !== "string") return v;
3314
3506
  const cleanMediaRoot = cleanUpSlashes(
3315
3507
  schema.config.media.tina.mediaRoot
3316
3508
  );
@@ -3338,8 +3530,7 @@ var resolveMediaRelativeToCloud = (value, config = { useRelativeMedia: true }, s
3338
3530
  }
3339
3531
  if (Array.isArray(value)) {
3340
3532
  return value.map((v) => {
3341
- if (!v || typeof v !== "string")
3342
- return v;
3533
+ if (!v || typeof v !== "string") return v;
3343
3534
  const strippedValue = v.replace(cleanMediaRoot, "");
3344
3535
  return `https://${config.assetsHost}/${config.clientId}${strippedValue}`;
3345
3536
  });
@@ -3357,8 +3548,7 @@ var cleanUpSlashes = (path7) => {
3357
3548
  return "";
3358
3549
  };
3359
3550
  var hasTinaMediaConfig = (schema) => {
3360
- if (!schema.config?.media?.tina)
3361
- return false;
3551
+ if (!schema.config?.media?.tina) return false;
3362
3552
  if (typeof schema.config?.media?.tina?.publicFolder !== "string" && typeof schema.config?.media?.tina?.mediaRoot !== "string")
3363
3553
  return false;
3364
3554
  return true;
@@ -3402,6 +3592,7 @@ var LevelProxyHandler = {
3402
3592
  } else if (property === "sublevel") {
3403
3593
  return (...args) => {
3404
3594
  return new Proxy(
3595
+ // eslint-disable-next-line prefer-spread
3405
3596
  target[property].apply(target, args),
3406
3597
  LevelProxyHandler
3407
3598
  );
@@ -4278,6 +4469,7 @@ var makeFolderOpsForCollection = (folderTree, collection, indexDefinitions, opTy
4278
4469
  result.push({
4279
4470
  type: opType,
4280
4471
  key: `${collection.path}/${subFolderKey}.${collection.format}`,
4472
+ // replace the root with the collection path
4281
4473
  sublevel: indexSublevel,
4282
4474
  value: {}
4283
4475
  });
@@ -4392,6 +4584,7 @@ var resolveFieldData = async ({ namespace, ...field }, rawData, accumulator, tin
4392
4584
  case "password":
4393
4585
  accumulator[field.name] = {
4394
4586
  value: void 0,
4587
+ // never resolve the password hash
4395
4588
  passwordChangeRequired: value["passwordChangeRequired"] ?? false
4396
4589
  };
4397
4590
  break;
@@ -4586,6 +4779,7 @@ var Resolver = class {
4586
4779
  const collection = this.tinaSchema.getCollection(collectionName);
4587
4780
  const extraFields = {};
4588
4781
  return {
4782
+ // return the collection and hasDocuments to resolve documents at a lower level
4589
4783
  documents: { collection, hasDocuments },
4590
4784
  ...collection,
4591
4785
  ...extraFields
@@ -4672,7 +4866,9 @@ var Resolver = class {
4672
4866
  );
4673
4867
  } else {
4674
4868
  return this.buildFieldMutations(
4869
+ // @ts-ignore FIXME Argument of type 'string | object' is not assignable to parameter of type '{ [fieldName: string]: string | object | (string | object)[]; }'
4675
4870
  fieldValue,
4871
+ //@ts-ignore
4676
4872
  objectTemplate,
4677
4873
  existingData
4678
4874
  );
@@ -4684,6 +4880,7 @@ var Resolver = class {
4684
4880
  fieldValue.map(async (item) => {
4685
4881
  if (typeof item === "string") {
4686
4882
  throw new Error(
4883
+ //@ts-ignore
4687
4884
  `Expected object for template value for field ${field.name}`
4688
4885
  );
4689
4886
  }
@@ -4692,16 +4889,19 @@ var Resolver = class {
4692
4889
  });
4693
4890
  const [templateName] = Object.entries(item)[0];
4694
4891
  const template = templates.find(
4892
+ //@ts-ignore
4695
4893
  (template2) => template2.name === templateName
4696
4894
  );
4697
4895
  if (!template) {
4698
4896
  throw new Error(`Expected to find template ${templateName}`);
4699
4897
  }
4700
4898
  return {
4899
+ // @ts-ignore FIXME Argument of type 'unknown' is not assignable to parameter of type '{ [fieldName: string]: string | { [key: string]: unknown; } | (string | { [key: string]: unknown; })[]; }'
4701
4900
  ...await this.buildFieldMutations(
4702
4901
  item[template.name],
4703
4902
  template
4704
4903
  ),
4904
+ //@ts-ignore
4705
4905
  _template: template.name
4706
4906
  };
4707
4907
  })
@@ -4709,6 +4909,7 @@ var Resolver = class {
4709
4909
  } else {
4710
4910
  if (typeof fieldValue === "string") {
4711
4911
  throw new Error(
4912
+ //@ts-ignore
4712
4913
  `Expected object for template value for field ${field.name}`
4713
4914
  );
4714
4915
  }
@@ -4717,16 +4918,19 @@ var Resolver = class {
4717
4918
  });
4718
4919
  const [templateName] = Object.entries(fieldValue)[0];
4719
4920
  const template = templates.find(
4921
+ //@ts-ignore
4720
4922
  (template2) => template2.name === templateName
4721
4923
  );
4722
4924
  if (!template) {
4723
4925
  throw new Error(`Expected to find template ${templateName}`);
4724
4926
  }
4725
4927
  return {
4928
+ // @ts-ignore FIXME Argument of type 'unknown' is not assignable to parameter of type '{ [fieldName: string]: string | { [key: string]: unknown; } | (string | { [key: string]: unknown; })[]; }'
4726
4929
  ...await this.buildFieldMutations(
4727
4930
  fieldValue[template.name],
4728
4931
  template
4729
4932
  ),
4933
+ //@ts-ignore
4730
4934
  _template: template.name
4731
4935
  };
4732
4936
  }
@@ -4766,6 +4970,7 @@ var Resolver = class {
4766
4970
  return this.getDocument(realPath);
4767
4971
  }
4768
4972
  const params = await this.buildObjectMutations(
4973
+ // @ts-ignore
4769
4974
  args.params[collection.name],
4770
4975
  collection
4771
4976
  );
@@ -4811,6 +5016,7 @@ var Resolver = class {
4811
5016
  const values = {
4812
5017
  ...oldDoc,
4813
5018
  ...await this.buildFieldMutations(
5019
+ // @ts-ignore FIXME: failing on unknown, which we don't need to know because it's recursive
4814
5020
  templateParams,
4815
5021
  template,
4816
5022
  doc?._rawData
@@ -4824,6 +5030,7 @@ var Resolver = class {
4824
5030
  return this.getDocument(realPath);
4825
5031
  }
4826
5032
  const params = await this.buildObjectMutations(
5033
+ //@ts-ignore
4827
5034
  isCollectionSpecific ? args.params : args.params[collection.name],
4828
5035
  collection,
4829
5036
  doc?._rawData
@@ -4831,6 +5038,10 @@ var Resolver = class {
4831
5038
  await this.database.put(realPath, { ...oldDoc, ...params }, collection.name);
4832
5039
  return this.getDocument(realPath);
4833
5040
  };
5041
+ /**
5042
+ * Returns top-level fields which are not defined in the collection, so their
5043
+ * values are not eliminated from Tina when new values are saved
5044
+ */
4834
5045
  this.resolveLegacyValues = (oldDoc, collection) => {
4835
5046
  const legacyValues = {};
4836
5047
  Object.entries(oldDoc).forEach(([key, value]) => {
@@ -5036,6 +5247,7 @@ var Resolver = class {
5036
5247
  },
5037
5248
  collection: referencedCollection,
5038
5249
  hydrator: (path7) => path7
5250
+ // just return the path
5039
5251
  }
5040
5252
  );
5041
5253
  const { edges } = resolvedCollectionConnection;
@@ -5103,6 +5315,12 @@ var Resolver = class {
5103
5315
  }
5104
5316
  };
5105
5317
  };
5318
+ /**
5319
+ * Checks if a document has references to it
5320
+ * @param id The id of the document to check for references
5321
+ * @param c The collection to check for references
5322
+ * @returns true if the document has references, false otherwise
5323
+ */
5106
5324
  this.hasReferences = async (id, c) => {
5107
5325
  let count = 0;
5108
5326
  const deepRefs = this.tinaSchema.findReferences(c.name);
@@ -5137,6 +5355,12 @@ var Resolver = class {
5137
5355
  }
5138
5356
  return false;
5139
5357
  };
5358
+ /**
5359
+ * Finds references to a document
5360
+ * @param id the id of the document to find references to
5361
+ * @param c the collection to find references in
5362
+ * @returns references to the document in the form of a map of collection names to a list of fields that reference the document
5363
+ */
5140
5364
  this.findReferences = async (id, c) => {
5141
5365
  const references = {};
5142
5366
  const deepRefs = this.tinaSchema.findReferences(c.name);
@@ -5263,6 +5487,27 @@ var Resolver = class {
5263
5487
  }
5264
5488
  return accum;
5265
5489
  };
5490
+ /**
5491
+ * A mutation looks nearly identical between updateDocument:
5492
+ * ```graphql
5493
+ * updateDocument(collection: $collection,relativePath: $path, params: {
5494
+ * post: {
5495
+ * title: "Hello, World"
5496
+ * }
5497
+ * })`
5498
+ * ```
5499
+ * and `updatePostDocument`:
5500
+ * ```graphql
5501
+ * updatePostDocument(relativePath: $path, params: {
5502
+ * title: "Hello, World"
5503
+ * })
5504
+ * ```
5505
+ * The problem here is that we don't know whether the payload came from `updateDocument`
5506
+ * or `updatePostDocument` (we could, but for now it's easier not to pipe those details through),
5507
+ * But we do know that when given a `args.collection` value, we can assume that
5508
+ * this was a `updateDocument` request, and thus - should grab the data
5509
+ * from the corresponding field name in the key
5510
+ */
5266
5511
  this.buildParams = (args) => {
5267
5512
  try {
5268
5513
  assertShape(
@@ -5362,7 +5607,10 @@ var resolve = async ({
5362
5607
  const graphQLSchema = buildASTSchema(graphQLSchemaAst);
5363
5608
  const tinaConfig = await database.getTinaSchema();
5364
5609
  const tinaSchema = await createSchema({
5610
+ // TODO: please update all the types to import from @tinacms/schema-tools
5611
+ // @ts-ignore
5365
5612
  schema: tinaConfig,
5613
+ // @ts-ignore
5366
5614
  flags: tinaConfig?.meta?.flags
5367
5615
  });
5368
5616
  const resolver = createResolver({
@@ -5379,8 +5627,7 @@ var resolve = async ({
5379
5627
  database
5380
5628
  },
5381
5629
  typeResolver: async (source, _args, info) => {
5382
- if (source.__typename)
5383
- return source.__typename;
5630
+ if (source.__typename) return source.__typename;
5384
5631
  const namedType = getNamedType(info.returnType).toString();
5385
5632
  const lookup = await database.getLookup(namedType);
5386
5633
  if (lookup.resolveType === "unionData") {
@@ -5529,11 +5776,13 @@ var resolve = async ({
5529
5776
  set(
5530
5777
  params,
5531
5778
  userField.path.slice(1),
5779
+ // remove _rawData from users path
5532
5780
  users.map((u) => {
5533
5781
  if (user[idFieldName] === u[idFieldName]) {
5534
5782
  return user;
5535
5783
  }
5536
5784
  return {
5785
+ // don't overwrite other users' passwords
5537
5786
  ...u,
5538
5787
  [passwordFieldName]: {
5539
5788
  ...u[passwordFieldName],
@@ -5556,6 +5805,9 @@ var resolve = async ({
5556
5805
  }
5557
5806
  const isCreation = lookup[info.fieldName] === "create";
5558
5807
  switch (lookup.resolveType) {
5808
+ /**
5809
+ * `node(id: $id)`
5810
+ */
5559
5811
  case "nodeDocument":
5560
5812
  assertShape(
5561
5813
  args,
@@ -5587,6 +5839,7 @@ var resolve = async ({
5587
5839
  collection: args.collection,
5588
5840
  isMutation,
5589
5841
  isCreation,
5842
+ // Right now this is the only case for deletion
5590
5843
  isDeletion: info.fieldName === "deleteDocument",
5591
5844
  isFolderCreation: info.fieldName === "createFolder",
5592
5845
  isUpdateName: Boolean(args?.params?.relativePath),
@@ -5596,6 +5849,9 @@ var resolve = async ({
5596
5849
  return result;
5597
5850
  }
5598
5851
  return value;
5852
+ /**
5853
+ * eg `getMovieDocument.data.actors`
5854
+ */
5599
5855
  case "multiCollectionDocumentList":
5600
5856
  if (Array.isArray(value)) {
5601
5857
  return {
@@ -5607,7 +5863,15 @@ var resolve = async ({
5607
5863
  }
5608
5864
  if (info.fieldName === "documents" && value?.collection && value?.hasDocuments) {
5609
5865
  let filter = args.filter;
5610
- if (typeof args?.filter !== "undefined" && args?.filter !== null && typeof value?.collection?.name === "string" && Object.keys(args.filter).includes(value?.collection?.name) && typeof args.filter[value?.collection?.name] !== "undefined") {
5866
+ if (
5867
+ // 1. Make sure that the filter exists
5868
+ typeof args?.filter !== "undefined" && args?.filter !== null && // 2. Make sure that the collection name exists
5869
+ // @ts-ignore
5870
+ typeof value?.collection?.name === "string" && // 3. Make sure that the collection name is in the filter and is not undefined
5871
+ // @ts-ignore
5872
+ Object.keys(args.filter).includes(value?.collection?.name) && // @ts-ignore
5873
+ typeof args.filter[value?.collection?.name] !== "undefined"
5874
+ ) {
5611
5875
  filter = args.filter[value.collection.name];
5612
5876
  }
5613
5877
  return resolver.resolveCollectionConnection({
@@ -5615,12 +5879,20 @@ var resolve = async ({
5615
5879
  ...args,
5616
5880
  filter
5617
5881
  },
5882
+ // @ts-ignore
5618
5883
  collection: value.collection
5619
5884
  });
5620
5885
  }
5621
5886
  throw new Error(
5622
5887
  `Expected an array for result of ${info.fieldName} at ${info.path}`
5623
5888
  );
5889
+ /**
5890
+ * Collections-specific getter
5891
+ * eg. `getPostDocument`/`createPostDocument`/`updatePostDocument`
5892
+ *
5893
+ * if coming from a query result
5894
+ * the field will be `node`
5895
+ */
5624
5896
  case "collectionDocument": {
5625
5897
  if (value) {
5626
5898
  return value;
@@ -5635,11 +5907,32 @@ var resolve = async ({
5635
5907
  });
5636
5908
  return result;
5637
5909
  }
5910
+ /**
5911
+ * Collections-specific list getter
5912
+ * eg. `getPageList`
5913
+ */
5638
5914
  case "collectionDocumentList":
5639
5915
  return resolver.resolveCollectionConnection({
5640
5916
  args,
5641
5917
  collection: tinaSchema.getCollection(lookup.collection)
5642
5918
  });
5919
+ /**
5920
+ * A polymorphic data set, it can be from a document's data
5921
+ * of any nested object which can be one of many shapes
5922
+ *
5923
+ * ```graphql
5924
+ * getPostDocument(relativePath: $relativePath) {
5925
+ * data {...} <- this part
5926
+ * }
5927
+ * ```
5928
+ * ```graphql
5929
+ * getBlockDocument(relativePath: $relativePath) {
5930
+ * data {
5931
+ * blocks {...} <- or this part
5932
+ * }
5933
+ * }
5934
+ * ```
5935
+ */
5643
5936
  case "unionData":
5644
5937
  if (!value) {
5645
5938
  if (args.relativePath) {
@@ -5704,8 +5997,7 @@ var TinaLevelClient = class extends ManyLevelGuest {
5704
5997
  this.port = port || 9e3;
5705
5998
  }
5706
5999
  openConnection() {
5707
- if (this._connected)
5708
- return;
6000
+ if (this._connected) return;
5709
6001
  const socket = connect(this.port);
5710
6002
  pipeline(socket, this.createRpcStream(), socket, () => {
5711
6003
  this._connected = false;
@@ -5715,7 +6007,7 @@ var TinaLevelClient = class extends ManyLevelGuest {
5715
6007
  };
5716
6008
 
5717
6009
  // src/database/index.ts
5718
- import path4 from "path";
6010
+ import path4 from "node:path";
5719
6011
  import { GraphQLError as GraphQLError5 } from "graphql";
5720
6012
  import micromatch2 from "micromatch";
5721
6013
  import sha2 from "js-sha1";
@@ -5886,6 +6178,7 @@ var Database = class {
5886
6178
  "put",
5887
6179
  level
5888
6180
  ),
6181
+ // folder indices
5889
6182
  ...makeIndexOpsForDocument(
5890
6183
  normalizedPath,
5891
6184
  `${collection?.name}_${folderKey}`,
@@ -5908,6 +6201,7 @@ var Database = class {
5908
6201
  "del",
5909
6202
  level
5910
6203
  ),
6204
+ // folder indices
5911
6205
  ...makeIndexOpsForDocument(
5912
6206
  normalizedPath,
5913
6207
  `${collection?.name}_${folderKey}`,
@@ -6001,6 +6295,7 @@ var Database = class {
6001
6295
  "put",
6002
6296
  level
6003
6297
  ),
6298
+ // folder indices
6004
6299
  ...makeIndexOpsForDocument(
6005
6300
  normalizedPath,
6006
6301
  `${collection?.name}_${folderKey}`,
@@ -6023,6 +6318,7 @@ var Database = class {
6023
6318
  "del",
6024
6319
  level
6025
6320
  ),
6321
+ // folder indices
6026
6322
  ...makeIndexOpsForDocument(
6027
6323
  normalizedPath,
6028
6324
  `${collection?.name}_${folderKey}`,
@@ -6100,6 +6396,7 @@ var Database = class {
6100
6396
  aliasedData,
6101
6397
  extension,
6102
6398
  writeTemplateKey,
6399
+ //templateInfo.type === 'union',
6103
6400
  {
6104
6401
  frontmatterFormat: collection?.frontmatterFormat,
6105
6402
  frontmatterDelimiters: collection?.frontmatterDelimiters
@@ -6138,6 +6435,7 @@ var Database = class {
6138
6435
  SUBLEVEL_OPTIONS
6139
6436
  ).get(graphqlPath);
6140
6437
  };
6438
+ //TODO - is there a reason why the database fetches some config with "bridge.get", and some with "store.get"?
6141
6439
  this.getGraphQLSchemaFromBridge = async () => {
6142
6440
  if (!this.bridge) {
6143
6441
  throw new Error(`No bridge configured`);
@@ -6184,6 +6482,7 @@ var Database = class {
6184
6482
  for (const collection of collections) {
6185
6483
  const indexDefinitions = {
6186
6484
  [DEFAULT_COLLECTION_SORT_KEY]: { fields: [] }
6485
+ // provide a default sort key which is the file sort
6187
6486
  };
6188
6487
  if (collection.fields) {
6189
6488
  for (const field of collection.fields) {
@@ -6507,12 +6806,12 @@ var Database = class {
6507
6806
  if (collection?.isDetached) {
6508
6807
  level = this.appLevel.sublevel(collection?.name, SUBLEVEL_OPTIONS);
6509
6808
  }
6510
- const itemKey = normalizePath(filepath);
6809
+ const normalizedPath = normalizePath(filepath);
6511
6810
  const rootSublevel = level.sublevel(
6512
6811
  CONTENT_ROOT_PREFIX,
6513
6812
  SUBLEVEL_OPTIONS
6514
6813
  );
6515
- const item = await rootSublevel.get(itemKey);
6814
+ const item = await rootSublevel.get(normalizedPath);
6516
6815
  if (item) {
6517
6816
  const folderTreeBuilder = new FolderTreeBuilder();
6518
6817
  const folderKey = folderTreeBuilder.update(
@@ -6521,15 +6820,16 @@ var Database = class {
6521
6820
  );
6522
6821
  await this.contentLevel.batch([
6523
6822
  ...makeIndexOpsForDocument(
6524
- filepath,
6823
+ normalizedPath,
6525
6824
  collection.name,
6526
6825
  collectionIndexDefinitions,
6527
6826
  item,
6528
6827
  "del",
6529
6828
  level
6530
6829
  ),
6830
+ // folder indices
6531
6831
  ...makeIndexOpsForDocument(
6532
- filepath,
6832
+ normalizedPath,
6533
6833
  `${collection.name}_${folderKey}`,
6534
6834
  collectionIndexDefinitions,
6535
6835
  item,
@@ -6538,17 +6838,17 @@ var Database = class {
6538
6838
  ),
6539
6839
  {
6540
6840
  type: "del",
6541
- key: itemKey,
6841
+ key: normalizedPath,
6542
6842
  sublevel: rootSublevel
6543
6843
  }
6544
6844
  ]);
6545
6845
  }
6546
6846
  if (!collection?.isDetached) {
6547
6847
  if (this.bridge) {
6548
- await this.bridge.delete(normalizePath(filepath));
6848
+ await this.bridge.delete(normalizedPath);
6549
6849
  }
6550
6850
  try {
6551
- await this.onDelete(normalizePath(filepath));
6851
+ await this.onDelete(normalizedPath);
6552
6852
  } catch (e) {
6553
6853
  throw new GraphQLError5(
6554
6854
  `Error running onDelete hook for ${filepath}: ${e}`,
@@ -6682,6 +6982,9 @@ var Database = class {
6682
6982
  info: templateInfo
6683
6983
  };
6684
6984
  }
6985
+ /**
6986
+ * Clears the internal cache of the tinaSchema and the lookup file. This allows the state to be reset
6987
+ */
6685
6988
  clearCache() {
6686
6989
  this.tinaSchema = null;
6687
6990
  this._lookup = null;
@@ -6778,6 +7081,7 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
6778
7081
  "put",
6779
7082
  level
6780
7083
  ),
7084
+ // folder indexes
6781
7085
  ...makeIndexOpsForDocument(
6782
7086
  normalizedPath,
6783
7087
  `${collection?.name}_${folderKey}`,
@@ -6863,6 +7167,7 @@ var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection
6863
7167
  "del",
6864
7168
  database.contentLevel
6865
7169
  ),
7170
+ // folder indexes
6866
7171
  ...makeIndexOpsForDocument(
6867
7172
  itemKey,
6868
7173
  `${collection?.name}_${folderKey}`,
@@ -7078,17 +7383,26 @@ var IsomorphicBridge = class {
7078
7383
  getAuthor() {
7079
7384
  return {
7080
7385
  ...this.author,
7081
- timestamp: Math.round(new Date().getTime() / 1e3),
7386
+ timestamp: Math.round((/* @__PURE__ */ new Date()).getTime() / 1e3),
7082
7387
  timezoneOffset: 0
7083
7388
  };
7084
7389
  }
7085
7390
  getCommitter() {
7086
7391
  return {
7087
7392
  ...this.committer,
7088
- timestamp: Math.round(new Date().getTime() / 1e3),
7393
+ timestamp: Math.round((/* @__PURE__ */ new Date()).getTime() / 1e3),
7089
7394
  timezoneOffset: 0
7090
7395
  };
7091
7396
  }
7397
+ /**
7398
+ * Recursively populate paths matching `pattern` for the given `entry`
7399
+ *
7400
+ * @param pattern - pattern to filter paths by
7401
+ * @param entry - TreeEntry to start building list from
7402
+ * @param path - base path
7403
+ * @param results
7404
+ * @private
7405
+ */
7092
7406
  async listEntries({
7093
7407
  pattern,
7094
7408
  entry,
@@ -7121,6 +7435,15 @@ var IsomorphicBridge = class {
7121
7435
  });
7122
7436
  }
7123
7437
  }
7438
+ /**
7439
+ * For the specified path, returns an object with an array containing the parts of the path (pathParts)
7440
+ * and an array containing the WalkerEntry objects for the path parts (pathEntries). Any null elements in the
7441
+ * pathEntries are placeholders for non-existent entries.
7442
+ *
7443
+ * @param path - path being resolved
7444
+ * @param ref - ref to resolve path entries for
7445
+ * @private
7446
+ */
7124
7447
  async resolvePathEntries(path7, ref) {
7125
7448
  let pathParts = path7.split("/");
7126
7449
  const result = await git2.walk({
@@ -7151,6 +7474,17 @@ var IsomorphicBridge = class {
7151
7474
  }
7152
7475
  return { pathParts, pathEntries };
7153
7476
  }
7477
+ /**
7478
+ * Updates tree entry and associated parent tree entries
7479
+ *
7480
+ * @param existingOid - the existing OID
7481
+ * @param updatedOid - the updated OID
7482
+ * @param path - the path of the entry being updated
7483
+ * @param type - the type of the entry being updated (blob or tree)
7484
+ * @param pathEntries - parent path entries
7485
+ * @param pathParts - parent path parts
7486
+ * @private
7487
+ */
7154
7488
  async updateTreeHierarchy(existingOid, updatedOid, path7, type, pathEntries, pathParts) {
7155
7489
  const lastIdx = pathEntries.length - 1;
7156
7490
  const parentEntry = pathEntries[lastIdx];
@@ -7206,6 +7540,13 @@ var IsomorphicBridge = class {
7206
7540
  );
7207
7541
  }
7208
7542
  }
7543
+ /**
7544
+ * Creates a commit for the specified tree and updates the specified ref to point to the commit
7545
+ *
7546
+ * @param treeSha - sha of the new tree
7547
+ * @param ref - the ref that should be updated
7548
+ * @private
7549
+ */
7209
7550
  async commitTree(treeSha, ref) {
7210
7551
  const commitSha = await git2.writeCommit({
7211
7552
  ...this.isomorphicConfig,
@@ -7218,6 +7559,7 @@ var IsomorphicBridge = class {
7218
7559
  })
7219
7560
  ],
7220
7561
  message: this.commitMessage,
7562
+ // TODO these should be configurable
7221
7563
  author: this.getAuthor(),
7222
7564
  committer: this.getCommitter()
7223
7565
  }
@@ -7455,5 +7797,5 @@ export {
7455
7797
  transformDocument,
7456
7798
  transformDocumentIntoPayload
7457
7799
  };
7458
- //! Replaces _.flattenDeep()
7459
7800
  //! Replaces _.get()
7801
+ //! Replaces _.flattenDeep()