@easy-cms/plugin-graphql 1.0.1 → 1.2.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.
Files changed (2) hide show
  1. package/dist/index.js +25 -8
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -317,7 +317,7 @@ var INFO = {
317
317
  name: "@easy-cms/plugin-graphql",
318
318
  // The plugin API it is written for (`PLUGIN_API_VERSION` of @easy-cms/core).
319
319
  apiVersion: 1,
320
- ...true ? { version: "1.0.1" } : {}
320
+ ...true ? { version: "1.2.0" } : {}
321
321
  };
322
322
 
323
323
  // src/schema.ts
@@ -1148,12 +1148,13 @@ var Builder = class {
1148
1148
  });
1149
1149
  }
1150
1150
  nestedInputs = /* @__PURE__ */ new Map();
1151
+ nestedCreateInputs = /* @__PURE__ */ new Map();
1151
1152
  inputFields(fields, path, strict) {
1152
1153
  const out = {};
1153
1154
  for (const field of fields) {
1154
1155
  if (field.hidden || !isValidName(field.name)) continue;
1155
- const type = this.inputOf(field, path);
1156
- const required = strict && field.required === true && field.defaultValue === void 0 && !["slug", "group", "array", "blocks"].includes(field.type);
1156
+ const type = this.inputOf(field, path, strict);
1157
+ const required = strict && isRequiredInput(field);
1157
1158
  out[field.name] = {
1158
1159
  type: required ? new GraphQLNonNull(type) : type,
1159
1160
  ...describe(field.label)
@@ -1161,7 +1162,12 @@ var Builder = class {
1161
1162
  }
1162
1163
  return out;
1163
1164
  }
1164
- inputOf(field, path) {
1165
+ /**
1166
+ * `strict` (create inputs): required fields inside groups and array rows are non-null too, in
1167
+ * `…CreateInput` types made only where something inside is required. Updates and globals use
1168
+ * the plain types: drafts may leave required fields empty, and rows are sent whole.
1169
+ */
1170
+ inputOf(field, path, strict = false) {
1165
1171
  const many = (type) => hasMany(field) ? list(type) : type;
1166
1172
  switch (field.type) {
1167
1173
  case "number":
@@ -1183,18 +1189,21 @@ var Builder = class {
1183
1189
  return list(JSONScalar);
1184
1190
  case "group":
1185
1191
  case "array": {
1186
- let type = this.nestedInputs.get(field);
1192
+ const create = strict && requiresInside(field.fields);
1193
+ const cache = create ? this.nestedCreateInputs : this.nestedInputs;
1194
+ let type = cache.get(field);
1187
1195
  if (!type) {
1188
1196
  const base = `${path}${pascal(field.name)}`;
1189
- const name = field.type === "group" ? `${base}Input` : `${base}RowInput`;
1197
+ const kind = create ? "CreateInput" : "Input";
1198
+ const name = field.type === "group" ? `${base}${kind}` : `${base}Row${kind}`;
1190
1199
  type = new GraphQLInputObjectType({
1191
1200
  name: this.claim(name),
1192
1201
  fields: () => ({
1193
1202
  ...field.type === "array" ? { id: { type: GraphQLString } } : {},
1194
- ...this.inputFields(field.fields, base, false)
1203
+ ...this.inputFields(field.fields, base, create)
1195
1204
  })
1196
1205
  });
1197
- this.nestedInputs.set(field, type);
1206
+ cache.set(field, type);
1198
1207
  }
1199
1208
  return field.type === "group" ? type : list(type);
1200
1209
  }
@@ -1203,6 +1212,14 @@ var Builder = class {
1203
1212
  }
1204
1213
  }
1205
1214
  };
1215
+ function isRequiredInput(field) {
1216
+ return field.required === true && field.defaultValue === void 0 && !["slug", "group", "array", "blocks"].includes(field.type);
1217
+ }
1218
+ function requiresInside(fields) {
1219
+ return fields.some(
1220
+ (f) => !f.hidden && isValidName(f.name) && (isRequiredInput(f) || (f.type === "group" || f.type === "array") && requiresInside(f.fields))
1221
+ );
1222
+ }
1206
1223
  function toWhere(input, fields, prefix = "") {
1207
1224
  const out = {};
1208
1225
  for (const [key, value] of Object.entries(input)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cms/plugin-graphql",
3
- "version": "1.0.1",
3
+ "version": "1.2.0",
4
4
  "description": "GraphQL API for Easy CMS: queries and mutations for every collection and global, with the same access rules as REST (experimental)",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -20,13 +20,13 @@
20
20
  },
21
21
  "peerDependencies": {
22
22
  "graphql": "^16.11.0 || ^17.0.0",
23
- "@easy-cms/core": "^1.0.1"
23
+ "@easy-cms/core": "^1.2.0"
24
24
  },
25
25
  "devDependencies": {
26
26
  "graphql": "^17.0.2",
27
27
  "graphql-16": "npm:graphql@^16.14.2",
28
- "@easy-cms/core": "^1.0.1",
29
- "@easy-cms/db-sqlite": "^1.0.1"
28
+ "@easy-cms/core": "^1.2.0",
29
+ "@easy-cms/db-sqlite": "^1.2.0"
30
30
  },
31
31
  "engines": {
32
32
  "node": ">=22.12"