@kubun/graphql 0.8.0 → 0.8.2
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/lib/context.d.ts +83 -4
- package/lib/context.js +1 -1
- package/lib/index.d.ts +1 -1
- package/package.json +5 -6
package/lib/context.d.ts
CHANGED
|
@@ -1,9 +1,69 @@
|
|
|
1
1
|
import type { Tracer } from '@enkaku/otel';
|
|
2
|
-
import
|
|
3
|
-
import type { DocumentData, DocumentFilter, DocumentNode } from '@kubun/protocol';
|
|
2
|
+
import { DocumentID } from '@kubun/id';
|
|
3
|
+
import type { DocumentData, DocumentFilter, DocumentNode, DocumentOrderBy } from '@kubun/protocol';
|
|
4
4
|
import type { GraphQLResolveInfo } from 'graphql';
|
|
5
5
|
import type { Connection, Edge } from 'graphql-relay';
|
|
6
6
|
import type { AllConnectionArguments, ModelAccessDefaults, UpdateMutationInput } from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* Event data for document save notifications used by subscriptions.
|
|
9
|
+
*/
|
|
10
|
+
export type DocumentSavedEvent = {
|
|
11
|
+
type: 'create' | 'update';
|
|
12
|
+
document: DocumentNode;
|
|
13
|
+
previous: {
|
|
14
|
+
data: DocumentData | null;
|
|
15
|
+
};
|
|
16
|
+
getCursor(): string;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Event emitter interface for document events used by subscriptions.
|
|
20
|
+
*/
|
|
21
|
+
export type DocumentEventEmitter = {
|
|
22
|
+
on(event: 'document:saved', handler: (data: DocumentSavedEvent) => void): () => void;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Read-only database interface required by the GraphQL read context.
|
|
26
|
+
* This replaces the direct KubunDB dependency, allowing any store
|
|
27
|
+
* implementation (e.g. GraphStoreAPI) to be used.
|
|
28
|
+
*/
|
|
29
|
+
export type ReadContextDB = {
|
|
30
|
+
getDocument(id: DocumentID): Promise<DocumentNode | null>;
|
|
31
|
+
queryDocuments(params: {
|
|
32
|
+
modelIDs: Array<string>;
|
|
33
|
+
filter?: DocumentFilter;
|
|
34
|
+
orderBy?: DocumentOrderBy;
|
|
35
|
+
owner?: string;
|
|
36
|
+
catalogID?: string;
|
|
37
|
+
first?: number | null;
|
|
38
|
+
last?: number | null;
|
|
39
|
+
before?: string | null;
|
|
40
|
+
after?: string | null;
|
|
41
|
+
}): Promise<{
|
|
42
|
+
entries: Array<{
|
|
43
|
+
cursor: string;
|
|
44
|
+
document: DocumentNode;
|
|
45
|
+
}>;
|
|
46
|
+
hasMore: boolean;
|
|
47
|
+
}>;
|
|
48
|
+
listDocuments(params: {
|
|
49
|
+
modelIDs: Array<string>;
|
|
50
|
+
docIDs: Array<string>;
|
|
51
|
+
}): Promise<Array<DocumentNode>>;
|
|
52
|
+
countDocuments(params: {
|
|
53
|
+
modelIDs: Array<string>;
|
|
54
|
+
filter?: DocumentFilter;
|
|
55
|
+
catalogID?: string;
|
|
56
|
+
}): Promise<number>;
|
|
57
|
+
searchDocuments(params: {
|
|
58
|
+
query: string;
|
|
59
|
+
modelIDs: Array<string>;
|
|
60
|
+
first?: number;
|
|
61
|
+
}): Promise<Array<{
|
|
62
|
+
documentID: string;
|
|
63
|
+
modelID: string;
|
|
64
|
+
rank: number;
|
|
65
|
+
}>>;
|
|
66
|
+
};
|
|
7
67
|
/**
|
|
8
68
|
* Query functions requiring server implementation
|
|
9
69
|
*/
|
|
@@ -46,7 +106,8 @@ export type ComplexityStats = {
|
|
|
46
106
|
resolverTimings?: Map<string, number>;
|
|
47
107
|
};
|
|
48
108
|
export type ReadContextParams = {
|
|
49
|
-
|
|
109
|
+
store: ReadContextDB;
|
|
110
|
+
events?: DocumentEventEmitter;
|
|
50
111
|
viewerDID: string;
|
|
51
112
|
catalogID?: string;
|
|
52
113
|
accessChecker?: AccessChecker;
|
|
@@ -112,4 +173,22 @@ export type MutationContext = {
|
|
|
112
173
|
success: boolean;
|
|
113
174
|
};
|
|
114
175
|
};
|
|
115
|
-
|
|
176
|
+
/**
|
|
177
|
+
* Extension point for plugins to contribute typed fields to the GraphQL
|
|
178
|
+
* resolver context. Plugins augment this interface via module declaration
|
|
179
|
+
* merging:
|
|
180
|
+
*
|
|
181
|
+
* declare module '@kubun/graphql' {
|
|
182
|
+
* interface PluginContextMap {
|
|
183
|
+
* myPlugin: MyPluginContext
|
|
184
|
+
* }
|
|
185
|
+
* }
|
|
186
|
+
*
|
|
187
|
+
* At runtime, engine merges each plugin's context factory output under its
|
|
188
|
+
* `name` key. Fields are `Partial<>` because plugins are optional at build
|
|
189
|
+
* time — consumers use optional chaining or assert when the plugin is
|
|
190
|
+
* known to be wired.
|
|
191
|
+
*/
|
|
192
|
+
export interface PluginContextMap {
|
|
193
|
+
}
|
|
194
|
+
export type Context = ReadOnlyContext & MutationContext & Partial<PluginContextMap>;
|
package/lib/context.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{defer as e}from"@enkaku/async";import{withSpan as t}from"@enkaku/otel";import{DocumentID as o}from"@kubun/id";import{KubunAttributeKeys as n,KubunSpanNames as r}from"@kubun/protocol";export function createReadContext(l){let{
|
|
1
|
+
import{defer as e}from"@enkaku/async";import{withSpan as t}from"@enkaku/otel";import{DocumentID as o}from"@kubun/id";import{KubunAttributeKeys as n,KubunSpanNames as r}from"@kubun/protocol";export function createReadContext(l){let{store:s,viewerDID:i,accessChecker:a}=l;function u(e,o,n){return null!=l.tracer?t(l.tracer,e,{attributes:o},async()=>n()):n()}function c(t,o){let n=!1,r=null,s=[];if(null==l.events)throw Error("Subscriptions require a document event emitter");let i=l.events.on("document:saved",e=>{if(t(e)){let t=o(e);null==r?s.push(t):(r.resolve(t),r=null)}}),a=()=>(i(),n=!0,Promise.resolve({done:!0,value:void 0}));return{[Symbol.asyncDispose]:()=>(i(),n=!0,Promise.resolve()),[Symbol.asyncIterator](){return this},next:()=>{if(n)return Promise.resolve({done:!0,value:void 0});let t=s.shift();return null!=t?Promise.resolve({value:t,done:!1}):(r=e()).promise.then(e=>({value:e,done:!1}))},return:a,throw:a}}return{getViewer:()=>i,async loadDocument(e){let t=`document:${e}`,i=l.complexity?.resolverTimings!=null?performance.now():0,c=await u(r.DATA_QUERY,{[n.DOCUMENT_ID]:e},async()=>{let t=await s.getDocument(o.fromString(e));return t?a&&!await a(t,"read")?(null!=l.warnings&&l.warnings.push({message:`Access denied for document ${e}`,code:"KB07"}),null):(null!=l.complexity&&(l.complexity.resolverCount++,l.complexity.documentsScanned++),t):null});if(l.complexity?.resolverTimings!=null){let e=Math.round((performance.now()-i)*100)/100,o=l.complexity.resolverTimings.get(t)??0;l.complexity.resolverTimings.set(t,o+e)}return c},searchDocuments:async(e,t,o)=>await s.searchDocuments({query:e,modelIDs:t,first:o}),async resolveConnection(e,t){let o=`connection:${e.join(",")}`,i=l.complexity?.resolverTimings!=null?performance.now():0,c=await u(r.DATA_QUERY,{[n.MODEL_ID]:e.join(",")},async()=>{if(null!=t.search&&""!==t.search.trim()){let o=await s.searchDocuments({query:t.search,modelIDs:e,first:200});if(0===o.length){if(null!=l.complexity)for(let t of(l.complexity.resolverCount++,e))l.complexity.modelsQueried.add(t);return{edges:[],pageInfo:{hasNextPage:!1,hasPreviousPage:!1,startCursor:null,endCursor:null}}}let n=o.map(e=>e.documentID),r=await s.listDocuments({modelIDs:e,docIDs:n}),i=r;if(a)for(let e of(i=[],r))await a(e,"read")?i.push(e):null!=l.warnings&&l.warnings.push({message:`Access denied for document ${e.id}`,code:"KB07"});let u=new Map(o.map(e=>[e.documentID,e.rank]));i.sort((e,t)=>Math.abs(u.get(t.id)??0)-Math.abs(u.get(e.id)??0));let c=i.slice(0,t.first??50);if(null!=l.complexity){for(let t of(l.complexity.resolverCount++,e))l.complexity.modelsQueried.add(t);l.complexity.documentsScanned+=c.length}return{edges:c.map((e,t)=>({cursor:String(t),node:e})),pageInfo:{hasNextPage:i.length>c.length,hasPreviousPage:!1,startCursor:c.length>0?"0":null,endCursor:c.length>0?String(c.length-1):null}}}if(null==a){let{entries:o,hasMore:n}=await s.queryDocuments({...t,modelIDs:e,catalogID:l.catalogID});if(null!=l.complexity){for(let t of(l.complexity.resolverCount++,e))l.complexity.modelsQueried.add(t);l.complexity.documentsScanned+=o.length}return{edges:o.map(e=>({cursor:e.cursor,node:e.document})),pageInfo:{hasNextPage:!!t.first&&n,hasPreviousPage:!t.first&&n,endCursor:o.at(-1)?.cursor??null,startCursor:o.at(0)?.cursor??null}}}let o=[],n=0,r=0;if(null!=t.first){let i=2*(n=t.first),u=t.after;for(;o.length<n&&r<10;){let{entries:c,hasMore:m}=await s.queryDocuments({...t,modelIDs:e,catalogID:l.catalogID,first:i,after:u});if(0===c.length&&!m)break;for(let e of c)if(await a(e.document,"read")){if(o.push(e),o.length>=n)break}else null!=l.warnings&&l.warnings.push({message:`Access denied for document ${e.document.id}`,code:"KB07"});if(!m)break;u=c.at(-1)?.cursor??null,r++}}else if(null!=t.last){let i=2*(n=t.last),u=t.before;for(;o.length<n&&r<10;){let{entries:c,hasMore:m}=await s.queryDocuments({...t,modelIDs:e,catalogID:l.catalogID,last:i,before:u});if(0===c.length&&!m)break;for(let e of[...c].reverse())if(await a(e.document,"read")){if(o.unshift(e),o.length>=n)break}else null!=l.warnings&&l.warnings.push({message:`Access denied for document ${e.document.id}`,code:"KB07"});if(!m)break;u=c.at(0)?.cursor??null,r++}}let i=o.slice(0,n),u=o.length>n;if(null!=l.complexity){for(let t of(l.complexity.resolverCount++,e))l.complexity.modelsQueried.add(t);l.complexity.documentsScanned+=i.length}return{edges:i.map(e=>({cursor:e.cursor,node:e.document})),pageInfo:{hasNextPage:!!t.first&&u,hasPreviousPage:!t.first&&u,endCursor:i.at(-1)?.cursor??null,startCursor:i.at(0)?.cursor??null}}});if(l.complexity?.resolverTimings!=null){let e=Math.round((performance.now()-i)*100)/100,t=l.complexity.resolverTimings.get(o)??0;l.complexity.resolverTimings.set(o,t+e)}return c},async resolveList(e,t){let o=`list:${e.join(",")}`,i=l.complexity?.resolverTimings!=null?performance.now():0,c=await u(r.DATA_QUERY,{[n.MODEL_ID]:e.join(",")},async()=>{let o=await s.listDocuments({modelIDs:e,docIDs:t});if(!a){if(null!=l.complexity){for(let t of(l.complexity.resolverCount++,e))l.complexity.modelsQueried.add(t);l.complexity.documentsScanned+=o.length}return o}let n=[];for(let e of o)await a(e,"read")?n.push(e):null!=l.warnings&&l.warnings.push({message:`Access denied for document ${e.id}`,code:"KB07"});if(null!=l.complexity){for(let t of(l.complexity.resolverCount++,e))l.complexity.modelsQueried.add(t);l.complexity.documentsScanned+=n.length}return n});if(l.complexity?.resolverTimings!=null){let e=Math.round((performance.now()-i)*100)/100,t=l.complexity.resolverTimings.get(o)??0;l.complexity.resolverTimings.set(o,t+e)}return c},async resolveCount(e,t){let o=`count:${e.join(",")}`,i=l.complexity?.resolverTimings!=null?performance.now():0,a=await u(r.DATA_QUERY,{[n.MODEL_ID]:e.join(",")},async()=>(null!=l.complexity&&l.complexity.resolverCount++,await s.countDocuments({modelIDs:e,filter:t,catalogID:l.catalogID})));if(l.complexity?.resolverTimings!=null){let e=Math.round((performance.now()-i)*100)/100,t=l.complexity.resolverTimings.get(o)??0;l.complexity.resolverTimings.set(o,t+e)}return a},subscribeToDocumentCreated(e,t){let o=c(o=>"create"===o.type&&e.includes(o.document.model)&&(null==t||o.document.owner===t),e=>e.document);return a?async function*(){for await(let e of o)await a(e,"read")&&(yield e)}():o},subscribeToDocumentChanged(e){let t=c(t=>"update"===t.type&&t.document.id===e&&null!==t.document.data,e=>e.document);return a?async function*(){for await(let e of t)await a(e,"read")&&(yield e)}():t},subscribeToDocumentRemoved:()=>c(e=>"update"===e.type&&null===e.document.data,e=>e.document.id),subscribeToDocumentEdgeAdded(e,t){let o=c(o=>null!==o.document.data&&e.includes(o.document.model)&&t(o.document.data)&&("create"===o.type||null!==o.previous.data&&t(o.previous.data)),e=>({cursor:e.getCursor(),node:e.document}));return a?async function*(){for await(let e of o)await a(e.node,"read")&&(yield e)}():o},subscribeToDocumentEdgeRemoved:(e,t)=>c(o=>"update"===o.type&&e.includes(o.document.model)&&null!==o.previous.data&&t(o.previous.data)&&(null===o.document.data||!t(o.document.data)),e=>e.document.id)}}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { AccessWarning, ComplexityStats, Context, CreateMutationParams, MutationContext, QueryContext, ReadContextParams, ReadOnlyContext, RemoveMutationParams, SetDocumentAccessOverrideParams, SetModelAccessDefaultsParams, SetMutationParams, SubscriptionContext, UpdateMutationParams, } from './context.js';
|
|
1
|
+
export type { AccessWarning, ComplexityStats, Context, CreateMutationParams, DocumentEventEmitter, DocumentSavedEvent, MutationContext, PluginContextMap, QueryContext, ReadContextDB, ReadContextParams, ReadOnlyContext, RemoveMutationParams, SetDocumentAccessOverrideParams, SetModelAccessDefaultsParams, SetMutationParams, SubscriptionContext, UpdateMutationParams, } from './context.js';
|
|
2
2
|
export { createReadContext } from './context.js';
|
|
3
3
|
export { GraphQLAttachmentID, GraphQLDocID } from './identifiers.js';
|
|
4
4
|
export type { ExtensionResolvers, SchemaBuilderParams, SharedDefinitions, } from './schema.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubun/graphql",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.2",
|
|
4
4
|
"license": "see LICENSE.md",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"type": "module",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
],
|
|
16
16
|
"sideEffects": false,
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@enkaku/async": "^0.14.
|
|
18
|
+
"@enkaku/async": "^0.14.1",
|
|
19
19
|
"@enkaku/codec": "^0.14.0",
|
|
20
20
|
"@enkaku/event": "^0.14.1",
|
|
21
21
|
"@enkaku/otel": "^0.14.0",
|
|
@@ -26,14 +26,13 @@
|
|
|
26
26
|
"multiformats": "^13.4.2",
|
|
27
27
|
"@kubun/hlc": "^0.8.0",
|
|
28
28
|
"@kubun/id": "^0.8.0",
|
|
29
|
-
"@kubun/protocol": "^0.8.
|
|
29
|
+
"@kubun/protocol": "^0.8.1"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@enkaku/capability": "^0.14.0",
|
|
33
33
|
"@enkaku/token": "0.14.1",
|
|
34
|
-
"@kubun/
|
|
35
|
-
"@kubun/db-better-sqlite": "^0.8.
|
|
36
|
-
"@kubun/test-utils": "^0.8.0"
|
|
34
|
+
"@kubun/test-utils": "^0.8.0",
|
|
35
|
+
"@kubun/db-better-sqlite": "^0.8.1"
|
|
37
36
|
},
|
|
38
37
|
"scripts": {
|
|
39
38
|
"build:clean": "del lib",
|