@easy-cms/plugin-graphql 0.43.0 → 0.44.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 +7 -3
- package/dist/index.js +9 -6
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EasyCMS, AuthUser, ID, RawDocument, ResolvedConfig, Plugin } from '@easy-cms/core';
|
|
1
|
+
import { RequestContext, EasyCMS, AuthUser, ID, RawDocument, ResolvedConfig, Plugin } from '@easy-cms/core';
|
|
2
2
|
import { GraphQLSchema, ExecutionResult, GraphQLScalarType, GraphQLObjectType, GraphQLFieldConfig } from 'graphql';
|
|
3
3
|
|
|
4
4
|
/** How documents are read: what the query asked for, kept with each document it returns. */
|
|
@@ -9,10 +9,12 @@ interface ReadArgs {
|
|
|
9
9
|
}
|
|
10
10
|
/** What resolvers receive as their context, yours from `extend` included. */
|
|
11
11
|
interface GraphQLContext {
|
|
12
|
-
/** The Local API. Pass `{ user, overrideAccess: false }` to apply the reader's access rules. */
|
|
12
|
+
/** The Local API. Pass `{ user, context, overrideAccess: false }` to apply the reader's access rules. */
|
|
13
13
|
readonly cms: EasyCMS;
|
|
14
14
|
/** The logged-in user or API key, or `null`. */
|
|
15
15
|
readonly user: AuthUser | null;
|
|
16
|
+
/** The request's context (`onRequest`, e.g. its tenant): pass it to the Local API with `user`. */
|
|
17
|
+
readonly context: RequestContext;
|
|
16
18
|
/**
|
|
17
19
|
* One document by id, with the reader's access. Loads in the same tick are batched into one
|
|
18
20
|
* query per collection, and count towards `limits.documents`.
|
|
@@ -24,6 +26,8 @@ interface GraphQLContext {
|
|
|
24
26
|
interface ContextOptions {
|
|
25
27
|
/** Most documents one request may load. Default 2000. */
|
|
26
28
|
readonly documents?: number;
|
|
29
|
+
/** The request's context (`onRequest`). */
|
|
30
|
+
readonly context?: RequestContext;
|
|
27
31
|
}
|
|
28
32
|
/** The context of one request: its user, its batched loads, its document count. */
|
|
29
33
|
declare function createContext(cms: EasyCMS, user: AuthUser | null, options?: ContextOptions): GraphQLContext;
|
|
@@ -46,7 +50,7 @@ interface GraphQLRequest {
|
|
|
46
50
|
}
|
|
47
51
|
type Json = Record<string, unknown>;
|
|
48
52
|
/** Parses, checks and runs one request, for the user. */
|
|
49
|
-
declare function runGraphQL(cms: EasyCMS, user: AuthUser | null, input: GraphQLRequest, options: RunOptions, onlyQueries?: boolean): Promise<{
|
|
53
|
+
declare function runGraphQL(cms: EasyCMS, user: AuthUser | null, input: GraphQLRequest, options: RunOptions, onlyQueries?: boolean, context?: RequestContext): Promise<{
|
|
50
54
|
status: number;
|
|
51
55
|
body: ExecutionResult | Json;
|
|
52
56
|
allow?: string;
|
package/dist/index.js
CHANGED
|
@@ -22,6 +22,7 @@ function toId(value) {
|
|
|
22
22
|
}
|
|
23
23
|
function createContext(cms, user, options = {}) {
|
|
24
24
|
const max = options.documents ?? DEFAULT_DOCUMENTS;
|
|
25
|
+
const context = options.context ?? {};
|
|
25
26
|
let loaded = 0;
|
|
26
27
|
const count = (documents) => {
|
|
27
28
|
loaded += documents;
|
|
@@ -42,6 +43,7 @@ function createContext(cms, user, options = {}) {
|
|
|
42
43
|
try {
|
|
43
44
|
const found = await cms.find(collection, {
|
|
44
45
|
user,
|
|
46
|
+
context,
|
|
45
47
|
overrideAccess: false,
|
|
46
48
|
where: { id: { in: [...ids] } },
|
|
47
49
|
limit: 0,
|
|
@@ -66,6 +68,7 @@ function createContext(cms, user, options = {}) {
|
|
|
66
68
|
return {
|
|
67
69
|
cms,
|
|
68
70
|
user,
|
|
71
|
+
context,
|
|
69
72
|
count,
|
|
70
73
|
load: (collection, id, read = {}) => loaderFor(collection, read).load(id)
|
|
71
74
|
};
|
|
@@ -264,7 +267,7 @@ function fromBody(body) {
|
|
|
264
267
|
operationName: body.operationName ?? null
|
|
265
268
|
};
|
|
266
269
|
}
|
|
267
|
-
async function runGraphQL(cms, user, input, options, onlyQueries = false) {
|
|
270
|
+
async function runGraphQL(cms, user, input, options, onlyQueries = false, context = {}) {
|
|
268
271
|
let document;
|
|
269
272
|
try {
|
|
270
273
|
document = parse(input.query);
|
|
@@ -300,7 +303,7 @@ async function runGraphQL(cms, user, input, options, onlyQueries = false) {
|
|
|
300
303
|
document,
|
|
301
304
|
variableValues: input.variables ?? void 0,
|
|
302
305
|
operationName: input.operationName ?? void 0,
|
|
303
|
-
contextValue: createContext(cms, user, { documents: options.documents })
|
|
306
|
+
contextValue: createContext(cms, user, { documents: options.documents, context })
|
|
304
307
|
});
|
|
305
308
|
const body = {
|
|
306
309
|
...result.errors ? { errors: result.errors.map((e) => formatError(e, cms)) } : {},
|
|
@@ -320,7 +323,7 @@ async function handleGraphQL(args, options) {
|
|
|
320
323
|
}
|
|
321
324
|
const input = get ? fromSearchParams(url) : fromBody(await args.json());
|
|
322
325
|
if (typeof input === "string") return requestError(input, request);
|
|
323
|
-
const { status, body, allow } = await runGraphQL(cms, user, input, options, get);
|
|
326
|
+
const { status, body, allow } = await runGraphQL(cms, user, input, options, get, args.context);
|
|
324
327
|
return respond(body, status, request, allow ? { allow } : {});
|
|
325
328
|
}
|
|
326
329
|
|
|
@@ -337,7 +340,7 @@ import { printSchema } from "graphql";
|
|
|
337
340
|
// src/info.ts
|
|
338
341
|
var INFO = {
|
|
339
342
|
name: "@easy-cms/plugin-graphql",
|
|
340
|
-
...true ? { version: "0.
|
|
343
|
+
...true ? { version: "0.44.0" } : {}
|
|
341
344
|
};
|
|
342
345
|
|
|
343
346
|
// src/schema.ts
|
|
@@ -998,7 +1001,7 @@ var Builder = class {
|
|
|
998
1001
|
const type = this.objects.get(slug);
|
|
999
1002
|
const drafts = c.drafts === true;
|
|
1000
1003
|
const where = this.whereType(c.fields, names.type, drafts, true);
|
|
1001
|
-
const access = (ctx) => ({ user: ctx.user, overrideAccess: false });
|
|
1004
|
+
const access = (ctx) => ({ user: ctx.user, context: ctx.context, overrideAccess: false });
|
|
1002
1005
|
query[names.one] = {
|
|
1003
1006
|
type,
|
|
1004
1007
|
...describe(c.labels?.singular),
|
|
@@ -1125,7 +1128,7 @@ var Builder = class {
|
|
|
1125
1128
|
const slug = g.slug;
|
|
1126
1129
|
const names = this.names.globals.get(slug);
|
|
1127
1130
|
const drafts = g.drafts === true;
|
|
1128
|
-
const access = (ctx) => ({ user: ctx.user, overrideAccess: false });
|
|
1131
|
+
const access = (ctx) => ({ user: ctx.user, context: ctx.context, overrideAccess: false });
|
|
1129
1132
|
query[names.one] = {
|
|
1130
1133
|
type,
|
|
1131
1134
|
...describe(g.label),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@easy-cms/plugin-graphql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.44.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.
|
|
23
|
+
"@easy-cms/core": "^0.44.0"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@easy-cms/core": "^0.
|
|
27
|
-
"@easy-cms/db-sqlite": "^0.
|
|
26
|
+
"@easy-cms/core": "^0.44.0",
|
|
27
|
+
"@easy-cms/db-sqlite": "^0.44.0"
|
|
28
28
|
},
|
|
29
29
|
"engines": {
|
|
30
30
|
"node": ">=22.12"
|