@kubun/client 0.7.0 → 0.8.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/lib/client.d.ts +12 -7
- package/lib/client.js +1 -1
- package/lib/graphql.d.ts +2 -2
- package/lib/graphql.js +1 -1
- package/lib/index.d.ts +1 -2
- package/package.json +11 -9
package/lib/client.d.ts
CHANGED
|
@@ -1,46 +1,50 @@
|
|
|
1
|
-
import { Client, type RequestCall
|
|
1
|
+
import { Client, type RequestCall } from '@enkaku/client';
|
|
2
|
+
import { type Runtime } from '@enkaku/runtime';
|
|
2
3
|
import type { Identity, SigningIdentity } from '@enkaku/token';
|
|
3
4
|
import { type Logger } from '@kubun/logger';
|
|
4
|
-
import type { BeginTransactionParams, BeginTransactionResult, ClientTransport, DeployGraphParams, DeployGraphResult, ExecuteGraphParams, ExecuteGraphResult, ListGraphResult, LoadGraphParams, LoadGraphResult, Protocol, TransactionParams, TransactionResult } from '@kubun/protocol';
|
|
5
|
-
import { type GetRandomValues } from './graphql.js';
|
|
5
|
+
import type { BeginTransactionParams, BeginTransactionResult, ClientTransport, DeployGraphParams, DeployGraphResult, ExecuteGraphParams, ExecuteGraphResult, GraphQLSource, GraphQLSourceParams, ListGraphResult, LoadGraphParams, LoadGraphResult, Protocol, TransactionParams, TransactionResult } from '@kubun/protocol';
|
|
6
6
|
export type MutateGraphParams = ExecuteGraphParams & {
|
|
7
7
|
attachments?: Record<string, string>;
|
|
8
8
|
transactionID?: string;
|
|
9
9
|
};
|
|
10
|
+
export type GraphSubscription<Data extends Record<string, unknown> = Record<string, unknown>> = RequestCall<AsyncGenerator<ExecuteGraphResult<Data>>>;
|
|
10
11
|
export type KubunClientType = {
|
|
11
12
|
deployGraph: (params: DeployGraphParams) => RequestCall<DeployGraphResult>;
|
|
12
13
|
listGraphs: () => RequestCall<ListGraphResult>;
|
|
13
14
|
loadGraph: (params: LoadGraphParams) => RequestCall<LoadGraphResult>;
|
|
14
15
|
queryGraph: <Data extends Record<string, unknown> = Record<string, unknown>>(params: ExecuteGraphParams, signal?: AbortSignal) => RequestCall<ExecuteGraphResult<Data>>;
|
|
15
|
-
subscribeToGraph<Data extends Record<string, unknown> = Record<string, unknown>>(params: ExecuteGraphParams, signal?: AbortSignal):
|
|
16
|
+
subscribeToGraph<Data extends Record<string, unknown> = Record<string, unknown>>(params: ExecuteGraphParams, signal?: AbortSignal): GraphSubscription<Data>;
|
|
16
17
|
mutateGraph: <Data extends Record<string, unknown> = Record<string, unknown>>(params: MutateGraphParams, signal?: AbortSignal) => RequestCall<ExecuteGraphResult<Data>>;
|
|
17
18
|
beginTransaction: (params: BeginTransactionParams) => RequestCall<BeginTransactionResult>;
|
|
18
19
|
commitTransaction: (params: TransactionParams) => RequestCall<TransactionResult>;
|
|
19
20
|
rollbackTransaction: (params: TransactionParams) => RequestCall<TransactionResult>;
|
|
20
21
|
};
|
|
21
22
|
export type ReadClientParams = {
|
|
22
|
-
getRandomID?: () => string;
|
|
23
23
|
identity?: Identity | Promise<Identity>;
|
|
24
24
|
logger?: Logger;
|
|
25
|
+
runtime?: Partial<Runtime>;
|
|
25
26
|
serverID?: string;
|
|
26
27
|
transport: ClientTransport;
|
|
27
28
|
};
|
|
28
29
|
export type ClientParams = Omit<ReadClientParams, 'identity'> & {
|
|
29
30
|
identity: SigningIdentity | Promise<SigningIdentity>;
|
|
30
|
-
getRandomValues?: GetRandomValues;
|
|
31
31
|
};
|
|
32
32
|
export declare class KubunReadClient {
|
|
33
33
|
#private;
|
|
34
34
|
constructor(params: ReadClientParams);
|
|
35
35
|
/** @internal */
|
|
36
36
|
get client(): Client<Protocol>;
|
|
37
|
+
/** @internal */
|
|
38
|
+
protected get logger(): Logger;
|
|
37
39
|
listGraphs(signal?: AbortSignal): RequestCall<ListGraphResult>;
|
|
38
40
|
loadGraph(param: LoadGraphParams, signal?: AbortSignal): RequestCall<LoadGraphResult>;
|
|
39
41
|
queryGraph<Data extends Record<string, unknown> = Record<string, unknown>>(param: ExecuteGraphParams, signal?: AbortSignal): RequestCall<ExecuteGraphResult<Data>>;
|
|
40
|
-
subscribeToGraph<Data extends Record<string, unknown> = Record<string, unknown>>(param: ExecuteGraphParams, signal?: AbortSignal):
|
|
42
|
+
subscribeToGraph<Data extends Record<string, unknown> = Record<string, unknown>>(param: ExecuteGraphParams, signal?: AbortSignal): GraphSubscription<Data>;
|
|
43
|
+
getGraphQLSource(params: GraphQLSourceParams): GraphQLSource;
|
|
41
44
|
}
|
|
42
45
|
export declare class KubunLightClient extends KubunReadClient implements KubunClientType {
|
|
43
46
|
deployGraph(param: DeployGraphParams, signal?: AbortSignal): RequestCall<DeployGraphResult>;
|
|
47
|
+
getGraphQLSource(params: GraphQLSourceParams): GraphQLSource;
|
|
44
48
|
mutateGraph<Data extends Record<string, unknown> = Record<string, unknown>>(param: MutateGraphParams, signal?: AbortSignal): RequestCall<ExecuteGraphResult<Data>>;
|
|
45
49
|
beginTransaction(param: BeginTransactionParams): RequestCall<BeginTransactionResult>;
|
|
46
50
|
commitTransaction(param: TransactionParams): RequestCall<TransactionResult>;
|
|
@@ -49,6 +53,7 @@ export declare class KubunLightClient extends KubunReadClient implements KubunCl
|
|
|
49
53
|
export declare class KubunClient extends KubunReadClient implements KubunClientType {
|
|
50
54
|
#private;
|
|
51
55
|
constructor(params: ClientParams);
|
|
56
|
+
getGraphQLSource(params: GraphQLSourceParams): GraphQLSource;
|
|
52
57
|
deployGraph(param: DeployGraphParams, signal?: AbortSignal): RequestCall<DeployGraphResult>;
|
|
53
58
|
loadGraph(param: LoadGraphParams, signal?: AbortSignal): RequestCall<LoadGraphResult>;
|
|
54
59
|
mutateGraph<Data extends Record<string, unknown> = Record<string, unknown>>(param: MutateGraphParams, signal?: AbortSignal): RequestCall<ExecuteGraphResult<Data>>;
|
package/lib/client.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Client as
|
|
1
|
+
import{Client as e,RequestError as r}from"@enkaku/client";import{createRuntime as t}from"@enkaku/runtime";import{HLC as a}from"@kubun/hlc";import{getKubunLogger as i}from"@kubun/logger";import{GraphQLExecutionError as s,MutationsRunner as n}from"./graphql.js";export class KubunReadClient{#e;#r;constructor(r){let a=t(r.runtime),s=r.logger??i("client",{clientID:a.getRandomID()});this.#e=new e({...r,logger:s,runtime:a}),this.#r=s}get client(){return this.#e}get logger(){return this.#r}listGraphs(e){return this.#e.request("graph/list",{signal:e})}loadGraph(e,r){return this.#e.request("graph/load",{param:e,signal:r})}queryGraph(e,r){this.#r.debug("queryGraph graph={id} query={text}",{id:e.id,text:e.text.slice(0,200)});let t=performance.now(),a=this.#e.request("graph/query",{param:e,signal:r});return Object.assign(a.then(e=>{let r=Math.round((performance.now()-t)*100)/100;return this.#r.debug("queryGraph completed duration={duration}ms errors={hasErrors}",{duration:String(r),hasErrors:String(null!=e.errors&&e.errors.length>0)}),e},e=>{throw this.#r.error("queryGraph failed: {error}",{error:String(e)}),e}),{abort:a.abort,id:a.id,procedure:a.procedure,type:a.type,signal:a.signal})}subscribeToGraph(e,r){this.#r.debug("subscribeToGraph graph={id} query={text}",{id:e.id,text:e.text.slice(0,200)});let t=this.#e.createStream("graph/subscribe",{param:e,signal:r});return t.catch(()=>{}),Object.assign(Promise.resolve(async function*(){let e=t.readable.getReader();try{for(;;){let{done:r,value:t}=await e.read();if(r)break;yield t}}finally{e.releaseLock()}}()),{id:t.id,type:t.type,procedure:t.procedure,abort:()=>t.close(),signal:t.signal})}getGraphQLSource(e){let{graphID:r}=e;return{query:async e=>await this.queryGraph({id:r,text:e.text,variables:e.variables}),mutate:async()=>{throw Error("KubunReadClient does not support mutations")},subscribe:async e=>await this.subscribeToGraph({id:r,text:e.text,variables:e.variables})}}}export class KubunLightClient extends KubunReadClient{deployGraph(e,r){return this.client.request("graph/deploy",{param:e,signal:r})}getGraphQLSource(e){let{graphID:r}=e;return{query:async e=>await this.queryGraph({id:r,text:e.text,variables:e.variables}),mutate:async e=>await this.mutateGraph({id:r,text:e.text,variables:e.variables}),subscribe:async e=>await this.subscribeToGraph({id:r,text:e.text,variables:e.variables})}}mutateGraph(e,t){this.logger.debug("mutateGraph graph={id} query={text}",{id:e.id,text:e.text.slice(0,200)});let a=performance.now(),i=this.client.request("graph/mutate",{param:{...e,mutations:null},signal:t});return Object.assign(i.then(e=>{let r=Math.round((performance.now()-a)*100)/100;return this.logger.debug("mutateGraph completed duration={duration}ms errors={hasErrors}",{duration:String(r),hasErrors:String(null!=e.errors&&e.errors.length>0)}),e},e=>{throw e instanceof r?this.logger.error("mutateGraph failed: code={code} message={message} data={data}",{code:e.code,message:e.message,data:e.data}):this.logger.error("mutateGraph failed: {error}",{error:String(e)}),e}),{abort:i.abort,id:i.id,procedure:i.procedure,type:i.type,signal:i.signal})}beginTransaction(e){return this.client.request("graph/beginTransaction",{param:e})}commitTransaction(e){return this.client.request("graph/commitTransaction",{param:e})}rollbackTransaction(e){return this.client.request("graph/rollbackTransaction",{param:e})}}export class KubunClient extends KubunReadClient{#t;#a;#i;constructor(e){super(e),this.#i=t(e.runtime),this.#t=Promise.resolve(e.identity),this.#a=new n}getGraphQLSource(e){let{graphID:r}=e;return{query:async e=>await this.queryGraph({id:r,text:e.text,variables:e.variables}),mutate:async e=>await this.mutateGraph({id:r,text:e.text,variables:e.variables}),subscribe:async e=>await this.subscribeToGraph({id:r,text:e.text,variables:e.variables})}}deployGraph(e,r){let t=this.client.request("graph/deploy",{param:e,signal:r});return Object.assign(t.then(e=>(this.#a.setSchema(e.id,e),e)),{abort:t.abort,id:t.id,procedure:t.procedure,type:t.type,signal:t.signal})}loadGraph(e,r){let t=super.loadGraph(e,r);return Object.assign(t.then(r=>(this.#a.setSchema(e.id,r),r)),{abort:t.abort,id:t.id,procedure:t.procedure,type:t.type,signal:t.signal})}async #s(e,t,i){this.logger.debug("mutateGraph graph={id} query={text}",{id:t.id,text:t.text.slice(0,200)});let n=performance.now();try{let{id:r,...o}=t;if(this.#a.hasSchema(r)||await this.loadGraph({id:r},i),i.aborted)throw i.reason??new DOMException("The operation was aborted.","AbortError");let u=await this.#t,l={getRandomValues:this.#i.getRandomValues,hlc:new a({nodeID:u.id}),identity:u,mutations:{}},{errors:h}=await this.#a.execute({context:l,schemaID:r,source:t.text,variables:t.variables});if(null!=h&&0!==h.length)throw this.logger.error("mutateGraph pre-execution failed: {errorCount} error(s): {details}",{errorCount:String(h.length),details:h.map(e=>`${e.message} (path: ${e.path?.join(".")??"root"})`).join("; ")}),new s(h);let c=await this.client.request("graph/mutate",{id:e,param:{id:r,mutations:l.mutations,...o},signal:i}),g=Math.round((performance.now()-n)*100)/100;return this.logger.debug("mutateGraph completed duration={duration}ms errors={hasErrors}",{duration:String(g),hasErrors:String(null!=c.errors&&c.errors.length>0)}),c}catch(e){throw e instanceof r?this.logger.error("mutateGraph failed: code={code} message={message} data={data}",{code:e.code,message:e.message,data:e.data}):this.logger.error("mutateGraph failed: {error}",{error:String(e)}),e}}mutateGraph(e,r){let t=this.#i.getRandomID(),a=new AbortController,i=r?AbortSignal.any([a.signal,r]):a.signal;return Object.assign(this.#s(t,e,i),{id:t,abort:()=>a.abort(),signal:i})}beginTransaction(e){return this.client.request("graph/beginTransaction",{param:e})}commitTransaction(e){return this.client.request("graph/commitTransaction",{param:e})}rollbackTransaction(e){return this.client.request("graph/rollbackTransaction",{param:e})}}
|
package/lib/graphql.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import type { GetRandomValues } from '@enkaku/runtime';
|
|
1
2
|
import { type SigningIdentity } from '@enkaku/token';
|
|
2
3
|
import type { Context } from '@kubun/graphql';
|
|
3
|
-
import {
|
|
4
|
+
import type { HLC } from '@kubun/hlc';
|
|
4
5
|
import type { GraphModelJSON } from '@kubun/protocol';
|
|
5
6
|
import type { ExecutionResult, GraphQLError, GraphQLSchema } from 'graphql';
|
|
6
|
-
export type GetRandomValues = <T extends ArrayBufferView>(array: T) => T;
|
|
7
7
|
export declare class GraphQLExecutionError extends Error {
|
|
8
8
|
#private;
|
|
9
9
|
constructor(errors: ReadonlyArray<GraphQLError>, message?: string);
|
package/lib/graphql.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{stringifyToken as e}from"@enkaku/token";import{createSchema as t}from"@kubun/graphql";import{convertPatchInput as
|
|
1
|
+
import{stringifyToken as e}from"@enkaku/token";import{createSchema as t}from"@kubun/graphql";import{convertPatchInput as n,createMutationOperations as a}from"@kubun/mutation";import{graphql as o}from"graphql";import{signChangeMutation as s,signSetMutation as r}from"./mutations.js";export class GraphQLExecutionError extends Error{#e;constructor(e,t="GraphQL execution failed"){super(t),this.name=GraphQLExecutionError.name,this.#e=e}get errors(){return this.#e}}export function createContext(t){let o=a({issuer:t.identity.id,hlc:t.hlc,getRandomValues:t.getRandomValues,processSetMutation:async n=>e(await r({identity:t.identity,mutation:n})),processChangeMutation:async n=>e(await s({identity:t.identity,mutation:n}))});return{getViewer:()=>null,loadDocument:async()=>null,resolveConnection:async()=>({edges:[],pageInfo:{hasNextPage:!1,hasPreviousPage:!1}}),resolveList:async()=>[],resolveCount:async()=>0,searchDocuments:async()=>[],subscribeToDocumentCreated:()=>null,subscribeToDocumentChanged:()=>null,subscribeToDocumentRemoved:()=>null,subscribeToDocumentEdgeAdded:()=>null,subscribeToDocumentEdgeRemoved:()=>null,executeCreateMutation:async({modelID:e,data:n,info:a})=>(t.mutations[a.path.key]=await o.createDocument({modelID:e,data:n}),null),executeSetMutation:async({modelID:e,unique:n,data:a,info:s})=>(t.mutations[s.path.key]=await o.setDocument({modelID:e,unique:n,data:a}),null),executeUpdateMutation:async({input:e,info:a})=>(t.mutations[a.path.key]=await o.updateDocument({docID:e.id,patch:n(e.patch)}),null),async executeRemoveMutation({id:e,info:n}){t.mutations[n.path.key]=await o.removeDocument({docID:e})},executeSetModelAccessDefaults:async()=>null,async executeRemoveModelAccessDefaults(){},executeSetDocumentAccessOverride:async()=>null,async executeRemoveDocumentAccessOverride(){},beginTransaction(){throw Error("Transaction mutations are not supported in client mode")},async commitTransaction(){throw Error("Transaction mutations are not supported in client mode")},rollbackTransaction(){throw Error("Transaction mutations are not supported in client mode")}}}export class MutationsRunner{#t;constructor(e={}){this.#t=e.schemas??{}}hasSchema(e){return null!=this.#t[e]}setSchema(e,n){this.#t[e]=Promise.resolve(n).then(e=>t({...e,stubExtensionResolvers:!0}))}async execute(e){let t=this.#t[e.schemaID];if(null==t)throw Error(`Schema ${e.schemaID} not found`);return await o({contextValue:createContext(e.context),schema:await t,source:e.source,variableValues:e.variables})}}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
1
|
export { type Attachment, createAttachment, toAttachmentsRecord } from './attachments.js';
|
|
2
|
-
export { type ClientParams, KubunClient, type KubunClientType, KubunLightClient, KubunReadClient, type MutateGraphParams, type ReadClientParams, } from './client.js';
|
|
3
|
-
export type { GetRandomValues } from './graphql.js';
|
|
2
|
+
export { type ClientParams, type GraphSubscription, KubunClient, type KubunClientType, KubunLightClient, KubunReadClient, type MutateGraphParams, type ReadClientParams, } from './client.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubun/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"license": "see LICENSE.md",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"type": "module",
|
|
@@ -15,18 +15,20 @@
|
|
|
15
15
|
],
|
|
16
16
|
"sideEffects": false,
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@enkaku/
|
|
18
|
+
"@enkaku/runtime": "^0.14.0",
|
|
19
|
+
"@enkaku/client": "^0.14.3",
|
|
19
20
|
"@enkaku/codec": "^0.14.0",
|
|
20
|
-
"@enkaku/token": "0.14.
|
|
21
|
-
"graphql": "^16.13.
|
|
21
|
+
"@enkaku/token": "0.14.1",
|
|
22
|
+
"graphql": "^16.13.2",
|
|
22
23
|
"multiformats": "^13.4.2",
|
|
23
|
-
"@kubun/
|
|
24
|
-
"@kubun/
|
|
25
|
-
"@kubun/
|
|
24
|
+
"@kubun/hlc": "^0.8.0",
|
|
25
|
+
"@kubun/logger": "^0.8.0",
|
|
26
|
+
"@kubun/id": "^0.8.0",
|
|
27
|
+
"@kubun/mutation": "^0.8.0"
|
|
26
28
|
},
|
|
27
29
|
"devDependencies": {
|
|
28
|
-
"@kubun/
|
|
29
|
-
"@kubun/
|
|
30
|
+
"@kubun/graphql": "^0.8.0",
|
|
31
|
+
"@kubun/protocol": "^0.8.0"
|
|
30
32
|
},
|
|
31
33
|
"scripts": {
|
|
32
34
|
"build:clean": "del lib",
|