@ontahi/react 0.1.0-alpha.3 → 0.1.0-alpha.5
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/README.md +80 -2
- package/dist/actions/fetch-operation-bridge-adapter.d.ts.map +1 -1
- package/dist/actions/fetch-operation-bridge-adapter.js +3 -1
- package/dist/actions/fetch-operation-bridge-adapter.js.map +1 -1
- package/dist/actions/operation-bridge-adapter.js +1 -1
- package/dist/actions/operation-bridge-adapter.js.map +1 -1
- package/dist/actions/operation-bridge.d.ts +4 -2
- package/dist/actions/operation-bridge.d.ts.map +1 -1
- package/dist/actions/operation-bridge.js +5 -3
- package/dist/actions/operation-bridge.js.map +1 -1
- package/dist/graph/context.d.ts +6 -1
- package/dist/graph/context.d.ts.map +1 -1
- package/dist/graph/context.js +16 -6
- package/dist/graph/context.js.map +1 -1
- package/dist/graph/executor.d.ts +10 -7
- package/dist/graph/executor.d.ts.map +1 -1
- package/dist/graph/executor.js.map +1 -1
- package/dist/graph/fetch-graph-client.d.ts +22 -0
- package/dist/graph/fetch-graph-client.d.ts.map +1 -0
- package/dist/graph/fetch-graph-client.js +43 -0
- package/dist/graph/fetch-graph-client.js.map +1 -0
- package/dist/graph/fetch-graph-read-executor.d.ts +8 -0
- package/dist/graph/fetch-graph-read-executor.d.ts.map +1 -0
- package/dist/graph/fetch-graph-read-executor.js +37 -0
- package/dist/graph/fetch-graph-read-executor.js.map +1 -0
- package/dist/graph/graph-hooks.d.ts +2 -2
- package/dist/graph/graph-hooks.d.ts.map +1 -1
- package/dist/graph/graph-hooks.js +39 -16
- package/dist/graph/graph-hooks.js.map +1 -1
- package/dist/graph/index.d.ts +2 -0
- package/dist/graph/index.d.ts.map +1 -1
- package/dist/graph/index.js +2 -0
- package/dist/graph/index.js.map +1 -1
- package/dist/graph/operation-hooks.d.ts +2 -1
- package/dist/graph/operation-hooks.d.ts.map +1 -1
- package/dist/graph/operation-hooks.js +31 -13
- package/dist/graph/operation-hooks.js.map +1 -1
- package/dist/graph/operation-types.d.ts +7 -1
- package/dist/graph/operation-types.d.ts.map +1 -1
- package/dist/graph/operation-types.js.map +1 -1
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"executor.js","sourceRoot":"","sources":["../../src/graph/executor.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n GraphCommandSpec,\n GraphOperationDeclaration,\n GraphSchemaLike,\n InferGraphSchemaClientInput,\n QueryOrView,\n QuerySpec,\n} from '@ontahi/core/data-graph';\nimport type { QueryKey, UseMutationOptions, UseQueryOptions } from '@tanstack/react-query';\n\nexport interface ReactGraphExecutor<TReadOptions = unknown, TCommandOptions = TReadOptions> {\n get<TParams, TResult>(\n read: QueryOrView<TParams, TResult>,\n params: TParams,\n options?: TReadOptions,\n ): Promise<TResult | null>;\n run<TParams, TResult>(\n read: QueryOrView<TParams, TResult>,\n params: TParams,\n options?: TReadOptions,\n ): Promise<TResult[]>;\n count<TParams, TResult>(\n read: QueryOrView<TParams, TResult>,\n params: TParams,\n options?: TReadOptions,\n ): Promise<number>;\n runCommand<TResult = void>(\n command: GraphCommandSpec<any, any, TResult>,\n options?: TCommandOptions,\n ): Promise<TResult>;\n}\n\nexport type BuildableRead<TResult> = {\n build: () => QuerySpec<any, TResult>;\n};\n\
|
|
1
|
+
{"version":3,"file":"executor.js","sourceRoot":"","sources":["../../src/graph/executor.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n GraphCommandSpec,\n GraphOperationDeclaration,\n GraphReadExpression,\n GraphReadIntent,\n GraphSchemaLike,\n InferGraphSchemaClientInput,\n QueryOrView,\n QuerySpec,\n} from '@ontahi/core/data-graph';\nimport type { QueryKey, UseMutationOptions, UseQueryOptions } from '@tanstack/react-query';\n\nexport interface ReactGraphExecutor<TReadOptions = unknown, TCommandOptions = TReadOptions> {\n get<TParams, TResult>(\n read: QueryOrView<TParams, TResult>,\n params: TParams,\n options?: TReadOptions,\n ): Promise<TResult | null>;\n run<TParams, TResult>(\n read: QueryOrView<TParams, TResult>,\n params: TParams,\n options?: TReadOptions,\n ): Promise<TResult[]>;\n count<TParams, TResult>(\n read: QueryOrView<TParams, TResult>,\n params: TParams,\n options?: TReadOptions,\n ): Promise<number>;\n runCommand<TResult = void>(\n command: GraphCommandSpec<any, any, TResult>,\n options?: TCommandOptions,\n ): Promise<TResult>;\n}\n\nexport type BuildableRead<TResult> = {\n build: () => QuerySpec<any, TResult>;\n};\n\ntype PlainGraphReadSource<TResult> = QueryOrView<any, TResult> | BuildableRead<TResult>;\n\nexport type GraphReadSource<TResult = unknown> =\n | PlainGraphReadSource<TResult>\n | GraphReadExpression<PlainGraphReadSource<TResult>, GraphReadIntent, TResult>;\n\nexport type ReadParams<TRead> =\n TRead extends GraphReadExpression<infer TSource, any, any>\n ? ReadParams<TSource>\n : TRead extends QueryOrView<infer TParams, any>\n ? TParams\n : undefined;\n\nexport type ReadResult<TRead> =\n TRead extends GraphReadExpression<any, any, infer TResult>\n ? TResult\n : TRead extends QueryOrView<any, infer TResult>\n ? TResult\n : TRead extends BuildableRead<infer TResult>\n ? TResult\n : never;\n\nexport type GraphQueryMode = 'get' | 'run' | 'count' | 'exists';\n\ntype GraphReadIntentData<TRead, TIntent extends GraphReadIntent> = TIntent extends 'count'\n ? number\n : TIntent extends 'exists'\n ? boolean\n : TIntent extends 'one'\n ? ReadResult<TRead>\n : ReadResult<TRead> | null;\n\nexport type GraphQueryData<TRead, TMode extends GraphQueryMode = 'run'> =\n TRead extends GraphReadExpression<any, infer TIntent, any>\n ? GraphReadIntentData<TRead, TIntent>\n : TMode extends 'get'\n ? ReadResult<TRead> | null\n : TMode extends 'run'\n ? ReadResult<TRead>[]\n : TMode extends 'count'\n ? number\n : boolean;\n\nexport type GraphQueryOptions<\n TRead,\n TMode extends GraphQueryMode = 'run',\n TReadOptions = unknown,\n> = Omit<\n UseQueryOptions<GraphQueryData<TRead, TMode>, Error, GraphQueryData<TRead, TMode>, QueryKey>,\n 'queryKey' | 'queryFn'\n> & {\n mode?: TRead extends GraphReadExpression<any, any, any> ? never : TMode;\n params?: ReadParams<TRead>;\n queryKey?: QueryKey;\n runtimeOptions?: TReadOptions;\n};\n\nexport type CommandLike<TResult> =\n | GraphCommandSpec<any, any, TResult>\n | {\n build: () => GraphCommandSpec<any, any, TResult>;\n };\n\nexport type GraphCommandBuilder<TVariables, TResult> = (\n variables: TVariables,\n) => CommandLike<TResult>;\n\nexport type GraphOperationLike<TVariables, TResult> = GraphOperationDeclaration<\n TVariables,\n CommandLike<TResult>\n> & {\n id: string;\n};\n\nexport type GraphOperationClientInput<TOperation> = TOperation extends {\n input: infer TSchema extends GraphSchemaLike;\n}\n ? InferGraphSchemaClientInput<TSchema>\n : TOperation extends GraphOperationDeclaration<infer TInput, any>\n ? TInput\n : never;\n\nexport type GraphOperationResult<TOperation> = TOperation extends {\n run: (...args: any[]) => CommandLike<infer TResult>;\n}\n ? TResult\n : never;\n\nexport type GraphCommandHookOptions<\n TResult,\n TVariables,\n TContext,\n TCommandOptions = unknown,\n> = Omit<UseMutationOptions<TResult, Error, TVariables, TContext>, 'mutationFn'> & {\n mutationKey?: QueryKey;\n runtimeOptions?: TCommandOptions;\n invalidateQueryKeys?: QueryKey[];\n};\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ReflectedEntityDataReader, ReflectedOperationInvoker } from '@ontahi/core/data-graph';
|
|
2
|
+
import { type AnyOperationBridgeAdapter, type FetchOperationBridgeOptions } from '../actions/index.js';
|
|
3
|
+
import type { ReactGraphExecutor } from './executor.js';
|
|
4
|
+
import { type FetchGraphReadExecutorOptions } from './fetch-graph-read-executor.js';
|
|
5
|
+
export type FetchReflectedEntityDataReaderOptions = {
|
|
6
|
+
endpoint?: string;
|
|
7
|
+
fetch?: typeof globalThis.fetch;
|
|
8
|
+
};
|
|
9
|
+
export type OntahiGraphClient<TReadOptions = unknown, TCommandOptions = TReadOptions> = {
|
|
10
|
+
graphExecutor?: ReactGraphExecutor<TReadOptions, TCommandOptions>;
|
|
11
|
+
operationBridgeAdapters?: AnyOperationBridgeAdapter[];
|
|
12
|
+
reflectedEntityDataReader?: ReflectedEntityDataReader;
|
|
13
|
+
reflectedOperationInvoker?: ReflectedOperationInvoker;
|
|
14
|
+
};
|
|
15
|
+
export type FetchGraphClientOptions<TOptions = undefined> = {
|
|
16
|
+
graphRead?: false | FetchGraphReadExecutorOptions<TOptions>;
|
|
17
|
+
operations?: false | FetchOperationBridgeOptions;
|
|
18
|
+
reflectedEntityData?: false | FetchReflectedEntityDataReaderOptions;
|
|
19
|
+
};
|
|
20
|
+
export declare const createFetchReflectedEntityDataReader: ({ endpoint, fetch: fetchRequest, }?: FetchReflectedEntityDataReaderOptions) => ReflectedEntityDataReader;
|
|
21
|
+
export declare const createFetchGraphClient: <TOptions = undefined>({ graphRead, operations, reflectedEntityData, }?: FetchGraphClientOptions<TOptions>) => OntahiGraphClient<TOptions, TOptions>;
|
|
22
|
+
//# sourceMappingURL=fetch-graph-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch-graph-client.d.ts","sourceRoot":"","sources":["../../src/graph/fetch-graph-client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,yBAAyB,EAEzB,yBAAyB,EAC1B,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAGL,KAAK,yBAAyB,EAC9B,KAAK,2BAA2B,EACjC,MAAM,qBAAqB,CAAC;AAE7B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAEL,KAAK,6BAA6B,EACnC,MAAM,gCAAgC,CAAC;AAExC,MAAM,MAAM,qCAAqC,GAAG;IAClD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;CACjC,CAAC;AAEF,MAAM,MAAM,iBAAiB,CAAC,YAAY,GAAG,OAAO,EAAE,eAAe,GAAG,YAAY,IAAI;IACtF,aAAa,CAAC,EAAE,kBAAkB,CAAC,YAAY,EAAE,eAAe,CAAC,CAAC;IAClE,uBAAuB,CAAC,EAAE,yBAAyB,EAAE,CAAC;IACtD,yBAAyB,CAAC,EAAE,yBAAyB,CAAC;IACtD,yBAAyB,CAAC,EAAE,yBAAyB,CAAC;CACvD,CAAC;AAEF,MAAM,MAAM,uBAAuB,CAAC,QAAQ,GAAG,SAAS,IAAI;IAC1D,SAAS,CAAC,EAAE,KAAK,GAAG,6BAA6B,CAAC,QAAQ,CAAC,CAAC;IAC5D,UAAU,CAAC,EAAE,KAAK,GAAG,2BAA2B,CAAC;IACjD,mBAAmB,CAAC,EAAE,KAAK,GAAG,qCAAqC,CAAC;CACrE,CAAC;AAiBF,eAAO,MAAM,oCAAoC,GAAI,qCAGlD,qCAA0C,KAAG,yBAe9C,CAAC;AAEH,eAAO,MAAM,sBAAsB,GAAI,QAAQ,GAAG,SAAS,EAAE,kDAI1D,uBAAuB,CAAC,QAAQ,CAAM,KAAG,iBAAiB,CAAC,QAAQ,EAAE,QAAQ,CAgB/E,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { createFetchOperationBridgeAdapter, createFetchReflectedOperationInvoker, } from '../actions/index.js';
|
|
3
|
+
import { createFetchGraphReadExecutor, } from './fetch-graph-read-executor.js';
|
|
4
|
+
const DEFAULT_OPERATIONS_ENDPOINT = '/operations';
|
|
5
|
+
const DEFAULT_TASKS_ENDPOINT = '/operations/tasks';
|
|
6
|
+
const DEFAULT_REFLECTED_ENTITY_DATA_ENDPOINT = '/explorer/entities';
|
|
7
|
+
const conventionalOperationOptions = (options) => options.mountPath === undefined
|
|
8
|
+
? {
|
|
9
|
+
endpoint: DEFAULT_OPERATIONS_ENDPOINT,
|
|
10
|
+
taskEndpoint: DEFAULT_TASKS_ENDPOINT,
|
|
11
|
+
...options,
|
|
12
|
+
}
|
|
13
|
+
: options;
|
|
14
|
+
export const createFetchReflectedEntityDataReader = ({ endpoint = DEFAULT_REFLECTED_ENTITY_DATA_ENDPOINT, fetch: fetchRequest = globalThis.fetch, } = {}) => ({
|
|
15
|
+
readEntityData: async (query) => {
|
|
16
|
+
const response = await fetchRequest(endpoint, {
|
|
17
|
+
method: 'POST',
|
|
18
|
+
headers: { 'content-type': 'application/json' },
|
|
19
|
+
credentials: 'same-origin',
|
|
20
|
+
body: JSON.stringify(query),
|
|
21
|
+
});
|
|
22
|
+
if (!response.ok) {
|
|
23
|
+
throw new Error(`Reflected entity data request failed with status ${response.status}.`);
|
|
24
|
+
}
|
|
25
|
+
return response.json();
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
export const createFetchGraphClient = ({ graphRead = {}, operations = {}, reflectedEntityData = {}, } = {}) => {
|
|
29
|
+
const operationOptions = operations === false ? undefined : conventionalOperationOptions(operations);
|
|
30
|
+
return {
|
|
31
|
+
...(graphRead === false ? {} : { graphExecutor: createFetchGraphReadExecutor(graphRead) }),
|
|
32
|
+
...(operationOptions
|
|
33
|
+
? {
|
|
34
|
+
operationBridgeAdapters: [createFetchOperationBridgeAdapter(operationOptions)],
|
|
35
|
+
reflectedOperationInvoker: createFetchReflectedOperationInvoker(operationOptions),
|
|
36
|
+
}
|
|
37
|
+
: {}),
|
|
38
|
+
...(reflectedEntityData === false
|
|
39
|
+
? {}
|
|
40
|
+
: { reflectedEntityDataReader: createFetchReflectedEntityDataReader(reflectedEntityData) }),
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=fetch-graph-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch-graph-client.js","sourceRoot":"","sources":["../../src/graph/fetch-graph-client.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAQb,OAAO,EACL,iCAAiC,EACjC,oCAAoC,GAGrC,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EACL,4BAA4B,GAE7B,MAAM,gCAAgC,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AC,UAAU,CAAC,CAAC;IAE9E,OAAO;QACL,GAAG,CAAC,SAAS,KAAK,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,4BAA4B,CAAC,SAAS,CAAC,EAAE,CAAC;QAC1F,GAAG,CAAC,gBAAgB;YAClB,CAAC,CAAC;gBACE,uBAAuB,EAAE,CAAC,iCAAiC,CAAC,gBAAgB,CAAC,CAAC;gBAC9E,yBAAyB,EAAE,oCAAoC,CAAC,gBAAgB,CAAC;aAClF;YACH,CAAC,CAAC,EAAE,CAAC;QACP,GAAG,CAAC,mBAAmB,KAAK,KAAK;YAC/B,CAAC,CAAC,EAAE;YACJ,CAAC,CAAC,EAAE,yBAAyB,EAAE,oCAAoC,CAAC,mBAAmB,CAAC,EAAE,CAAC;KAC9F,CAAC;AACJ,CAAC,CAAC","sourcesContent":["'use client';\n\nimport type {\n ReflectedEntityDataReader,\n ReflectedEntityDataResult,\n ReflectedOperationInvoker,\n} from '@ontahi/core/data-graph';\n\nimport {\n createFetchOperationBridgeAdapter,\n createFetchReflectedOperationInvoker,\n type AnyOperationBridgeAdapter,\n type FetchOperationBridgeOptions,\n} from '../actions/index.js';\n\nimport type { ReactGraphExecutor } from './executor.js';\nimport {\n createFetchGraphReadExecutor,\n type FetchGraphReadExecutorOptions,\n} from './fetch-graph-read-executor.js';\n\nexport type FetchReflectedEntityDataReaderOptions = {\n endpoint?: string;\n fetch?: typeof globalThis.fetch;\n};\n\nexport type OntahiGraphClient<TReadOptions = unknown, TCommandOptions = TReadOptions> = {\n graphExecutor?: ReactGraphExecutor<TReadOptions, TCommandOptions>;\n operationBridgeAdapters?: AnyOperationBridgeAdapter[];\n reflectedEntityDataReader?: ReflectedEntityDataReader;\n reflectedOperationInvoker?: ReflectedOperationInvoker;\n};\n\nexport type FetchGraphClientOptions<TOptions = undefined> = {\n graphRead?: false | FetchGraphReadExecutorOptions<TOptions>;\n operations?: false | FetchOperationBridgeOptions;\n reflectedEntityData?: false | FetchReflectedEntityDataReaderOptions;\n};\n\nconst DEFAULT_OPERATIONS_ENDPOINT = '/operations';\nconst DEFAULT_TASKS_ENDPOINT = '/operations/tasks';\nconst DEFAULT_REFLECTED_ENTITY_DATA_ENDPOINT = '/explorer/entities';\n\nconst conventionalOperationOptions = (\n options: FetchOperationBridgeOptions,\n): FetchOperationBridgeOptions =>\n options.mountPath === undefined\n ? {\n endpoint: DEFAULT_OPERATIONS_ENDPOINT,\n taskEndpoint: DEFAULT_TASKS_ENDPOINT,\n ...options,\n }\n : options;\n\nexport const createFetchReflectedEntityDataReader = ({\n endpoint = DEFAULT_REFLECTED_ENTITY_DATA_ENDPOINT,\n fetch: fetchRequest = globalThis.fetch,\n}: FetchReflectedEntityDataReaderOptions = {}): ReflectedEntityDataReader => ({\n readEntityData: async query => {\n const response = await fetchRequest(endpoint, {\n method: 'POST',\n headers: { 'content-type': 'application/json' },\n credentials: 'same-origin',\n body: JSON.stringify(query),\n });\n\n if (!response.ok) {\n throw new Error(`Reflected entity data request failed with status ${response.status}.`);\n }\n\n return response.json() as Promise<ReflectedEntityDataResult>;\n },\n});\n\nexport const createFetchGraphClient = <TOptions = undefined>({\n graphRead = {},\n operations = {},\n reflectedEntityData = {},\n}: FetchGraphClientOptions<TOptions> = {}): OntahiGraphClient<TOptions, TOptions> => {\n const operationOptions =\n operations === false ? undefined : conventionalOperationOptions(operations);\n\n return {\n ...(graphRead === false ? {} : { graphExecutor: createFetchGraphReadExecutor(graphRead) }),\n ...(operationOptions\n ? {\n operationBridgeAdapters: [createFetchOperationBridgeAdapter(operationOptions)],\n reflectedOperationInvoker: createFetchReflectedOperationInvoker(operationOptions),\n }\n : {}),\n ...(reflectedEntityData === false\n ? {}\n : { reflectedEntityDataReader: createFetchReflectedEntityDataReader(reflectedEntityData) }),\n };\n};\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ReactGraphExecutor } from './executor.js';
|
|
2
|
+
export type FetchGraphReadExecutorOptions<TOptions = undefined> = {
|
|
3
|
+
endpoint?: string;
|
|
4
|
+
fetch?: typeof globalThis.fetch;
|
|
5
|
+
requestInit?: (options?: TOptions) => Omit<RequestInit, 'body' | 'method'>;
|
|
6
|
+
};
|
|
7
|
+
export declare const createFetchGraphReadExecutor: <TOptions = undefined>({ endpoint, fetch: fetchRequest, requestInit, }?: FetchGraphReadExecutorOptions<TOptions>) => ReactGraphExecutor<TOptions, TOptions>;
|
|
8
|
+
//# sourceMappingURL=fetch-graph-read-executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch-graph-read-executor.d.ts","sourceRoot":"","sources":["../../src/graph/fetch-graph-read-executor.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAExD,MAAM,MAAM,6BAA6B,CAAC,QAAQ,GAAG,SAAS,IAAI;IAChE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;IAChC,WAAW,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE,QAAQ,KAAK,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,QAAQ,CAAC,CAAC;CAC5E,CAAC;AAIF,eAAO,MAAM,4BAA4B,GAAI,QAAQ,GAAG,SAAS,EAAE,kDAIhE,6BAA6B,CAAC,QAAQ,CAAM,KAAG,kBAAkB,CAAC,QAAQ,EAAE,QAAQ,CAgCtF,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { createRemoteDataGraphRuntime, isGraphReadProtocolError, } from '@ontahi/core/data-graph';
|
|
3
|
+
import { runBrowserEffect } from '@ontahi/core/runtime/browser';
|
|
4
|
+
const DEFAULT_GRAPH_READ_ENDPOINT = '/graph/reads';
|
|
5
|
+
export const createFetchGraphReadExecutor = ({ endpoint = DEFAULT_GRAPH_READ_ENDPOINT, fetch: fetchRequest = globalThis.fetch, requestInit, } = {}) => {
|
|
6
|
+
const transport = async (request, options) => {
|
|
7
|
+
const init = requestInit?.(options) ?? {};
|
|
8
|
+
const headers = new Headers(init.headers);
|
|
9
|
+
if (!headers.has('content-type'))
|
|
10
|
+
headers.set('content-type', 'application/json');
|
|
11
|
+
const response = await fetchRequest(endpoint, {
|
|
12
|
+
...init,
|
|
13
|
+
method: 'POST',
|
|
14
|
+
headers,
|
|
15
|
+
credentials: init.credentials ?? 'same-origin',
|
|
16
|
+
body: JSON.stringify(request),
|
|
17
|
+
});
|
|
18
|
+
try {
|
|
19
|
+
const payload = await response.json();
|
|
20
|
+
if (!response.ok && !isGraphReadProtocolError(payload)) {
|
|
21
|
+
throw new Error(`Graph read request failed with status ${response.status}.`);
|
|
22
|
+
}
|
|
23
|
+
return payload;
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
throw new Error(`Graph read request failed with status ${response.status}.`);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const runtime = createRemoteDataGraphRuntime({ transport });
|
|
30
|
+
return {
|
|
31
|
+
get: (read, params, options) => runBrowserEffect(runtime.get(read, params, options)),
|
|
32
|
+
run: (read, params, options) => runBrowserEffect(runtime.run(read, params, options)),
|
|
33
|
+
count: (read, params, options) => runBrowserEffect(runtime.count(read, params, options)),
|
|
34
|
+
runCommand: (command, options) => runBrowserEffect(runtime.runCommand(command, options)),
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=fetch-graph-read-executor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch-graph-read-executor.js","sourceRoot":"","sources":["../../src/graph/fetch-graph-read-executor.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EACL,4BAA4B,EAC5B,wBAAwB,GAEzB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAUhE,MAAM,2BAA2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,OAAO;QACL,GAAG,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,CAAC,gBAAgB,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QACpF,GAAG,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,CAAC,gBAAgB,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QACpF,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,CAAC,gBAAgB,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QACxF,UAAU,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,CAAC,gBAAgB,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;KACzF,CAAC;AACJ,CAAC,CAAC","sourcesContent":["'use client';\n\nimport {\n createRemoteDataGraphRuntime,\n isGraphReadProtocolError,\n type RemoteGraphReadTransport,\n} from '@ontahi/core/data-graph';\nimport { runBrowserEffect } from '@ontahi/core/runtime/browser';\n\nimport type { ReactGraphExecutor } from './executor.js';\n\nexport type FetchGraphReadExecutorOptions<TOptions = undefined> = {\n endpoint?: string;\n fetch?: typeof globalThis.fetch;\n requestInit?: (options?: TOptions) => Omit<RequestInit, 'body' | 'method'>;\n};\n\nconst DEFAULT_GRAPH_READ_ENDPOINT = '/graph/reads';\n\nexport const createFetchGraphReadExecutor = <TOptions = undefined>({\n endpoint = DEFAULT_GRAPH_READ_ENDPOINT,\n fetch: fetchRequest = globalThis.fetch,\n requestInit,\n}: FetchGraphReadExecutorOptions<TOptions> = {}): ReactGraphExecutor<TOptions, TOptions> => {\n const transport: RemoteGraphReadTransport<TOptions> = async (request, options) => {\n const init = requestInit?.(options) ?? {};\n const headers = new Headers(init.headers);\n if (!headers.has('content-type')) headers.set('content-type', 'application/json');\n\n const response = await fetchRequest(endpoint, {\n ...init,\n method: 'POST',\n headers,\n credentials: init.credentials ?? 'same-origin',\n body: JSON.stringify(request),\n });\n\n try {\n const payload: unknown = await response.json();\n if (!response.ok && !isGraphReadProtocolError(payload)) {\n throw new Error(`Graph read request failed with status ${response.status}.`);\n }\n return payload;\n } catch {\n throw new Error(`Graph read request failed with status ${response.status}.`);\n }\n };\n const runtime = createRemoteDataGraphRuntime({ transport });\n\n return {\n get: (read, params, options) => runBrowserEffect(runtime.get(read, params, options)),\n run: (read, params, options) => runBrowserEffect(runtime.run(read, params, options)),\n count: (read, params, options) => runBrowserEffect(runtime.count(read, params, options)),\n runCommand: (command, options) => runBrowserEffect(runtime.runCommand(command, options)),\n };\n};\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type UseMutationResult, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
-
import type { GraphCommandBuilder, GraphCommandHookOptions, GraphOperationLike, GraphOperationClientInput, GraphOperationResult, GraphQueryData, GraphQueryMode, GraphQueryOptions, GraphReadSource
|
|
3
|
-
export declare function useGraphQuery<TRead
|
|
2
|
+
import type { GraphCommandBuilder, GraphCommandHookOptions, GraphOperationLike, GraphOperationClientInput, GraphOperationResult, GraphQueryData, GraphQueryMode, GraphQueryOptions, GraphReadSource } from './executor.js';
|
|
3
|
+
export declare function useGraphQuery<TRead extends GraphReadSource<any>, TMode extends GraphQueryMode = 'run', TReadOptions = unknown, TCommandOptions = TReadOptions>(read: TRead, options?: GraphQueryOptions<TRead, TMode, TReadOptions>): UseQueryResult<GraphQueryData<TRead, TMode>, Error>;
|
|
4
4
|
export declare function useGraphCommand<TVariables, TResult = void, TContext = unknown, TReadOptions = unknown, TCommandOptions = TReadOptions>(buildCommand: GraphCommandBuilder<TVariables, TResult>, options?: GraphCommandHookOptions<TResult, TVariables, TContext, TCommandOptions>): UseMutationResult<TResult, Error, TVariables, TContext>;
|
|
5
5
|
export declare function useGraphOperation<TOperation extends GraphOperationLike<any, any>, TContext = unknown, TReadOptions = unknown, TCommandOptions = TReadOptions>(operation: TOperation, options?: GraphCommandHookOptions<GraphOperationResult<TOperation>, GraphOperationClientInput<TOperation>, TContext, TCommandOptions>): UseMutationResult<GraphOperationResult<TOperation>, Error, GraphOperationClientInput<TOperation>, TContext>;
|
|
6
6
|
//# sourceMappingURL=graph-hooks.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph-hooks.d.ts","sourceRoot":"","sources":["../../src/graph/graph-hooks.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"graph-hooks.d.ts","sourceRoot":"","sources":["../../src/graph/graph-hooks.ts"],"names":[],"mappings":"AAaA,OAAO,EAKL,KAAK,iBAAiB,EACtB,KAAK,cAAc,EACpB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,KAAK,EAGV,mBAAmB,EACnB,uBAAuB,EACvB,kBAAkB,EAClB,yBAAyB,EACzB,oBAAoB,EACpB,cAAc,EACd,cAAc,EACd,iBAAiB,EACjB,eAAe,EAGhB,MAAM,eAAe,CAAC;AAgEvB,wBAAgB,aAAa,CAC3B,KAAK,SAAS,eAAe,CAAC,GAAG,CAAC,EAClC,KAAK,SAAS,cAAc,GAAG,KAAK,EACpC,YAAY,GAAG,OAAO,EACtB,eAAe,GAAG,YAAY,EAE9B,IAAI,EAAE,KAAK,EACX,OAAO,CAAC,EAAE,iBAAiB,CAAC,KAAK,EAAE,KAAK,EAAE,YAAY,CAAC,GACtD,cAAc,CAAC,cAAc,CAAC,KAAK,EAAE,KAAK,CAAC,EAAE,KAAK,CAAC,CA6BrD;AAED,wBAAgB,eAAe,CAC7B,UAAU,EACV,OAAO,GAAG,IAAI,EACd,QAAQ,GAAG,OAAO,EAClB,YAAY,GAAG,OAAO,EACtB,eAAe,GAAG,YAAY,EAE9B,YAAY,EAAE,mBAAmB,CAAC,UAAU,EAAE,OAAO,CAAC,EACtD,OAAO,CAAC,EAAE,uBAAuB,CAAC,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,eAAe,CAAC,GAChF,iBAAiB,CAAC,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,QAAQ,CAAC,CAoBzD;AAED,wBAAgB,iBAAiB,CAC/B,UAAU,SAAS,kBAAkB,CAAC,GAAG,EAAE,GAAG,CAAC,EAC/C,QAAQ,GAAG,OAAO,EAClB,YAAY,GAAG,OAAO,EACtB,eAAe,GAAG,YAAY,EAE9B,SAAS,EAAE,UAAU,EACrB,OAAO,CAAC,EAAE,uBAAuB,CAC/B,oBAAoB,CAAC,UAAU,CAAC,EAChC,yBAAyB,CAAC,UAAU,CAAC,EACrC,QAAQ,EACR,eAAe,CAChB,GACA,iBAAiB,CAClB,oBAAoB,CAAC,UAAU,CAAC,EAChC,KAAK,EACL,yBAAyB,CAAC,UAAU,CAAC,EACrC,QAAQ,CACT,CAiBA"}
|
|
@@ -1,35 +1,58 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { normalizeGraphSchemaClientInput, } from '@ontahi/core/data-graph';
|
|
2
|
+
import { isGraphReadExpression, normalizeGraphSchemaClientInput, resolveQuerySpec, toGraphReadRequest, } from '@ontahi/core/data-graph';
|
|
3
|
+
import { executionIdentityCacheKey } from '@ontahi/core/runtime/identity';
|
|
3
4
|
import { useMutation, useQuery, useQueryClient, } from '@tanstack/react-query';
|
|
4
|
-
import { useGraphExecutor } from './context.js';
|
|
5
|
+
import { useExecutionIdentity, useGraphExecutor } from './context.js';
|
|
5
6
|
const isQueryOrView = (read) => 'kind' in read;
|
|
6
7
|
const isBuildableRead = (read) => typeof read.build === 'function';
|
|
7
8
|
const isNamedView = (read) => 'kind' in read && read.kind === 'view';
|
|
8
9
|
const resolveGraphRead = (read) => (isQueryOrView(read) ? read : isBuildableRead(read) ? read.build() : read);
|
|
9
10
|
const resolveGraphCommand = (command) => ('build' in command ? command.build() : command);
|
|
10
|
-
const
|
|
11
|
+
const intentFromMode = (mode) => mode === 'get' ? 'first' : mode === 'run' ? 'many' : mode;
|
|
12
|
+
const modeFromIntent = (intent) => intent === 'many' ? 'run' : intent === 'count' ? 'count' : 'get';
|
|
13
|
+
const deriveGraphQueryKey = (read, intent, params, identityKey) => {
|
|
11
14
|
const resolved = isQueryOrView(read) ? read : resolveGraphRead(read);
|
|
12
15
|
if (isNamedView(resolved)) {
|
|
13
|
-
return [
|
|
16
|
+
return [
|
|
17
|
+
resolved.root.name,
|
|
18
|
+
'graph-read',
|
|
19
|
+
identityKey,
|
|
20
|
+
intent,
|
|
21
|
+
'view',
|
|
22
|
+
resolved.name,
|
|
23
|
+
params ?? null,
|
|
24
|
+
];
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
const request = toGraphReadRequest(resolveQuerySpec(resolved, params), modeFromIntent(intent));
|
|
28
|
+
return [request.selection.entityName, 'graph-read', identityKey, intent, request];
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
const message = 'useGraphQuery could not infer a transport-safe queryKey; provide queryKey explicitly';
|
|
32
|
+
throw new Error(`${message}: ${error instanceof Error ? error.message : String(error)}`);
|
|
14
33
|
}
|
|
15
|
-
throw new Error('useGraphQuery requires queryKey for unnamed selections or query specs');
|
|
16
34
|
};
|
|
17
35
|
export function useGraphQuery(read, options) {
|
|
18
36
|
const graphExecutor = useGraphExecutor();
|
|
37
|
+
const identity = useExecutionIdentity();
|
|
38
|
+
const expression = isGraphReadExpression(read) ? read : undefined;
|
|
39
|
+
const source = (expression?.read ?? read);
|
|
40
|
+
const mode = options?.mode ?? 'run';
|
|
41
|
+
const intent = expression?.intent ?? intentFromMode(mode);
|
|
19
42
|
return useQuery({
|
|
20
43
|
...options,
|
|
21
|
-
queryKey: options
|
|
22
|
-
deriveGraphQueryKey(
|
|
44
|
+
queryKey: options?.queryKey ??
|
|
45
|
+
deriveGraphQueryKey(source, intent, options?.params, executionIdentityCacheKey(identity)),
|
|
23
46
|
queryFn: async () => {
|
|
24
|
-
const resolved = resolveGraphRead(
|
|
25
|
-
const params = options
|
|
26
|
-
const value =
|
|
27
|
-
? await graphExecutor.
|
|
28
|
-
:
|
|
29
|
-
? await graphExecutor.
|
|
30
|
-
:
|
|
31
|
-
? await graphExecutor.
|
|
32
|
-
:
|
|
47
|
+
const resolved = resolveGraphRead(source);
|
|
48
|
+
const params = options?.params;
|
|
49
|
+
const value = intent === 'many'
|
|
50
|
+
? await graphExecutor.run(resolved, params, options?.runtimeOptions)
|
|
51
|
+
: intent === 'count'
|
|
52
|
+
? await graphExecutor.count(resolved, params, options?.runtimeOptions)
|
|
53
|
+
: intent === 'exists'
|
|
54
|
+
? (await graphExecutor.get(resolved, params, options?.runtimeOptions)) != null
|
|
55
|
+
: await graphExecutor.get(resolved, params, options?.runtimeOptions);
|
|
33
56
|
return value;
|
|
34
57
|
},
|
|
35
58
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph-hooks.js","sourceRoot":"","sources":["../../src/graph/graph-hooks.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EACL,+BAA+B,GAIhC,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,WAAW,EACX,QAAQ,EACR,cAAc,GAIf,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAiBhD,MAAM,aAAa,GAAG,CACpB,IAA8B,EACK,EAAE,CAAC,MAAM,IAAI,IAAI,CAAC;AAEvD,MAAM,eAAe,GAAG,CAAU,IAA8B,EAAkC,EAAE,CAClG,OAAQ,IAA+B,CAAC,KAAK,KAAK,UAAU,CAAC;AAE/D,MAAM,WAAW,GAAG,CAClB,IAA+B,EACY,EAAE,CAAC,MAAM,IAAI,IAAI,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,CAAC;AAEvF,MAAM,gBAAgB,GAAG,CAAU,IAA8B,EAAE,EAAE,CACnE,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC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,SAAS,EAAE,CAAC,IAAI,EAAE,SAAS,EAAE,cAAc,EAAE,OAAO,CAAC,CAAC;QACvE,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,iBAAiB,CAM/B,SAAqB,EACrB,OAKC;IASD,MAAM,YAAY,GAAG,CAAC,SAAqB,EAAE,EAAE,CAC7C,SAAS,CAAC,GAAG,CACX,CAAC,SAAS,CAAC,KAAK;QACd,CAAC,CAAC,+BAA+B,CAAC,SAAS,CAAC,KAAK,EAAE,SAAS,CAAC;QAC7D,CAAC,CAAC,SAAS,CAAU,CACxB,CAAC;IAEJ,OAAO,eAAe,CACpB,YAAY,EACZ;QACE,WAAW,EAAE,OAAO,EAAE,WAAW,IAAI,CAAC,iBAAiB,EAAE,SAAS,CAAC,EAAE,CAAC;QACtE,GAAG,OAAO;KACX,CACF,CAAC;AACJ,CAAC","sourcesContent":["'use client';\n\nimport {\n normalizeGraphSchemaClientInput,\n type GraphCommandSpec,\n type QueryOrView,\n type ViewDefinition,\n} from '@ontahi/core/data-graph';\nimport {\n useMutation,\n useQuery,\n useQueryClient,\n type QueryKey,\n type UseMutationResult,\n type UseQueryResult,\n} from '@tanstack/react-query';\n\nimport { useGraphExecutor } from './context.js';\nimport type {\n BuildableRead,\n CommandLike,\n GraphCommandBuilder,\n GraphCommandHookOptions,\n GraphOperationLike,\n GraphOperationClientInput,\n GraphOperationResult,\n GraphQueryData,\n GraphQueryMode,\n GraphQueryOptions,\n GraphReadSource,\n ReadParams,\n ReadResult,\n} from './executor.js';\n\nconst isQueryOrView = <TResult>(\n read: GraphReadSource<TResult>,\n): read is QueryOrView<any, TResult> => 'kind' in read;\n\nconst isBuildableRead = <TResult>(read: GraphReadSource<TResult>): read is BuildableRead<TResult> =>\n typeof (read as BuildableRead<TResult>).build === 'function';\n\nconst isNamedView = <TResult>(\n read: QueryOrView<any, TResult>,\n): read is ViewDefinition<any, any, TResult> => 'kind' in read && read.kind === 'view';\n\nconst resolveGraphRead = <TResult>(read: GraphReadSource<TResult>) =>\n (isQueryOrView(read) ? read : isBuildableRead(read) ? read.build() : read) as QueryOrView<\n any,\n TResult\n >;\n\nconst resolveGraphCommand = <TResult>(command: CommandLike<TResult>) =>\n ('build' in command ? command.build() : command) as GraphCommandSpec<any, any, TResult>;\n\nconst deriveGraphQueryKey = <TRead, TMode extends GraphQueryMode, TReadOptions>(\n read: GraphReadSource<ReadResult<TRead>>,\n mode: TMode,\n params: ReadParams<TRead> | undefined,\n runtimeOptions: TReadOptions | undefined,\n): QueryKey => {\n const resolved = isQueryOrView(read) ? read : resolveGraphRead(read);\n\n if (isNamedView(resolved)) {\n return ['graph', mode, resolved.name, params ?? null, runtimeOptions ?? null];\n }\n\n throw new Error('useGraphQuery requires queryKey for unnamed selections or query specs');\n};\n\nexport function useGraphQuery<\n TRead,\n TMode extends GraphQueryMode,\n TReadOptions = unknown,\n TCommandOptions = TReadOptions,\n>(\n read: GraphReadSource<ReadResult<TRead>>,\n options: GraphQueryOptions<TRead, TMode, TReadOptions>,\n): UseQueryResult<GraphQueryData<TRead, TMode>, Error> {\n const graphExecutor = useGraphExecutor<TReadOptions, TCommandOptions>();\n\n return useQuery({\n ...options,\n queryKey:\n options.queryKey ??\n deriveGraphQueryKey(read, options.mode, options.params, options.runtimeOptions),\n queryFn: async () => {\n const resolved = resolveGraphRead(read);\n const params = options.params as any;\n\n const value =\n options.mode === 'get'\n ? await graphExecutor.get(resolved, params, options.runtimeOptions)\n : options.mode === 'run'\n ? await graphExecutor.run(resolved, params, options.runtimeOptions)\n : options.mode === 'count'\n ? await graphExecutor.count(resolved, params, options.runtimeOptions)\n : (await graphExecutor.get(resolved, params, options.runtimeOptions)) != null;\n\n return value as GraphQueryData<TRead, TMode>;\n },\n });\n}\n\nexport function useGraphCommand<\n TVariables,\n TResult = void,\n TContext = unknown,\n TReadOptions = unknown,\n TCommandOptions = TReadOptions,\n>(\n buildCommand: GraphCommandBuilder<TVariables, TResult>,\n options?: GraphCommandHookOptions<TResult, TVariables, TContext, TCommandOptions>,\n): UseMutationResult<TResult, Error, TVariables, TContext> {\n const graphExecutor = useGraphExecutor<TReadOptions, TCommandOptions>();\n const queryClient = useQueryClient();\n\n return useMutation({\n ...options,\n mutationKey: options?.mutationKey,\n mutationFn: variables =>\n graphExecutor.runCommand(\n resolveGraphCommand(buildCommand(variables)),\n options?.runtimeOptions,\n ),\n onSuccess: async (data, variables, onMutateResult, context) => {\n for (const queryKey of options?.invalidateQueryKeys ?? []) {\n await queryClient.invalidateQueries({ queryKey });\n }\n\n await options?.onSuccess?.(data, variables, onMutateResult, context);\n },\n });\n}\n\nexport function useGraphOperation<\n TOperation extends GraphOperationLike<any, any>,\n TContext = unknown,\n TReadOptions = unknown,\n TCommandOptions = TReadOptions,\n>(\n operation: TOperation,\n options?: GraphCommandHookOptions<\n GraphOperationResult<TOperation>,\n GraphOperationClientInput<TOperation>,\n TContext,\n TCommandOptions\n >,\n): UseMutationResult<\n GraphOperationResult<TOperation>,\n Error,\n GraphOperationClientInput<TOperation>,\n TContext\n> {\n type TVariables = GraphOperationClientInput<TOperation>;\n type TResult = GraphOperationResult<TOperation>;\n const buildCommand = (variables: TVariables) =>\n operation.run(\n (operation.input\n ? normalizeGraphSchemaClientInput(operation.input, variables)\n : variables) as never,\n );\n\n return useGraphCommand<TVariables, TResult, TContext, TReadOptions, TCommandOptions>(\n buildCommand,\n {\n mutationKey: options?.mutationKey ?? ['graph-operation', operation.id],\n ...options,\n },\n );\n}\n"]}
|
|
1
|
+
{"version":3,"file":"graph-hooks.js","sourceRoot":"","sources":["../../src/graph/graph-hooks.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EACL,qBAAqB,EACrB,+BAA+B,EAC/B,gBAAgB,EAChB,kBAAkB,GAKnB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,yBAAyB,EAAE,MAAM,+BAA+B,CAAC;AAC1E,OAAO,EACL,WAAW,EACX,QAAQ,EACR,cAAc,GAIf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AAW,EAAE,OAAO,EAAE,WAAW;QACjC,UAAU,EAAE,SAAS,CAAC,EAAE,CACtB,aAAa,CAAC,UAAU,CACtB,mBAAmB,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC,EAC5C,OAAO,EAAE,cAAc,CACxB;QACH,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,cAAc,EAAE,OAAO,EAAE,EAAE;YAC5D,KAAK,MAAM,QAAQ,IAAI,OAAO,EAAE,mBAAmB,IAAI,EAAE,EAAE,CAAC;gBAC1D,MAAM,WAAW,CAAC,iBAAiB,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC;YACpD,CAAC;YAED,MAAM,OAAO,EAAE,SAAS,EAAE,CAAC,IAAI,EAAE,SAAS,EAAE,cAAc,EAAE,OAAO,CAAC,CAAC;QACvE,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,iBAAiB,CAM/B,SAAqB,EACrB,OAKC;IASD,MAAM,YAAY,GAAG,CAAC,SAAqB,EAAE,EAAE,CAC7C,SAAS,CAAC,GAAG,CACX,CAAC,SAAS,CAAC,KAAK;QACd,CAAC,CAAC,+BAA+B,CAAC,SAAS,CAAC,KAAK,EAAE,SAAS,CAAC;QAC7D,CAAC,CAAC,SAAS,CAAU,CACxB,CAAC;IAEJ,OAAO,eAAe,CACpB,YAAY,EACZ;QACE,WAAW,EAAE,OAAO,EAAE,WAAW,IAAI,CAAC,iBAAiB,EAAE,SAAS,CAAC,EAAE,CAAC;QACtE,GAAG,OAAO;KACX,CACF,CAAC;AACJ,CAAC","sourcesContent":["'use client';\n\nimport {\n isGraphReadExpression,\n normalizeGraphSchemaClientInput,\n resolveQuerySpec,\n toGraphReadRequest,\n type GraphCommandSpec,\n type GraphReadIntent,\n type QueryOrView,\n type ViewDefinition,\n} from '@ontahi/core/data-graph';\nimport { executionIdentityCacheKey } from '@ontahi/core/runtime/identity';\nimport {\n useMutation,\n useQuery,\n useQueryClient,\n type QueryKey,\n type UseMutationResult,\n type UseQueryResult,\n} from '@tanstack/react-query';\n\nimport { useExecutionIdentity, useGraphExecutor } from './context.js';\nimport type {\n BuildableRead,\n CommandLike,\n GraphCommandBuilder,\n GraphCommandHookOptions,\n GraphOperationLike,\n GraphOperationClientInput,\n GraphOperationResult,\n GraphQueryData,\n GraphQueryMode,\n GraphQueryOptions,\n GraphReadSource,\n ReadParams,\n ReadResult,\n} from './executor.js';\n\nconst isQueryOrView = <TResult>(\n read: Exclude<GraphReadSource<TResult>, { kind: 'graph-read-expression' }>,\n): read is QueryOrView<any, TResult> => 'kind' in read;\n\nconst isBuildableRead = <TResult>(\n read: Exclude<GraphReadSource<TResult>, { kind: 'graph-read-expression' }>,\n): read is BuildableRead<TResult> => typeof (read as BuildableRead<TResult>).build === 'function';\n\nconst isNamedView = <TResult>(\n read: QueryOrView<any, TResult>,\n): read is ViewDefinition<any, any, TResult> => 'kind' in read && read.kind === 'view';\n\ntype PlainGraphReadSource<TResult> = Exclude<\n GraphReadSource<TResult>,\n { kind: 'graph-read-expression' }\n>;\n\nconst resolveGraphRead = <TResult>(read: PlainGraphReadSource<TResult>) =>\n (isQueryOrView(read) ? read : isBuildableRead(read) ? read.build() : read) as QueryOrView<\n any,\n TResult\n >;\n\nconst resolveGraphCommand = <TResult>(command: CommandLike<TResult>) =>\n ('build' in command ? command.build() : command) as GraphCommandSpec<any, any, TResult>;\n\nconst intentFromMode = (mode: GraphQueryMode): GraphReadIntent | 'many' =>\n mode === 'get' ? 'first' : mode === 'run' ? 'many' : mode;\n\nconst modeFromIntent = (intent: GraphReadIntent | 'many'): Exclude<GraphQueryMode, 'exists'> =>\n intent === 'many' ? 'run' : intent === 'count' ? 'count' : 'get';\n\nconst deriveGraphQueryKey = <TRead>(\n read: PlainGraphReadSource<ReadResult<TRead>>,\n intent: GraphReadIntent | 'many',\n params: ReadParams<TRead> | undefined,\n identityKey: readonly unknown[],\n): QueryKey => {\n const resolved = isQueryOrView(read) ? read : resolveGraphRead(read);\n\n if (isNamedView(resolved)) {\n return [\n resolved.root.name,\n 'graph-read',\n identityKey,\n intent,\n 'view',\n resolved.name,\n params ?? null,\n ];\n }\n\n try {\n const request = toGraphReadRequest(resolveQuerySpec(resolved, params), modeFromIntent(intent));\n return [request.selection.entityName, 'graph-read', identityKey, intent, request];\n } catch (error) {\n const message =\n 'useGraphQuery could not infer a transport-safe queryKey; provide queryKey explicitly';\n throw new Error(`${message}: ${error instanceof Error ? error.message : String(error)}`);\n }\n};\n\nexport function useGraphQuery<\n TRead extends GraphReadSource<any>,\n TMode extends GraphQueryMode = 'run',\n TReadOptions = unknown,\n TCommandOptions = TReadOptions,\n>(\n read: TRead,\n options?: GraphQueryOptions<TRead, TMode, TReadOptions>,\n): UseQueryResult<GraphQueryData<TRead, TMode>, Error> {\n const graphExecutor = useGraphExecutor<TReadOptions, TCommandOptions>();\n const identity = useExecutionIdentity();\n const expression = isGraphReadExpression(read) ? read : undefined;\n const source = (expression?.read ?? read) as PlainGraphReadSource<ReadResult<TRead>>;\n const mode = options?.mode ?? 'run';\n const intent = expression?.intent ?? intentFromMode(mode);\n\n return useQuery({\n ...options,\n queryKey:\n options?.queryKey ??\n deriveGraphQueryKey(source, intent, options?.params, executionIdentityCacheKey(identity)),\n queryFn: async () => {\n const resolved = resolveGraphRead(source);\n const params = options?.params as any;\n\n const value =\n intent === 'many'\n ? await graphExecutor.run(resolved, params, options?.runtimeOptions)\n : intent === 'count'\n ? await graphExecutor.count(resolved, params, options?.runtimeOptions)\n : intent === 'exists'\n ? (await graphExecutor.get(resolved, params, options?.runtimeOptions)) != null\n : await graphExecutor.get(resolved, params, options?.runtimeOptions);\n\n return value as GraphQueryData<TRead, TMode>;\n },\n });\n}\n\nexport function useGraphCommand<\n TVariables,\n TResult = void,\n TContext = unknown,\n TReadOptions = unknown,\n TCommandOptions = TReadOptions,\n>(\n buildCommand: GraphCommandBuilder<TVariables, TResult>,\n options?: GraphCommandHookOptions<TResult, TVariables, TContext, TCommandOptions>,\n): UseMutationResult<TResult, Error, TVariables, TContext> {\n const graphExecutor = useGraphExecutor<TReadOptions, TCommandOptions>();\n const queryClient = useQueryClient();\n\n return useMutation({\n ...options,\n mutationKey: options?.mutationKey,\n mutationFn: variables =>\n graphExecutor.runCommand(\n resolveGraphCommand(buildCommand(variables)),\n options?.runtimeOptions,\n ),\n onSuccess: async (data, variables, onMutateResult, context) => {\n for (const queryKey of options?.invalidateQueryKeys ?? []) {\n await queryClient.invalidateQueries({ queryKey });\n }\n\n await options?.onSuccess?.(data, variables, onMutateResult, context);\n },\n });\n}\n\nexport function useGraphOperation<\n TOperation extends GraphOperationLike<any, any>,\n TContext = unknown,\n TReadOptions = unknown,\n TCommandOptions = TReadOptions,\n>(\n operation: TOperation,\n options?: GraphCommandHookOptions<\n GraphOperationResult<TOperation>,\n GraphOperationClientInput<TOperation>,\n TContext,\n TCommandOptions\n >,\n): UseMutationResult<\n GraphOperationResult<TOperation>,\n Error,\n GraphOperationClientInput<TOperation>,\n TContext\n> {\n type TVariables = GraphOperationClientInput<TOperation>;\n type TResult = GraphOperationResult<TOperation>;\n const buildCommand = (variables: TVariables) =>\n operation.run(\n (operation.input\n ? normalizeGraphSchemaClientInput(operation.input, variables)\n : variables) as never,\n );\n\n return useGraphCommand<TVariables, TResult, TContext, TReadOptions, TCommandOptions>(\n buildCommand,\n {\n mutationKey: options?.mutationKey ?? ['graph-operation', operation.id],\n ...options,\n },\n );\n}\n"]}
|
package/dist/graph/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export * from './context.js';
|
|
2
2
|
export * from './executor.js';
|
|
3
|
+
export * from './fetch-graph-read-executor.js';
|
|
4
|
+
export * from './fetch-graph-client.js';
|
|
3
5
|
export * from './graph-hooks.js';
|
|
4
6
|
export * from './operation-cache.js';
|
|
5
7
|
export * from './operation-hooks.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/graph/index.tsx"],"names":[],"mappings":"AAEA,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kCAAkC,CAAC;AACjD,cAAc,kCAAkC,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/graph/index.tsx"],"names":[],"mappings":"AAEA,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,gCAAgC,CAAC;AAC/C,cAAc,yBAAyB,CAAC;AACxC,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kCAAkC,CAAC;AACjD,cAAc,kCAAkC,CAAC"}
|
package/dist/graph/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
export * from './context.js';
|
|
3
3
|
export * from './executor.js';
|
|
4
|
+
export * from './fetch-graph-read-executor.js';
|
|
5
|
+
export * from './fetch-graph-client.js';
|
|
4
6
|
export * from './graph-hooks.js';
|
|
5
7
|
export * from './operation-cache.js';
|
|
6
8
|
export * from './operation-hooks.js';
|
package/dist/graph/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/graph/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kCAAkC,CAAC;AACjD,cAAc,kCAAkC,CAAC","sourcesContent":["'use client';\n\nexport * from './context.js';\nexport * from './executor.js';\nexport * from './graph-hooks.js';\nexport * from './operation-cache.js';\nexport * from './operation-hooks.js';\nexport * from './operation-types.js';\nexport * from './reflected-entity-data-hooks.js';\nexport * from './reflected-operation-invoker.js';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/graph/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,gCAAgC,CAAC;AAC/C,cAAc,yBAAyB,CAAC;AACxC,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kCAAkC,CAAC;AACjD,cAAc,kCAAkC,CAAC","sourcesContent":["'use client';\n\nexport * from './context.js';\nexport * from './executor.js';\nexport * from './fetch-graph-read-executor.js';\nexport * from './fetch-graph-client.js';\nexport * from './graph-hooks.js';\nexport * from './operation-cache.js';\nexport * from './operation-hooks.js';\nexport * from './operation-types.js';\nexport * from './reflected-entity-data-hooks.js';\nexport * from './reflected-operation-invoker.js';\n"]}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { DomainOperationInvocation as OperationInvocation } from '@ontahi/core/data-graph';
|
|
2
2
|
import { type InfiniteData, type QueryKey, type UseQueryResult } from '@tanstack/react-query';
|
|
3
3
|
import type { GraphPermission } from '../actions/index.js';
|
|
4
|
-
import type { ClientOperationLike, ClientSchemaOperationLike, DurableOperationHookOptions, DurableOperationHookResult, DurableOperationLike, OperationHookOptions, OperationHookResult, OperationInputHookResult, OperationInfiniteQueryOptions, OperationQueryOptions, OperationRunner, ReflectedOperationLike } from './operation-types.js';
|
|
4
|
+
import type { ClientOperationLike, ClientOperationInvocation, ClientSchemaOperationLike, BoundOperationHookResult, DurableOperationHookOptions, DurableOperationHookResult, DurableOperationLike, OperationHookOptions, OperationHookResult, OperationInputHookResult, OperationInfiniteQueryOptions, OperationQueryOptions, OperationRunner, ReflectedOperationLike } from './operation-types.js';
|
|
5
5
|
export declare const getOperationQueryKey: <TInput, TData>(operation: ClientOperationLike<TInput, TData>, input: TInput) => QueryKey;
|
|
6
|
+
export declare function useOperation<TInput, TData>(invocation: ClientOperationInvocation<TInput, TData>, options?: OperationHookOptions<TInput, TData>): BoundOperationHookResult<TInput, TData>;
|
|
6
7
|
export declare function useOperation<TInput, TData>(operation: ClientSchemaOperationLike<TInput, TData>, initialInput: TInput, options?: OperationHookOptions<TInput, TData>): OperationInputHookResult<TInput, TData>;
|
|
7
8
|
export declare function useOperation<TInput, TData>(operation: ClientSchemaOperationLike<TInput, TData>, options?: OperationHookOptions<TInput, TData>): OperationHookResult<TInput, TData>;
|
|
8
9
|
export declare function useOperation<TInput, TData>(operation: ClientOperationLike<TInput, TData>, options?: OperationHookOptions<TInput, TData>): OperationHookResult<TInput, TData>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"operation-hooks.d.ts","sourceRoot":"","sources":["../../src/graph/operation-hooks.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAEV,yBAAyB,IAAI,mBAAmB,EAIjD,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"operation-hooks.d.ts","sourceRoot":"","sources":["../../src/graph/operation-hooks.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAEV,yBAAyB,IAAI,mBAAmB,EAIjD,MAAM,yBAAyB,CAAC;AAKjC,OAAO,EAIL,KAAK,YAAY,EACjB,KAAK,QAAQ,EAEb,KAAK,cAAc,EACpB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,KAAK,EACV,eAAe,EAGhB,MAAM,qBAAqB,CAAC;AAuB7B,OAAO,KAAK,EACV,mBAAmB,EACnB,yBAAyB,EACzB,yBAAyB,EACzB,wBAAwB,EACxB,2BAA2B,EAC3B,0BAA0B,EAC1B,oBAAoB,EAEpB,oBAAoB,EACpB,mBAAmB,EAEnB,wBAAwB,EACxB,6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BAAkB,CAAC,MAAM,EAAE,KAAK,EAC9C,SAAS,EAAE,mBAAmB,CAAC,MAAM,EAAE,KAAK,CAAC,GAC5C,eAAe,CAAC,MAAM,EAAE,KAAK,CAAC,CAsBhC;AAED,wBAAgB,2BAA2B,CAAC,MAAM,GAAG,OAAO,EAAE,KAAK,GAAG,OAAO,EAC3E,SAAS,EAAE,sBAAsB,CAAC,MAAM,EAAE,KAAK,CAAC,GAC/C,eAAe,CAAC,MAAM,EAAE,KAAK,CAAC,CAYhC"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { normalizeGraphSchemaClientInput } from '@ontahi/core/data-graph';
|
|
3
3
|
import { operationInputInvalid } from '@ontahi/core/runtime/contracts';
|
|
4
|
+
import { hasOwn, isRecord } from '@ontahi/core/value/object';
|
|
4
5
|
import { useInfiniteQuery, useQuery, useQueryClient, } from '@tanstack/react-query';
|
|
5
6
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
6
7
|
import { operationBridgeErrored, resolveOperationBridgeInvalidationQueryKeys, resolveOperationBridgeQueryKey, toOperationInvocationResult, unwrapOperationInvocationValue, useAction, } from '../actions/index.js';
|
|
@@ -13,7 +14,7 @@ const operationHookOptionKeys = new Set([
|
|
|
13
14
|
'onSettled',
|
|
14
15
|
'invalidateOnSuccess',
|
|
15
16
|
]);
|
|
16
|
-
const
|
|
17
|
+
const isOperationInvocation = (value) => isRecord(value) && hasOwn(value, 'kind') && value.kind === 'domain-operation-invocation';
|
|
17
18
|
const readOperationInputFields = (input) => {
|
|
18
19
|
if (!isRecord(input))
|
|
19
20
|
return undefined;
|
|
@@ -31,19 +32,26 @@ const looksLikeOperationOptions = (operation, value) => {
|
|
|
31
32
|
if (keys.length === 0)
|
|
32
33
|
return false;
|
|
33
34
|
const inputFields = readOperationInputFields(operation.input);
|
|
34
|
-
if (inputFields && keys.some(key => key
|
|
35
|
+
if (inputFields && keys.some(key => hasOwn(inputFields, key)))
|
|
35
36
|
return false;
|
|
36
37
|
return keys.some(key => operationHookOptionKeys.has(key));
|
|
37
38
|
};
|
|
38
39
|
const issuePath = (path) => Array.isArray(path) ? path.map(String).join('.') : path;
|
|
39
40
|
export const getOperationQueryKey = (operation, input) => resolveOperationBridgeQueryKey(operation, input);
|
|
40
41
|
const getOperationCacheAwareQueryKey = (clientCache, operation, input) => getOperationClientCacheKey(clientCache, operation, input, getOperationQueryKey(operation, input));
|
|
41
|
-
export function useOperation(
|
|
42
|
-
const
|
|
42
|
+
export function useOperation(operationOrInvocation, initialInputOrOptions, managedOptions) {
|
|
43
|
+
const invocation = isOperationInvocation(operationOrInvocation)
|
|
44
|
+
? operationOrInvocation
|
|
45
|
+
: undefined;
|
|
46
|
+
const operation = (invocation?.operation ?? operationOrInvocation);
|
|
47
|
+
const managesInput = !invocation &&
|
|
48
|
+
(managedOptions !== undefined || !looksLikeOperationOptions(operation, initialInputOrOptions));
|
|
43
49
|
const initialInput = managesInput ? initialInputOrOptions : undefined;
|
|
44
|
-
const options =
|
|
45
|
-
?
|
|
46
|
-
:
|
|
50
|
+
const options = invocation
|
|
51
|
+
? initialInputOrOptions
|
|
52
|
+
: managesInput
|
|
53
|
+
? managedOptions
|
|
54
|
+
: initialInputOrOptions;
|
|
47
55
|
const adapter = useDefaultOperationBridgeAdapter();
|
|
48
56
|
const clientCache = useGraphClientCache();
|
|
49
57
|
const queryClient = useQueryClient();
|
|
@@ -113,6 +121,10 @@ export function useOperation(operation, initialInputOrOptions, managedOptions) {
|
|
|
113
121
|
const execute = useCallback((...args) => {
|
|
114
122
|
void executeAsync(...args);
|
|
115
123
|
}, [executeAsync]);
|
|
124
|
+
const executeBoundAsync = useCallback(() => executeInputAsync(invocation?.input), [executeInputAsync, invocation?.input]);
|
|
125
|
+
const executeBound = useCallback(() => {
|
|
126
|
+
void executeBoundAsync();
|
|
127
|
+
}, [executeBoundAsync]);
|
|
116
128
|
const reset = useCallback(() => {
|
|
117
129
|
resetTransport();
|
|
118
130
|
setResult(undefined);
|
|
@@ -188,9 +200,13 @@ export function useOperation(operation, initialInputOrOptions, managedOptions) {
|
|
|
188
200
|
? 'hasErrored'
|
|
189
201
|
: 'hasSucceeded';
|
|
190
202
|
const baseResult = {
|
|
191
|
-
execute: managesInput ? executeManaged : execute,
|
|
192
|
-
executeAsync:
|
|
193
|
-
|
|
203
|
+
execute: invocation ? executeBound : managesInput ? executeManaged : execute,
|
|
204
|
+
executeAsync: invocation
|
|
205
|
+
? executeBoundAsync
|
|
206
|
+
: managesInput
|
|
207
|
+
? executeManagedAsync
|
|
208
|
+
: executeAsync,
|
|
209
|
+
input: invocation ? invocation.input : managesInput ? inputController : lastInput,
|
|
194
210
|
result,
|
|
195
211
|
value: result?.ok ? result.value : undefined,
|
|
196
212
|
reset,
|
|
@@ -202,9 +218,11 @@ export function useOperation(operation, initialInputOrOptions, managedOptions) {
|
|
|
202
218
|
hasSucceeded,
|
|
203
219
|
hasErrored,
|
|
204
220
|
};
|
|
205
|
-
return (
|
|
206
|
-
?
|
|
207
|
-
:
|
|
221
|
+
return (invocation
|
|
222
|
+
? baseResult
|
|
223
|
+
: managesInput
|
|
224
|
+
? { ...baseResult, input: inputController, lastInput }
|
|
225
|
+
: baseResult);
|
|
208
226
|
}
|
|
209
227
|
export function useDurableOperation(operation, options) {
|
|
210
228
|
const adapter = useDefaultOperationBridgeAdapter();
|