@tinacms/graphql 0.56.0 → 0.56.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # tina-graphql
2
2
 
3
+ ## 0.56.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 4b7795612: Adds support for collection.templates to TinaAdmin
8
+
3
9
  ## 0.56.0
4
10
 
5
11
  ### Minor Changes
package/dist/index.js CHANGED
@@ -11264,11 +11264,6 @@ var Builder = class {
11264
11264
  return astBuilder.FieldDefinition({
11265
11265
  name: "createDocument",
11266
11266
  args: [
11267
- astBuilder.InputValueDefinition({
11268
- name: "collection",
11269
- required: true,
11270
- type: astBuilder.TYPES.String
11271
- }),
11272
11267
  astBuilder.InputValueDefinition({
11273
11268
  name: "relativePath",
11274
11269
  required: true,
@@ -11291,11 +11286,6 @@ var Builder = class {
11291
11286
  return astBuilder.FieldDefinition({
11292
11287
  name: "updateDocument",
11293
11288
  args: [
11294
- astBuilder.InputValueDefinition({
11295
- name: "collection",
11296
- required: true,
11297
- type: astBuilder.TYPES.String
11298
- }),
11299
11289
  astBuilder.InputValueDefinition({
11300
11290
  name: "relativePath",
11301
11291
  required: true,
@@ -21446,6 +21436,7 @@ var Resolver = class {
21446
21436
  const templates = {};
21447
21437
  await sequential(collectable.templates, async (template) => {
21448
21438
  templates[lastItem(template.namespace)] = {
21439
+ template,
21449
21440
  fields: await sequential(template.fields, async (field) => {
21450
21441
  return this.resolveField(field);
21451
21442
  })
@@ -21464,64 +21455,111 @@ var Resolver = class {
21464
21455
  throw e;
21465
21456
  }
21466
21457
  };
21467
- this.resolveDocument = async ({
21468
- value,
21469
- args,
21470
- collection: collectionName,
21471
- isMutation,
21472
- isCreation
21473
- }) => {
21474
- const collectionNames = this.tinaSchema.getCollections().map((item) => item.name);
21475
- assertShape(collectionName, (yup2) => {
21476
- return yup2.mixed().oneOf(collectionNames);
21477
- }, `"collection" must be one of: [${collectionNames.join(", ")}] but got ${collectionName}`);
21478
- assertShape(args, (yup2) => yup2.object({ relativePath: yup2.string().required() }));
21479
- const collection = await this.tinaSchema.getCollection(collectionName);
21480
- const realPath = import_path3.default.join(collection == null ? void 0 : collection.path, args.relativePath);
21481
- if (isMutation) {
21482
- if (isCreation) {
21483
- if (await this.database.documentExists(realPath)) {
21484
- throw new Error(`Unable to add document, ${realPath} already exists`);
21485
- }
21486
- const templateInfo2 = this.tinaSchema.getTemplatesForCollectable(collection);
21487
- switch (templateInfo2.type) {
21488
- case "object":
21489
- await this.database.put(realPath, {});
21490
- break;
21491
- case "union":
21492
- const templateString = args.template;
21493
- const template = templateInfo2.templates.find((template2) => lastItem(template2.namespace) === templateString);
21494
- if (!args.template) {
21495
- throw new Error(`Must specify a template when creating content for a collection with multiple templates. Possible templates are: ${templateInfo2.templates.map((t) => lastItem(t.namespace)).join(" ")}`);
21496
- }
21497
- if (!template) {
21498
- throw new Error(`Expected to find template named ${templateString} in collection "${collectionName}" but none was found. Possible templates are: ${templateInfo2.templates.map((t) => lastItem(t.namespace)).join(" ")}`);
21458
+ this.buildObjectMutations = (fieldValue, field) => {
21459
+ if (field.fields) {
21460
+ const objectTemplate = typeof field.fields === "string" ? this.tinaSchema.getGlobalTemplate(field.fields) : field;
21461
+ if (Array.isArray(fieldValue)) {
21462
+ return fieldValue.map((item) => this.buildFieldMutations(item, objectTemplate));
21463
+ } else {
21464
+ return this.buildFieldMutations(fieldValue, objectTemplate);
21465
+ }
21466
+ }
21467
+ if (field.templates) {
21468
+ if (Array.isArray(fieldValue)) {
21469
+ return fieldValue.map((item) => {
21470
+ if (typeof item === "string") {
21471
+ throw new Error(`Expected object for template value for field ${field.name}`);
21472
+ }
21473
+ const templates = field.templates.map((templateOrTemplateName) => {
21474
+ if (typeof templateOrTemplateName === "string") {
21475
+ return this.tinaSchema.getGlobalTemplate(templateOrTemplateName);
21499
21476
  }
21500
- await this.database.put(realPath, {
21501
- _template: lastItem(template.namespace)
21502
- });
21503
- return this.getDocument(realPath);
21504
- }
21477
+ return templateOrTemplateName;
21478
+ });
21479
+ const [templateName] = Object.entries(item)[0];
21480
+ const template = templates.find((template2) => template2.name === templateName);
21481
+ if (!template) {
21482
+ throw new Error(`Expected to find template ${templateName}`);
21483
+ }
21484
+ return __spreadProps(__spreadValues({}, this.buildFieldMutations(item[template.name], template)), {
21485
+ _template: template.name
21486
+ });
21487
+ });
21505
21488
  } else {
21506
- if (!await this.database.documentExists(realPath)) {
21507
- throw new Error(`Unable to update document, ${realPath} does not exist`);
21489
+ if (typeof fieldValue === "string") {
21490
+ throw new Error(`Expected object for template value for field ${field.name}`);
21491
+ }
21492
+ const templates = field.templates.map((templateOrTemplateName) => {
21493
+ if (typeof templateOrTemplateName === "string") {
21494
+ return this.tinaSchema.getGlobalTemplate(templateOrTemplateName);
21495
+ }
21496
+ return templateOrTemplateName;
21497
+ });
21498
+ const [templateName] = Object.entries(fieldValue)[0];
21499
+ const template = templates.find((template2) => template2.name === templateName);
21500
+ if (!template) {
21501
+ throw new Error(`Expected to find template ${templateName}`);
21508
21502
  }
21503
+ return __spreadProps(__spreadValues({}, this.buildFieldMutations(fieldValue[template.name], template)), {
21504
+ _template: template.name
21505
+ });
21509
21506
  }
21507
+ }
21508
+ };
21509
+ this.createResolveDocument = async ({
21510
+ collection,
21511
+ realPath,
21512
+ args,
21513
+ isAddPendingDocument
21514
+ }) => {
21515
+ if (isAddPendingDocument === true) {
21510
21516
  const templateInfo = this.tinaSchema.getTemplatesForCollectable(collection);
21511
- const params = this.buildParams(args);
21512
21517
  switch (templateInfo.type) {
21513
21518
  case "object":
21514
- if (params) {
21515
- const values = this.buildFieldMutations(params, templateInfo.template);
21519
+ await this.database.put(realPath, {});
21520
+ break;
21521
+ case "union":
21522
+ const templateString = args.template;
21523
+ const template = templateInfo.templates.find((template2) => lastItem(template2.namespace) === templateString);
21524
+ if (!args.template) {
21525
+ throw new Error(`Must specify a template when creating content for a collection with multiple templates. Possible templates are: ${templateInfo.templates.map((t) => lastItem(t.namespace)).join(" ")}`);
21526
+ }
21527
+ if (!template) {
21528
+ throw new Error(`Expected to find template named ${templateString} in collection "${collection.name}" but none was found. Possible templates are: ${templateInfo.templates.map((t) => lastItem(t.namespace)).join(" ")}`);
21529
+ }
21530
+ await this.database.put(realPath, {
21531
+ _template: lastItem(template.namespace)
21532
+ });
21533
+ }
21534
+ return this.getDocument(realPath);
21535
+ }
21536
+ const params = this.buildObjectMutations(args.params[collection.name], collection);
21537
+ await this.database.put(realPath, params);
21538
+ return this.getDocument(realPath);
21539
+ };
21540
+ this.updateResolveDocument = async ({
21541
+ collection,
21542
+ realPath,
21543
+ args,
21544
+ isAddPendingDocument,
21545
+ isCollectionSpecific
21546
+ }) => {
21547
+ if (isAddPendingDocument === true) {
21548
+ const templateInfo = this.tinaSchema.getTemplatesForCollectable(collection);
21549
+ const params2 = this.buildParams(args);
21550
+ switch (templateInfo.type) {
21551
+ case "object":
21552
+ if (params2) {
21553
+ const values = this.buildFieldMutations(params2, templateInfo.template);
21516
21554
  await this.database.put(realPath, values);
21517
21555
  }
21518
21556
  break;
21519
21557
  case "union":
21520
21558
  await sequential(templateInfo.templates, async (template) => {
21521
- const templateParams = params[lastItem(template.namespace)];
21559
+ const templateParams = params2[lastItem(template.namespace)];
21522
21560
  if (templateParams) {
21523
21561
  if (typeof templateParams === "string") {
21524
- throw new Error(`Expected to find an objet for template params, but got string`);
21562
+ throw new Error(`Expected to find an object for template params, but got string`);
21525
21563
  }
21526
21564
  const values = __spreadProps(__spreadValues({}, this.buildFieldMutations(templateParams, template)), {
21527
21565
  _template: lastItem(template.namespace)
@@ -21530,9 +21568,58 @@ var Resolver = class {
21530
21568
  }
21531
21569
  });
21532
21570
  }
21571
+ return this.getDocument(realPath);
21533
21572
  }
21573
+ const params = this.buildObjectMutations(isCollectionSpecific ? args.params : args.params[collection.name], collection);
21574
+ await this.database.put(realPath, params);
21534
21575
  return this.getDocument(realPath);
21535
21576
  };
21577
+ this.resolveDocument = async ({
21578
+ args,
21579
+ collection: collectionName,
21580
+ isMutation,
21581
+ isCreation,
21582
+ isAddPendingDocument,
21583
+ isCollectionSpecific
21584
+ }) => {
21585
+ let collectionLookup = collectionName || void 0;
21586
+ if (!collectionLookup && isCollectionSpecific === false) {
21587
+ collectionLookup = Object.keys(args.params)[0];
21588
+ }
21589
+ const collectionNames = this.tinaSchema.getCollections().map((item) => item.name);
21590
+ assertShape(collectionLookup, (yup2) => {
21591
+ return yup2.mixed().oneOf(collectionNames);
21592
+ }, `"collection" must be one of: [${collectionNames.join(", ")}] but got ${collectionLookup}`);
21593
+ assertShape(args, (yup2) => yup2.object({ relativePath: yup2.string().required() }));
21594
+ const collection = await this.tinaSchema.getCollection(collectionLookup);
21595
+ const realPath = import_path3.default.join(collection == null ? void 0 : collection.path, args.relativePath);
21596
+ const alreadyExists = await this.database.documentExists(realPath);
21597
+ if (isMutation) {
21598
+ if (isCreation) {
21599
+ if (alreadyExists === true) {
21600
+ throw new Error(`Unable to add document, ${realPath} already exists`);
21601
+ }
21602
+ return this.createResolveDocument({
21603
+ collection,
21604
+ realPath,
21605
+ args,
21606
+ isAddPendingDocument
21607
+ });
21608
+ }
21609
+ if (alreadyExists === false) {
21610
+ throw new Error(`Unable to update document, ${realPath} does not exist`);
21611
+ }
21612
+ return this.updateResolveDocument({
21613
+ collection,
21614
+ realPath,
21615
+ args,
21616
+ isAddPendingDocument,
21617
+ isCollectionSpecific
21618
+ });
21619
+ } else {
21620
+ return this.getDocument(realPath);
21621
+ }
21622
+ };
21536
21623
  this.resolveCollectionConnections = async ({ ids }) => {
21537
21624
  return {
21538
21625
  totalCount: ids.length,
@@ -21582,40 +21669,7 @@ var Resolver = class {
21582
21669
  accum[fieldName] = fieldValue;
21583
21670
  break;
21584
21671
  case "object":
21585
- if (field.fields) {
21586
- const objectTemplate = typeof field.fields === "string" ? this.tinaSchema.getGlobalTemplate(field.fields) : field;
21587
- if (Array.isArray(fieldValue)) {
21588
- accum[fieldName] = fieldValue.map((item) => this.buildFieldMutations(item, objectTemplate));
21589
- } else {
21590
- accum[fieldName] = this.buildFieldMutations(fieldValue, objectTemplate);
21591
- }
21592
- break;
21593
- }
21594
- if (field.templates) {
21595
- if (Array.isArray(fieldValue)) {
21596
- accum[fieldName] = fieldValue.map((item) => {
21597
- if (typeof item === "string") {
21598
- throw new Error(`Expected object for template value for field ${field.name}`);
21599
- }
21600
- const templates = field.templates.map((templateOrTemplateName) => {
21601
- if (typeof templateOrTemplateName === "string") {
21602
- return this.tinaSchema.getGlobalTemplate(templateOrTemplateName);
21603
- }
21604
- return templateOrTemplateName;
21605
- });
21606
- const [templateName] = Object.entries(item)[0];
21607
- const template2 = templates.find((template3) => template3.name === templateName);
21608
- if (!template2) {
21609
- throw new Error(`Expected to find template ${templateName}`);
21610
- }
21611
- return __spreadProps(__spreadValues({}, this.buildFieldMutations(item[template2.name], template2)), {
21612
- _template: template2.name
21613
- });
21614
- });
21615
- } else {
21616
- throw new Error("Not implement for polymorphic objects which are not lists");
21617
- }
21618
- }
21672
+ accum[fieldName] = this.buildObjectMutations(fieldValue, field);
21619
21673
  break;
21620
21674
  case "rich-text":
21621
21675
  field;
@@ -21956,20 +22010,21 @@ var resolve = async ({
21956
22010
  }
21957
22011
  if (args && args.collection && info.fieldName === "addPendingDocument") {
21958
22012
  return resolver2.resolveDocument({
21959
- value,
21960
22013
  args: __spreadProps(__spreadValues({}, args), { params: {} }),
21961
22014
  collection: args.collection,
21962
22015
  isMutation,
21963
- isCreation: true
22016
+ isCreation: true,
22017
+ isAddPendingDocument: true
21964
22018
  });
21965
22019
  }
21966
22020
  if (["getDocument", "createDocument", "updateDocument"].includes(info.fieldName)) {
21967
22021
  const result2 = await resolver2.resolveDocument({
21968
- value,
21969
22022
  args,
21970
22023
  collection: args.collection,
21971
22024
  isMutation,
21972
- isCreation
22025
+ isCreation,
22026
+ isAddPendingDocument: false,
22027
+ isCollectionSpecific: false
21973
22028
  });
21974
22029
  if (!isMutation) {
21975
22030
  const mutationPath = buildMutationPath(info, {
@@ -21992,11 +22047,12 @@ var resolve = async ({
21992
22047
  return value;
21993
22048
  }
21994
22049
  const result = value || await resolver2.resolveDocument({
21995
- value,
21996
22050
  args,
21997
22051
  collection: lookup.collection,
21998
22052
  isMutation,
21999
- isCreation
22053
+ isCreation,
22054
+ isAddPendingDocument: false,
22055
+ isCollectionSpecific: true
22000
22056
  });
22001
22057
  if (!isMutation) {
22002
22058
  const mutationPath = buildMutationPath(info, {
@@ -12,6 +12,7 @@ limitations under the License.
12
12
  */
13
13
  import { TinaSchema } from '../schema';
14
14
  import { Database, CollectionDocumentListLookup } from '../database';
15
+ import type { Collectable, TinaCloudCollection } from '../types';
15
16
  interface ResolverConfig {
16
17
  database: Database;
17
18
  tinaSchema: TinaSchema;
@@ -65,7 +66,7 @@ export declare class Resolver {
65
66
  path: string;
66
67
  relativePath: string;
67
68
  breadcrumbs: string[];
68
- collection: import("../types").TinaCloudCollection<true>;
69
+ collection: TinaCloudCollection<true>;
69
70
  template: string | number;
70
71
  };
71
72
  data: {
@@ -87,12 +88,91 @@ export declare class Resolver {
87
88
  };
88
89
  }>;
89
90
  getDocumentFields: () => Promise<{}>;
90
- resolveDocument: ({ value, args, collection: collectionName, isMutation, isCreation, }: {
91
- value: unknown;
91
+ buildObjectMutations: (fieldValue: any, field: Collectable) => {
92
+ [key: string]: unknown;
93
+ } | {
94
+ [key: string]: unknown;
95
+ }[];
96
+ createResolveDocument: ({ collection, realPath, args, isAddPendingDocument, }: {
97
+ collection: TinaCloudCollection<true>;
98
+ realPath: string;
99
+ args: unknown;
100
+ isAddPendingDocument: boolean;
101
+ }) => Promise<{
102
+ __typename: string;
103
+ id: string;
104
+ sys: {
105
+ basename: string;
106
+ filename: string;
107
+ extension: string;
108
+ path: string;
109
+ relativePath: string;
110
+ breadcrumbs: string[];
111
+ collection: TinaCloudCollection<true>;
112
+ template: string | number;
113
+ };
114
+ data: {
115
+ _collection: string;
116
+ _template: string;
117
+ };
118
+ values: {
119
+ _collection: string;
120
+ _template: string;
121
+ };
122
+ dataJSON: {
123
+ _collection: string;
124
+ _template: string;
125
+ };
126
+ form: {
127
+ label: string;
128
+ name: string;
129
+ fields: unknown[];
130
+ };
131
+ }>;
132
+ updateResolveDocument: ({ collection, realPath, args, isAddPendingDocument, isCollectionSpecific, }: {
133
+ collection: TinaCloudCollection<true>;
134
+ realPath: string;
135
+ args: unknown;
136
+ isAddPendingDocument: boolean;
137
+ isCollectionSpecific: boolean;
138
+ }) => Promise<{
139
+ __typename: string;
140
+ id: string;
141
+ sys: {
142
+ basename: string;
143
+ filename: string;
144
+ extension: string;
145
+ path: string;
146
+ relativePath: string;
147
+ breadcrumbs: string[];
148
+ collection: TinaCloudCollection<true>;
149
+ template: string | number;
150
+ };
151
+ data: {
152
+ _collection: string;
153
+ _template: string;
154
+ };
155
+ values: {
156
+ _collection: string;
157
+ _template: string;
158
+ };
159
+ dataJSON: {
160
+ _collection: string;
161
+ _template: string;
162
+ };
163
+ form: {
164
+ label: string;
165
+ name: string;
166
+ fields: unknown[];
167
+ };
168
+ }>;
169
+ resolveDocument: ({ args, collection: collectionName, isMutation, isCreation, isAddPendingDocument, isCollectionSpecific, }: {
92
170
  args: unknown;
93
- collection: string;
171
+ collection?: string;
94
172
  isMutation: boolean;
95
173
  isCreation?: boolean;
174
+ isAddPendingDocument?: boolean;
175
+ isCollectionSpecific?: boolean;
96
176
  }) => Promise<{
97
177
  __typename: string;
98
178
  id: string;
@@ -103,7 +183,7 @@ export declare class Resolver {
103
183
  path: string;
104
184
  relativePath: string;
105
185
  breadcrumbs: string[];
106
- collection: import("../types").TinaCloudCollection<true>;
186
+ collection: TinaCloudCollection<true>;
107
187
  template: string | number;
108
188
  };
109
189
  data: {
@@ -139,7 +219,7 @@ export declare class Resolver {
139
219
  path: string;
140
220
  relativePath: string;
141
221
  breadcrumbs: string[];
142
- collection: import("../types").TinaCloudCollection<true>;
222
+ collection: TinaCloudCollection<true>;
143
223
  template: string | number;
144
224
  };
145
225
  data: {
@@ -178,7 +258,7 @@ export declare class Resolver {
178
258
  path: string;
179
259
  relativePath: string;
180
260
  breadcrumbs: string[];
181
- collection: import("../types").TinaCloudCollection<true>;
261
+ collection: TinaCloudCollection<true>;
182
262
  template: string | number;
183
263
  };
184
264
  data: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinacms/graphql",
3
- "version": "0.56.0",
3
+ "version": "0.56.1",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
6
6
  "files": [