@easy-cms/plugin-graphql 0.48.0 → 0.49.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.d.ts CHANGED
@@ -107,8 +107,10 @@ interface SchemaOptions {
107
107
  * Default: from the slug, `categories` → type `Category`, queries `category` and `categories`.
108
108
  */
109
109
  readonly names?: NameOverrides;
110
- /** Collections and globals left out of the schema, by slug. */
111
- readonly exclude?: readonly string[];
110
+ /** Collections in the schema, by slug. Default: all (but internal ones and API keys). */
111
+ readonly collections?: readonly string[];
112
+ /** Globals in the schema, by slug. Default: all. */
113
+ readonly globals?: readonly string[];
112
114
  /** Your own queries, mutations and fields. */
113
115
  readonly extend?: (args: ExtendArgs) => GraphQLExtension;
114
116
  }
package/dist/index.js CHANGED
@@ -77,11 +77,9 @@ function createContext(cms, user, options = {}) {
77
77
  // src/http.ts
78
78
  import {
79
79
  EasyCMSError,
80
- ForbiddenError as ForbiddenError2,
81
- NotFoundError,
82
- UnauthorizedError as UnauthorizedError2,
83
80
  ValidationError
84
81
  } from "@easy-cms/core";
82
+ import { codeOfStatus } from "@easy-cms/core/internal";
85
83
  import {
86
84
  execute,
87
85
  getOperationAST,
@@ -172,28 +170,6 @@ function depthLimit(max) {
172
170
  }
173
171
 
174
172
  // src/http.ts
175
- function codeOf(error) {
176
- if (error instanceof ValidationError) return "VALIDATION_ERROR";
177
- if (error instanceof UnauthorizedError2) return "UNAUTHORIZED";
178
- if (error instanceof ForbiddenError2) return "FORBIDDEN";
179
- if (error instanceof NotFoundError) return "NOT_FOUND";
180
- switch (error.status) {
181
- case 400:
182
- return "BAD_REQUEST";
183
- case 401:
184
- return "UNAUTHORIZED";
185
- case 403:
186
- return "FORBIDDEN";
187
- case 404:
188
- return "NOT_FOUND";
189
- case 413:
190
- return "PAYLOAD_TOO_LARGE";
191
- case 429:
192
- return "TOO_MANY_REQUESTS";
193
- default:
194
- return "INTERNAL_SERVER_ERROR";
195
- }
196
- }
197
173
  function formatError(error, cms) {
198
174
  const original = error.originalError;
199
175
  const base = {
@@ -206,7 +182,7 @@ function formatError(error, cms) {
206
182
  ...base,
207
183
  extensions: {
208
184
  ...error.extensions,
209
- code: codeOf(original),
185
+ code: original.code,
210
186
  ...original instanceof ValidationError ? { fields: original.errors } : {}
211
187
  }
212
188
  };
@@ -236,7 +212,7 @@ function respond(body, status, request, headers = {}) {
236
212
  }
237
213
  });
238
214
  }
239
- var requestError = (message, request, status = 400) => respond({ errors: [{ message, extensions: { code: "BAD_REQUEST" } }] }, status, request);
215
+ var requestError = (message, request, status = 400) => respond({ errors: [{ message, extensions: { code: codeOfStatus(status) } }] }, status, request);
240
216
  function fromSearchParams(url) {
241
217
  const query = url.searchParams.get("query");
242
218
  if (!query) return "Send a query: ?query={...}";
@@ -292,7 +268,7 @@ async function runGraphQL(cms, user, input, options, onlyQueries = false, contex
292
268
  errors: [
293
269
  {
294
270
  message: `Send ${operation.operation}s with POST`,
295
- extensions: { code: "BAD_REQUEST" }
271
+ extensions: { code: "BAD_USER_INPUT" }
296
272
  }
297
273
  ]
298
274
  }
@@ -331,25 +307,21 @@ async function handleGraphQL(args, options) {
331
307
  import { writeFile } from "fs/promises";
332
308
  import { isAbsolute, resolve } from "path";
333
309
  import {
334
- API_KEYS as API_KEYS2,
335
- definePlugin,
336
- INTERNAL_COLLECTIONS as INTERNAL_COLLECTIONS2
310
+ definePlugin
337
311
  } from "@easy-cms/core";
312
+ import { API_KEYS as API_KEYS2, checkRenamedOptions, INTERNAL_COLLECTIONS as INTERNAL_COLLECTIONS2 } from "@easy-cms/core/internal";
338
313
  import { printSchema } from "graphql";
339
314
 
340
315
  // src/info.ts
341
316
  var INFO = {
342
317
  name: "@easy-cms/plugin-graphql",
343
- ...true ? { version: "0.48.0" } : {}
318
+ // The plugin API it is written for (`PLUGIN_API_VERSION` of @easy-cms/core).
319
+ apiVersion: 1,
320
+ ...true ? { version: "0.49.0" } : {}
344
321
  };
345
322
 
346
323
  // src/schema.ts
347
- import {
348
- API_KEYS,
349
- INTERNAL_COLLECTIONS,
350
- MEDIA,
351
- MEDIA_FOLDERS
352
- } from "@easy-cms/core";
324
+ import { API_KEYS, INTERNAL_COLLECTIONS, MEDIA, MEDIA_FOLDERS } from "@easy-cms/core/internal";
353
325
  import {
354
326
  GraphQLBoolean,
355
327
  GraphQLEnumType,
@@ -367,7 +339,7 @@ import {
367
339
  } from "graphql";
368
340
 
369
341
  // src/names.ts
370
- import { singularize } from "@easy-cms/core";
342
+ import { singularize } from "@easy-cms/core/internal";
371
343
  var VALID_NAME = /^[_A-Za-z][_0-9A-Za-z]*$/;
372
344
  var isValidName = (name) => VALID_NAME.test(name) && !name.startsWith("__");
373
345
  function pascal(name) {
@@ -485,9 +457,9 @@ var list = (type) => (
485
457
  );
486
458
  var hasMany = (field) => "hasMany" in field && field.hasMany === true;
487
459
  var userError = (message) => new GraphQLError4(message, { extensions: { code: "BAD_USER_INPUT" } });
488
- function exposedCollections(config, exclude = []) {
460
+ function exposedCollections(config, only) {
489
461
  return config.collections.filter(
490
- (c) => !INTERNAL_COLLECTIONS.has(c.slug) && c.slug !== API_KEYS && !exclude.includes(c.slug)
462
+ (c) => !INTERNAL_COLLECTIONS.has(c.slug) && c.slug !== API_KEYS && (only === void 0 || only.includes(c.slug))
491
463
  );
492
464
  }
493
465
  function checkNames(collections, globals, overrides = {}) {
@@ -559,9 +531,9 @@ var Builder = class {
559
531
  constructor(config, options) {
560
532
  this.config = config;
561
533
  this.options = options;
562
- const exclude = options.exclude ?? [];
563
- this.collections = exposedCollections(config, exclude);
564
- this.globals = config.globals.filter((g) => !exclude.includes(g.slug));
534
+ this.collections = exposedCollections(config, options.collections);
535
+ const globals = options.globals;
536
+ this.globals = config.globals.filter((g) => globals === void 0 || globals.includes(g.slug));
565
537
  this.names = checkNames(
566
538
  this.collections.map((c) => c.slug),
567
539
  this.globals.map((g) => g.slug),
@@ -1288,19 +1260,22 @@ function toInput(data, fields) {
1288
1260
  }
1289
1261
 
1290
1262
  // src/plugin.ts
1291
- function plannedSlugs(config, exclude) {
1263
+ function plannedSlugs(config, options) {
1292
1264
  const collections = /* @__PURE__ */ new Set(["users", "media"]);
1293
1265
  if (config.upload?.folders === true) collections.add("media-folders");
1294
1266
  for (const c of config.collections ?? []) collections.add(c.slug);
1295
- const keep = (slug) => !INTERNAL_COLLECTIONS2.has(slug) && slug !== API_KEYS2 && !exclude.includes(slug);
1267
+ const only = (list2) => (slug) => list2 === void 0 || list2.includes(slug);
1296
1268
  return {
1297
- collections: [...collections].filter(keep),
1298
- globals: (config.globals ?? []).map((g) => g.slug).filter((s) => !exclude.includes(s))
1269
+ collections: [...collections].filter((slug) => !INTERNAL_COLLECTIONS2.has(slug) && slug !== API_KEYS2).filter(only(options.collections)),
1270
+ globals: (config.globals ?? []).map((g) => g.slug).filter(only(options.globals))
1299
1271
  };
1300
1272
  }
1301
1273
  function graphqlPlugin(options = {}) {
1274
+ checkRenamedOptions("graphqlPlugin", options, {
1275
+ exclude: "collections (and `globals`): the ones in the schema, not the ones left out"
1276
+ });
1302
1277
  return definePlugin((config) => {
1303
- const planned = plannedSlugs(config, options.exclude ?? []);
1278
+ const planned = plannedSlugs(config, options);
1304
1279
  checkNames(planned.collections, planned.globals, options.names);
1305
1280
  const schemas = /* @__PURE__ */ new WeakMap();
1306
1281
  const schemaOf = (cms) => {
@@ -1343,7 +1318,7 @@ other tools. Default file: schema.graphql. Opens the database like the other com
1343
1318
  { path, method: "get", handler },
1344
1319
  { path, method: "post", handler }
1345
1320
  ],
1346
- commands: [...config.commands ?? [], generate]
1321
+ cliCommands: [...config.cliCommands ?? [], generate]
1347
1322
  };
1348
1323
  }, INFO);
1349
1324
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cms/plugin-graphql",
3
- "version": "0.48.0",
3
+ "version": "0.49.0",
4
4
  "description": "GraphQL API for Easy CMS: queries and mutations for every collection and global, with the same access rules as REST",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -20,11 +20,11 @@
20
20
  "graphql": "^16.11.0 || ^17.0.0"
21
21
  },
22
22
  "peerDependencies": {
23
- "@easy-cms/core": "^0.48.0"
23
+ "@easy-cms/core": "^0.49.0"
24
24
  },
25
25
  "devDependencies": {
26
- "@easy-cms/core": "^0.48.0",
27
- "@easy-cms/db-sqlite": "^0.48.0"
26
+ "@easy-cms/core": "^0.49.0",
27
+ "@easy-cms/db-sqlite": "^0.49.0"
28
28
  },
29
29
  "engines": {
30
30
  "node": ">=22.12"