@kubun/client 0.2.3 → 0.3.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 +5 -3
- package/lib/client.d.ts.map +1 -1
- package/lib/client.js +16 -13
- package/lib/graphql.d.ts +27 -5
- package/lib/graphql.d.ts.map +1 -1
- package/lib/graphql.js +95 -125
- package/lib/index.d.ts +0 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +0 -1
- package/lib/mutations.d.ts +6 -25
- package/lib/mutations.d.ts.map +1 -1
- package/lib/mutations.js +9 -0
- package/package.json +8 -9
- package/lib/errors.d.ts +0 -7
- package/lib/errors.d.ts.map +0 -1
- package/lib/errors.js +0 -11
- package/lib/mutations-runner.d.ts +0 -21
- package/lib/mutations-runner.d.ts.map +0 -1
- package/lib/mutations-runner.js +0 -25
package/lib/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Client, type RequestCall } from '@enkaku/client';
|
|
1
|
+
import { Client, type RequestCall, type StreamCall } from '@enkaku/client';
|
|
2
2
|
import type { TokenSigner } from '@enkaku/token';
|
|
3
3
|
import type { ClientTransport, DeployGraphParams, DeployGraphResult, ExecuteGraphParams, ExecuteGraphResult, ListGraphResult, LoadGraphParams, LoadGraphResult, Protocol } from '@kubun/protocol';
|
|
4
4
|
export type MutateGraphParams = ExecuteGraphParams & {
|
|
@@ -8,8 +8,9 @@ export type KubunClientType = {
|
|
|
8
8
|
deployGraph: (params: DeployGraphParams) => RequestCall<DeployGraphResult>;
|
|
9
9
|
listGraphs: () => RequestCall<ListGraphResult>;
|
|
10
10
|
loadGraph: (params: LoadGraphParams) => RequestCall<LoadGraphResult>;
|
|
11
|
-
queryGraph: <Data extends Record<string, unknown> = Record<string, unknown>>(params: ExecuteGraphParams) => RequestCall<ExecuteGraphResult<Data>>;
|
|
12
|
-
|
|
11
|
+
queryGraph: <Data extends Record<string, unknown> = Record<string, unknown>>(params: ExecuteGraphParams, signal?: AbortSignal) => RequestCall<ExecuteGraphResult<Data>>;
|
|
12
|
+
subscribeToGraph<Data extends Record<string, unknown> = Record<string, unknown>>(params: ExecuteGraphParams, signal?: AbortSignal): StreamCall<ExecuteGraphResult<Data>, ExecuteGraphResult<Data>>;
|
|
13
|
+
mutateGraph: (params: MutateGraphParams, signal?: AbortSignal) => RequestCall<ExecuteGraphResult>;
|
|
13
14
|
};
|
|
14
15
|
export type ReadClientParams = {
|
|
15
16
|
getRandomID?: () => string;
|
|
@@ -28,6 +29,7 @@ export declare class KubunReadClient {
|
|
|
28
29
|
listGraphs(signal?: AbortSignal): RequestCall<ListGraphResult>;
|
|
29
30
|
loadGraph(param: LoadGraphParams, signal?: AbortSignal): RequestCall<LoadGraphResult>;
|
|
30
31
|
queryGraph<Data extends Record<string, unknown> = Record<string, unknown>>(param: ExecuteGraphParams, signal?: AbortSignal): RequestCall<ExecuteGraphResult<Data>>;
|
|
32
|
+
subscribeToGraph<Data extends Record<string, unknown> = Record<string, unknown>>(param: ExecuteGraphParams, signal?: AbortSignal): StreamCall<ExecuteGraphResult<Data>, ExecuteGraphResult<Data>>;
|
|
31
33
|
}
|
|
32
34
|
export declare class KubunLightClient extends KubunReadClient implements KubunClientType {
|
|
33
35
|
deployGraph(param: DeployGraphParams, signal?: AbortSignal): RequestCall<DeployGraphResult>;
|
package/lib/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,WAAW,EAAE,MAAM,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,WAAW,EAAE,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAE1E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,e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AAI,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACxE,KAAK,EAAE,iBAAiB,EACxB,MAAM,CAAC,EAAE,WAAW,GACnB,WAAW,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC;CAMzC;AAED,qBAAa,WAAY,SAAQ,eAAgB,YAAW,eAAe;;gBAI7D,MAAM,EAAE,YAAY;IAMhC,WAAW,CAAC,KAAK,EAAE,iBAAiB,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,WAAW,CAAC,iBAAiB,CAAC;IAS3F,SAAS,CAAC,KAAK,EAAE,eAAe,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,WAAW,CAAC,eAAe,CAAC;IAoDrF,WAAW,CAAC,IAAI,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACxE,KAAK,EAAE,iBAAiB,EACxB,MAAM,CAAC,EAAE,WAAW,GACnB,WAAW,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC;CAWzC"}
|
package/lib/client.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { Client } from '@enkaku/client';
|
|
2
2
|
import { fromB64 } from '@enkaku/codec';
|
|
3
|
-
import { GraphQLExecutionError } from './
|
|
4
|
-
import { ClientSchemaBuilder } from './graphql.js';
|
|
5
|
-
import { MutationsRunner } from './mutations-runner.js';
|
|
3
|
+
import { GraphQLExecutionError, MutationsRunner } from './graphql.js';
|
|
6
4
|
export class KubunReadClient {
|
|
7
5
|
#client;
|
|
8
6
|
constructor(params){
|
|
@@ -28,6 +26,12 @@ export class KubunReadClient {
|
|
|
28
26
|
signal
|
|
29
27
|
});
|
|
30
28
|
}
|
|
29
|
+
subscribeToGraph(param, signal) {
|
|
30
|
+
return this.#client.createStream('graph/subscribe', {
|
|
31
|
+
param,
|
|
32
|
+
signal
|
|
33
|
+
});
|
|
34
|
+
}
|
|
31
35
|
}
|
|
32
36
|
export class KubunLightClient extends KubunReadClient {
|
|
33
37
|
deployGraph(param, signal) {
|
|
@@ -60,14 +64,12 @@ export class KubunClient extends KubunReadClient {
|
|
|
60
64
|
signal
|
|
61
65
|
});
|
|
62
66
|
const call = request.then((result)=>{
|
|
63
|
-
|
|
64
|
-
record: result.models
|
|
65
|
-
});
|
|
66
|
-
this.#mutationsRunner.setSchema(result.id, builder.build());
|
|
67
|
+
this.#mutationsRunner.setSchema(result.id, result.models);
|
|
67
68
|
return result;
|
|
68
69
|
});
|
|
69
70
|
return Object.assign(call, {
|
|
70
71
|
abort: request.abort,
|
|
72
|
+
id: request.id,
|
|
71
73
|
signal: request.signal
|
|
72
74
|
});
|
|
73
75
|
}
|
|
@@ -75,19 +77,17 @@ export class KubunClient extends KubunReadClient {
|
|
|
75
77
|
const request = super.loadGraph(param, signal);
|
|
76
78
|
const call = request.then((result)=>{
|
|
77
79
|
if (result.models != null) {
|
|
78
|
-
|
|
79
|
-
record: result.models
|
|
80
|
-
});
|
|
81
|
-
this.#mutationsRunner.setSchema(param.id, builder.build());
|
|
80
|
+
this.#mutationsRunner.setSchema(param.id, result.models);
|
|
82
81
|
}
|
|
83
82
|
return result;
|
|
84
83
|
});
|
|
85
84
|
return Object.assign(call, {
|
|
86
85
|
abort: request.abort,
|
|
86
|
+
id: request.id,
|
|
87
87
|
signal: request.signal
|
|
88
88
|
});
|
|
89
89
|
}
|
|
90
|
-
async #mutateGraph(param, signal) {
|
|
90
|
+
async #mutateGraph(requestID, param, signal) {
|
|
91
91
|
const { id, ...rest } = param;
|
|
92
92
|
if (!this.#mutationsRunner.hasSchema(id)) {
|
|
93
93
|
await this.loadGraph({
|
|
@@ -121,6 +121,7 @@ export class KubunClient extends KubunReadClient {
|
|
|
121
121
|
throw new GraphQLExecutionError(errors);
|
|
122
122
|
}
|
|
123
123
|
return await this.client.request('graph/mutate', {
|
|
124
|
+
id: requestID,
|
|
124
125
|
param: {
|
|
125
126
|
id,
|
|
126
127
|
mutations: context.mutations,
|
|
@@ -130,13 +131,15 @@ export class KubunClient extends KubunReadClient {
|
|
|
130
131
|
});
|
|
131
132
|
}
|
|
132
133
|
mutateGraph(param, signal) {
|
|
134
|
+
const id = globalThis.crypto.randomUUID();
|
|
133
135
|
const controller = new AbortController();
|
|
134
136
|
const anySignal = signal ? AbortSignal.any([
|
|
135
137
|
controller.signal,
|
|
136
138
|
signal
|
|
137
139
|
]) : controller.signal;
|
|
138
|
-
const promise = this.#mutateGraph(param, anySignal);
|
|
140
|
+
const promise = this.#mutateGraph(id, param, anySignal);
|
|
139
141
|
return Object.assign(promise, {
|
|
142
|
+
id,
|
|
140
143
|
abort: ()=>controller.abort(),
|
|
141
144
|
signal: anySignal
|
|
142
145
|
});
|
package/lib/graphql.d.ts
CHANGED
|
@@ -1,12 +1,34 @@
|
|
|
1
1
|
import { type TokenSigner } from '@enkaku/token';
|
|
2
|
-
import
|
|
3
|
-
|
|
2
|
+
import type { ContextType } from '@kubun/graphql';
|
|
3
|
+
import type { DocumentModelsRecord } from '@kubun/protocol';
|
|
4
|
+
import type { ExecutionResult, GraphQLError, GraphQLSchema } from 'graphql';
|
|
5
|
+
export declare class GraphQLExecutionError extends Error {
|
|
6
|
+
#private;
|
|
7
|
+
constructor(errors: ReadonlyArray<GraphQLError>, message?: string);
|
|
8
|
+
get errors(): ReadonlyArray<GraphQLError>;
|
|
9
|
+
}
|
|
10
|
+
export type ExecutionContext = {
|
|
4
11
|
loadState: (id: string) => Promise<Uint8Array | null>;
|
|
5
12
|
mutations: Record<string, string>;
|
|
6
13
|
signer: TokenSigner;
|
|
7
14
|
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
15
|
+
export declare function createContext(client: ExecutionContext): ContextType;
|
|
16
|
+
type SchemasRecord = Record<string, GraphQLSchema | Promise<GraphQLSchema>>;
|
|
17
|
+
type MutationRunnerParams = {
|
|
18
|
+
schemas?: SchemasRecord;
|
|
19
|
+
};
|
|
20
|
+
type ExecuteMutationParams = {
|
|
21
|
+
context: ExecutionContext;
|
|
22
|
+
schemaID: string;
|
|
23
|
+
source: string;
|
|
24
|
+
variables?: Record<string, unknown>;
|
|
25
|
+
};
|
|
26
|
+
export declare class MutationsRunner {
|
|
27
|
+
#private;
|
|
28
|
+
constructor(params?: MutationRunnerParams);
|
|
29
|
+
hasSchema(id: string): boolean;
|
|
30
|
+
setSchema(id: string, recordOrPromise: DocumentModelsRecord | Promise<DocumentModelsRecord>): void;
|
|
31
|
+
execute(params: ExecuteMutationParams): Promise<ExecutionResult>;
|
|
11
32
|
}
|
|
33
|
+
export {};
|
|
12
34
|
//# sourceMappingURL=graphql.d.ts.map
|
package/lib/graphql.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graphql.d.ts","sourceRoot":"","sources":["../src/graphql.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAkB,MAAM,eAAe,CAAA;AAChE,OAAO,
|
|
1
|
+
{"version":3,"file":"graphql.d.ts","sourceRoot":"","sources":["../src/graphql.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAkB,MAAM,eAAe,CAAA;AAChE,OAAO,KAAK,EACV,WAAW,EAKZ,MAAM,gBAAgB,CAAA;AAEvB,OAAO,KAAK,EAAgB,oBAAoB,EAAE,MAAM,iBAAiB,CAAA;AACzE,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAsB,aAAa,EAAE,MAAM,SAAS,CAAA;AAK/F,qBAAa,qBAAsB,SAAQ,KAAK;;gBAGlC,MAAM,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,OAAO,SAA6B;IAMrF,IAAI,MAAM,IAAI,aAAa,CAAC,YAAY,CAAC,CAExC;CACF;AAID,MAAM,MAAM,gBAAgB,GAAG;IAC7B,SAAS,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAAA;IACrD,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACjC,MAAM,EAAE,WAAW,CAAA;CACpB,CAAA;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,gBAAgB,GAAG,WAAW,CAwDnE;AAED,KAAK,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC,CAAA;AAE3E,KAAK,oBAAoB,GAAG;IAC1B,OAAO,CAAC,EAAE,aAAa,CAAA;CACxB,CAAA;AAED,KAAK,qBAAqB,GAAG;IAC3B,OAAO,EAAE,gBAAgB,CAAA;IACzB,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;IACd,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACpC,CAAA;AAED,qBAAa,eAAe;;gBAGd,MAAM,GAAE,oBAAyB;IAI7C,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAI9B,SAAS,CACP,EAAE,EAAE,MAAM,EACV,eAAe,EAAE,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,CAAC,GACpE,IAAI;IAID,OAAO,CAAC,MAAM,EAAE,qBAAqB,GAAG,OAAO,CAAC,eAAe,CAAC;CAavE"}
|
package/lib/graphql.js
CHANGED
|
@@ -1,131 +1,101 @@
|
|
|
1
1
|
import { stringifyToken } from '@enkaku/token';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
import { createSchema } from '@kubun/graphql';
|
|
3
|
+
import { graphql } from 'graphql';
|
|
4
|
+
import { createChangeMutation, createRemoveMutation, createSetMutation } from './mutations.js';
|
|
5
|
+
export class GraphQLExecutionError extends Error {
|
|
6
|
+
#errors;
|
|
7
|
+
constructor(errors, message = 'GraphQL execution failed'){
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = GraphQLExecutionError.name;
|
|
10
|
+
this.#errors = errors;
|
|
11
|
+
}
|
|
12
|
+
get errors() {
|
|
13
|
+
return this.#errors;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const noDocument = null;
|
|
17
|
+
export function createContext(client) {
|
|
18
|
+
const context = {
|
|
19
|
+
async executeCreateMutation (modelID, data, info) {
|
|
20
|
+
// Create the mutation token
|
|
21
|
+
const unique = globalThis.crypto.getRandomValues(new Uint8Array(12));
|
|
22
|
+
const mutation = await createSetMutation({
|
|
23
|
+
signer: client.signer,
|
|
24
|
+
modelID,
|
|
25
|
+
data,
|
|
26
|
+
unique
|
|
27
|
+
});
|
|
28
|
+
// Add the signed mutation to context so it can be retrieved by the client
|
|
29
|
+
client.mutations[info.path.key] = stringifyToken(mutation);
|
|
30
|
+
// Need to return the mutation result for execution to work
|
|
31
|
+
return noDocument;
|
|
32
|
+
},
|
|
33
|
+
async executeSetMutation (modelID, unique, data, info) {
|
|
34
|
+
// Create the mutation
|
|
35
|
+
const mutation = await createSetMutation({
|
|
36
|
+
signer: client.signer,
|
|
37
|
+
modelID,
|
|
38
|
+
data,
|
|
39
|
+
unique
|
|
40
|
+
});
|
|
41
|
+
// Add the signed mutation to context so it can be retrieved by the client
|
|
42
|
+
client.mutations[info.path.key] = stringifyToken(mutation);
|
|
43
|
+
// Need to return the mutation result for execution to work
|
|
44
|
+
return noDocument;
|
|
45
|
+
},
|
|
46
|
+
async executeUpdateMutation (input, info) {
|
|
47
|
+
const mutation = await createChangeMutation({
|
|
48
|
+
loadState: client.loadState,
|
|
49
|
+
signer: client.signer,
|
|
50
|
+
docID: input.id,
|
|
51
|
+
patch: input.patch.map((patch)=>{
|
|
52
|
+
// convert GraphQL patch input types to PatchOperation
|
|
53
|
+
const entries = Object.entries(patch);
|
|
54
|
+
const [op, rest] = entries[0];
|
|
55
|
+
return {
|
|
56
|
+
...rest,
|
|
57
|
+
op
|
|
58
|
+
};
|
|
59
|
+
}),
|
|
60
|
+
from: input.from
|
|
61
|
+
});
|
|
62
|
+
// Add the signed mutation to context so it can be retrieved by the client
|
|
63
|
+
client.mutations[info.path.key] = stringifyToken(mutation);
|
|
64
|
+
// Need to return the mutation result for execution to work
|
|
65
|
+
return noDocument;
|
|
66
|
+
},
|
|
67
|
+
async executeRemoveMutation (id, info) {
|
|
68
|
+
const mutation = await createRemoveMutation({
|
|
69
|
+
docID: id,
|
|
70
|
+
signer: client.signer
|
|
71
|
+
});
|
|
72
|
+
// Add the signed mutation to context so it can be retrieved by the client
|
|
73
|
+
client.mutations[info.path.key] = stringifyToken(mutation);
|
|
11
74
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
}),
|
|
32
|
-
mutateAndGetPayload: async (input, ctx, info)=>{
|
|
33
|
-
// Create the mutation token
|
|
34
|
-
const unique = globalThis.crypto.getRandomValues(new Uint8Array(12));
|
|
35
|
-
const mutation = await createSetMutation({
|
|
36
|
-
signer: ctx.signer,
|
|
37
|
-
modelID: id,
|
|
38
|
-
data: input.data,
|
|
39
|
-
// owner: ctx.owner,
|
|
40
|
-
unique
|
|
41
|
-
});
|
|
42
|
-
// Add the signed mutation to context so it can be retrieved by the client
|
|
43
|
-
ctx.mutations[info.path.key] = stringifyToken(mutation);
|
|
44
|
-
// Need to return the mutation result for execution to work
|
|
45
|
-
return {
|
|
46
|
-
document: null
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
break;
|
|
51
|
-
}
|
|
52
|
-
case 'unique':
|
|
53
|
-
{
|
|
54
|
-
this._mutations[`set${name}`] = mutationWithClientMutationId({
|
|
55
|
-
name: `Set${name}`,
|
|
56
|
-
inputFields: ()=>({
|
|
57
|
-
data: {
|
|
58
|
-
type: new GraphQLNonNull(this._inputObjects[id])
|
|
59
|
-
}
|
|
60
|
-
}),
|
|
61
|
-
outputFields: ()=>({
|
|
62
|
-
...definitions.queryFields,
|
|
63
|
-
document: {
|
|
64
|
-
type: this._types[id]
|
|
65
|
-
}
|
|
66
|
-
}),
|
|
67
|
-
mutateAndGetPayload: async (input, ctx, info)=>{
|
|
68
|
-
// Create the mutation token
|
|
69
|
-
const mutation = await createSetMutation({
|
|
70
|
-
signer: ctx.signer,
|
|
71
|
-
modelID: id,
|
|
72
|
-
data: input.data,
|
|
73
|
-
// owner: ctx.owner,
|
|
74
|
-
unique: getUniqueDocValue(model.uniqueFields, input.data)
|
|
75
|
-
});
|
|
76
|
-
// Add the signed mutation to context so it can be retrieved by the client
|
|
77
|
-
ctx.mutations[info.path.key] = stringifyToken(mutation);
|
|
78
|
-
// Need to return the mutation result for execution to work
|
|
79
|
-
return {
|
|
80
|
-
document: null
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
break;
|
|
85
|
-
}
|
|
75
|
+
};
|
|
76
|
+
return context;
|
|
77
|
+
}
|
|
78
|
+
export class MutationsRunner {
|
|
79
|
+
#schemas;
|
|
80
|
+
constructor(params = {}){
|
|
81
|
+
this.#schemas = params.schemas ?? {};
|
|
82
|
+
}
|
|
83
|
+
hasSchema(id) {
|
|
84
|
+
return this.#schemas[id] != null;
|
|
85
|
+
}
|
|
86
|
+
setSchema(id, recordOrPromise) {
|
|
87
|
+
this.#schemas[id] = Promise.resolve(recordOrPromise).then(createSchema);
|
|
88
|
+
}
|
|
89
|
+
async execute(params) {
|
|
90
|
+
const schemaPromise = this.#schemas[params.schemaID];
|
|
91
|
+
if (schemaPromise == null) {
|
|
92
|
+
throw new Error(`Schema ${params.schemaID} not found`);
|
|
86
93
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
},
|
|
93
|
-
patch: {
|
|
94
|
-
type: new GraphQLNonNull(toList(PatchOperation))
|
|
95
|
-
},
|
|
96
|
-
from: {
|
|
97
|
-
type: this._inputObjects[`${id}-update`]
|
|
98
|
-
}
|
|
99
|
-
}),
|
|
100
|
-
outputFields: ()=>({
|
|
101
|
-
...definitions.queryFields,
|
|
102
|
-
document: {
|
|
103
|
-
type: this._types[id]
|
|
104
|
-
}
|
|
105
|
-
}),
|
|
106
|
-
mutateAndGetPayload: async (input, ctx, info)=>{
|
|
107
|
-
const mutation = await createChangeMutation({
|
|
108
|
-
loadState: ctx.loadState,
|
|
109
|
-
signer: ctx.signer,
|
|
110
|
-
docID: input.id,
|
|
111
|
-
patch: input.patch.map((patch)=>{
|
|
112
|
-
// convert GraphQL patch input types to PatchOperationType
|
|
113
|
-
const entries = Object.entries(patch);
|
|
114
|
-
const [op, rest] = entries[0];
|
|
115
|
-
return {
|
|
116
|
-
...rest,
|
|
117
|
-
op
|
|
118
|
-
};
|
|
119
|
-
}),
|
|
120
|
-
from: input.from
|
|
121
|
-
});
|
|
122
|
-
// Add the signed mutation to context so it can be retrieved by the client
|
|
123
|
-
ctx.mutations[info.path.key] = stringifyToken(mutation);
|
|
124
|
-
// Need to return the mutation result for execution to work
|
|
125
|
-
return {
|
|
126
|
-
document: null
|
|
127
|
-
};
|
|
128
|
-
}
|
|
94
|
+
return await graphql({
|
|
95
|
+
contextValue: createContext(params.context),
|
|
96
|
+
schema: await schemaPromise,
|
|
97
|
+
source: params.source,
|
|
98
|
+
variableValues: params.variables
|
|
129
99
|
});
|
|
130
100
|
}
|
|
131
101
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
export { type Attachment, createAttachment, toAttachmentsRecord } from './attachments.js';
|
|
2
2
|
export { type ClientParams, KubunClient, type KubunClientType, KubunLightClient, KubunReadClient, type MutateGraphParams, type ReadClientParams, } from './client.js';
|
|
3
|
-
export { ClientSchemaBuilder } from './graphql.js';
|
|
4
3
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAA;AACzF,OAAO,EACL,KAAK,YAAY,EACjB,WAAW,EACX,KAAK,eAAe,EACpB,gBAAgB,EAChB,eAAe,EACf,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,GACtB,MAAM,aAAa,CAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAA;AACzF,OAAO,EACL,KAAK,YAAY,EACjB,WAAW,EACX,KAAK,eAAe,EACpB,gBAAgB,EAChB,eAAe,EACf,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,GACtB,MAAM,aAAa,CAAA"}
|
package/lib/index.js
CHANGED
package/lib/mutations.d.ts
CHANGED
|
@@ -1,31 +1,7 @@
|
|
|
1
1
|
import type { SignedToken, TokenSigner } from '@enkaku/token';
|
|
2
|
+
import type { PatchOperation } from '@kubun/graphql';
|
|
2
3
|
import { DocumentID, type DocumentModelID } from '@kubun/id';
|
|
3
4
|
import type { ChangeDocumentMutation, DocumentData, SetDocumentMutation } from '@kubun/protocol';
|
|
4
|
-
export type PatchAddOperation = {
|
|
5
|
-
op: 'add';
|
|
6
|
-
path: string;
|
|
7
|
-
value: unknown;
|
|
8
|
-
};
|
|
9
|
-
export type PatchRemoveOperation = {
|
|
10
|
-
op: 'remove';
|
|
11
|
-
path: string;
|
|
12
|
-
};
|
|
13
|
-
export type PatchReplaceOperation = {
|
|
14
|
-
op: 'replace';
|
|
15
|
-
path: string;
|
|
16
|
-
value: unknown;
|
|
17
|
-
};
|
|
18
|
-
export type PatchMoveOperation = {
|
|
19
|
-
op: 'move';
|
|
20
|
-
from: string;
|
|
21
|
-
path: string;
|
|
22
|
-
};
|
|
23
|
-
export type PatchCopyOperation = {
|
|
24
|
-
op: 'copy';
|
|
25
|
-
from: string;
|
|
26
|
-
path: string;
|
|
27
|
-
};
|
|
28
|
-
export type PatchOperation = PatchAddOperation | PatchCopyOperation | PatchMoveOperation | PatchRemoveOperation | PatchReplaceOperation;
|
|
29
5
|
export type SetMutationParams<Data extends DocumentData = DocumentData> = {
|
|
30
6
|
data: Data | null;
|
|
31
7
|
modelID: DocumentModelID | string;
|
|
@@ -42,4 +18,9 @@ export type CreateChangeMutationParams<Data extends DocumentData = DocumentData>
|
|
|
42
18
|
signer: TokenSigner;
|
|
43
19
|
};
|
|
44
20
|
export declare function createChangeMutation<Data extends DocumentData = DocumentData>(params: CreateChangeMutationParams<Data>): Promise<SignedToken<ChangeDocumentMutation>>;
|
|
21
|
+
export type RemoveMutationParams = {
|
|
22
|
+
docID: string;
|
|
23
|
+
signer: TokenSigner;
|
|
24
|
+
};
|
|
25
|
+
export declare function createRemoveMutation(params: RemoveMutationParams): Promise<SignedToken<ChangeDocumentMutation>>;
|
|
45
26
|
//# sourceMappingURL=mutations.d.ts.map
|
package/lib/mutations.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../src/mutations.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC7D,OAAO,
|
|
1
|
+
{"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../src/mutations.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC7D,OAAO,KAAK,EAAE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B,oBAAoB,CACxC,MAAM,EAAE,oBAAoB,GAC3B,OAAO,CAAC,WAAW,CAAC,sBAAsB,CAAC,CAAC,CAQ9C"}
|
package/lib/mutations.js
CHANGED
|
@@ -111,3 +111,12 @@ export async function createChangeMutation(params) {
|
|
|
111
111
|
inc: loadedDoc != null
|
|
112
112
|
});
|
|
113
113
|
}
|
|
114
|
+
export async function createRemoveMutation(params) {
|
|
115
|
+
return await params.signer.createToken({
|
|
116
|
+
typ: 'change',
|
|
117
|
+
iss: params.signer.id,
|
|
118
|
+
sub: params.docID,
|
|
119
|
+
data: null,
|
|
120
|
+
inc: false
|
|
121
|
+
});
|
|
122
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubun/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"license": "see LICENSE.md",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"type": "module",
|
|
@@ -16,18 +16,17 @@
|
|
|
16
16
|
"sideEffects": false,
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@automerge/automerge": "^2.2.8",
|
|
19
|
-
"@enkaku/async": "^0.
|
|
20
|
-
"@enkaku/client": "^0.
|
|
21
|
-
"@enkaku/codec": "^0.
|
|
22
|
-
"@enkaku/token": "^0.
|
|
19
|
+
"@enkaku/async": "^0.12.0",
|
|
20
|
+
"@enkaku/client": "^0.12.0",
|
|
21
|
+
"@enkaku/codec": "^0.12.0",
|
|
22
|
+
"@enkaku/token": "^0.12.0",
|
|
23
23
|
"graphql": "^16.9.0",
|
|
24
|
-
"graphql-relay": "^0.10.2",
|
|
25
24
|
"multiformats": "^13.3.2",
|
|
26
|
-
"@kubun/graphql": "^0.
|
|
27
|
-
"@kubun/id": "^0.
|
|
25
|
+
"@kubun/graphql": "^0.3.0",
|
|
26
|
+
"@kubun/id": "^0.3.0"
|
|
28
27
|
},
|
|
29
28
|
"devDependencies": {
|
|
30
|
-
"@kubun/protocol": "^0.
|
|
29
|
+
"@kubun/protocol": "^0.3.0"
|
|
31
30
|
},
|
|
32
31
|
"scripts": {
|
|
33
32
|
"build:clean": "del lib",
|
package/lib/errors.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { GraphQLError } from 'graphql';
|
|
2
|
-
export declare class GraphQLExecutionError extends Error {
|
|
3
|
-
#private;
|
|
4
|
-
constructor(errors: ReadonlyArray<GraphQLError>, message?: string);
|
|
5
|
-
get errors(): ReadonlyArray<GraphQLError>;
|
|
6
|
-
}
|
|
7
|
-
//# sourceMappingURL=errors.d.ts.map
|
package/lib/errors.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAE3C,qBAAa,qBAAsB,SAAQ,KAAK;;gBAGlC,MAAM,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,OAAO,SAA6B;IAMrF,IAAI,MAAM,IAAI,aAAa,CAAC,YAAY,CAAC,CAExC;CACF"}
|
package/lib/errors.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export class GraphQLExecutionError extends Error {
|
|
2
|
-
#errors;
|
|
3
|
-
constructor(errors, message = 'GraphQL execution failed'){
|
|
4
|
-
super(message);
|
|
5
|
-
this.name = GraphQLExecutionError.name;
|
|
6
|
-
this.#errors = errors;
|
|
7
|
-
}
|
|
8
|
-
get errors() {
|
|
9
|
-
return this.#errors;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { type ExecutionResult, type GraphQLSchema } from 'graphql';
|
|
2
|
-
import type { Context } from './graphql.js';
|
|
3
|
-
type SchemasRecord = Record<string, GraphQLSchema | Promise<GraphQLSchema>>;
|
|
4
|
-
type MutationRunnerParams = {
|
|
5
|
-
schemas?: SchemasRecord;
|
|
6
|
-
};
|
|
7
|
-
type ExecuteMutationParams = {
|
|
8
|
-
context: Context;
|
|
9
|
-
schemaID: string;
|
|
10
|
-
source: string;
|
|
11
|
-
variables?: Record<string, unknown>;
|
|
12
|
-
};
|
|
13
|
-
export declare class MutationsRunner {
|
|
14
|
-
#private;
|
|
15
|
-
constructor(params?: MutationRunnerParams);
|
|
16
|
-
hasSchema(id: string): boolean;
|
|
17
|
-
setSchema(id: string, schemaOrPromise: GraphQLSchema | Promise<GraphQLSchema>): void;
|
|
18
|
-
execute(params: ExecuteMutationParams): Promise<ExecutionResult>;
|
|
19
|
-
}
|
|
20
|
-
export {};
|
|
21
|
-
//# sourceMappingURL=mutations-runner.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mutations-runner.d.ts","sourceRoot":"","sources":["../src/mutations-runner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,eAAe,EAAE,KAAK,aAAa,EAAW,MAAM,SAAS,CAAA;AAE3E,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAE3C,KAAK,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC,CAAA;AAE3E,KAAK,oBAAoB,GAAG;IAC1B,OAAO,CAAC,EAAE,aAAa,CAAA;CACxB,CAAA;AAED,KAAK,qBAAqB,GAAG;IAC3B,OAAO,EAAE,OAAO,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;IACd,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACpC,CAAA;AAED,qBAAa,eAAe;;gBAGd,MAAM,GAAE,oBAAyB;IAI7C,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAI9B,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,eAAe,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,GAAG,IAAI;IAI9E,OAAO,CAAC,MAAM,EAAE,qBAAqB,GAAG,OAAO,CAAC,eAAe,CAAC;CAavE"}
|
package/lib/mutations-runner.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { graphql } from 'graphql';
|
|
2
|
-
export class MutationsRunner {
|
|
3
|
-
#schemas;
|
|
4
|
-
constructor(params = {}){
|
|
5
|
-
this.#schemas = params.schemas ?? {};
|
|
6
|
-
}
|
|
7
|
-
hasSchema(id) {
|
|
8
|
-
return this.#schemas[id] != null;
|
|
9
|
-
}
|
|
10
|
-
setSchema(id, schemaOrPromise) {
|
|
11
|
-
this.#schemas[id] = schemaOrPromise;
|
|
12
|
-
}
|
|
13
|
-
async execute(params) {
|
|
14
|
-
const schemaPromise = this.#schemas[params.schemaID];
|
|
15
|
-
if (schemaPromise == null) {
|
|
16
|
-
throw new Error(`Schema ${params.schemaID} not found`);
|
|
17
|
-
}
|
|
18
|
-
return await graphql({
|
|
19
|
-
contextValue: params.context,
|
|
20
|
-
schema: await schemaPromise,
|
|
21
|
-
source: params.source,
|
|
22
|
-
variableValues: params.variables
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
}
|