@soda-gql/runtime 0.0.9 → 0.2.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/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # @soda-gql/runtime
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@soda-gql/runtime.svg)](https://www.npmjs.com/package/@soda-gql/runtime)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ Runtime utilities for soda-gql zero-runtime GraphQL operations.
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ bun add @soda-gql/runtime
12
+ ```
13
+
14
+ ## Overview
15
+
16
+ This package provides the minimal runtime needed to execute soda-gql GraphQL operations. It includes:
17
+
18
+ - Operation registry for storing compiled GraphQL documents
19
+ - Runtime utilities for operation retrieval
20
+ - Minimal footprint for production builds
21
+
22
+ ## Usage
23
+
24
+ ### With Build-Time Transformation
25
+
26
+ When using soda-gql with a build plugin (Babel, TypeScript, Vite, etc.), the runtime is automatically integrated:
27
+
28
+ ```typescript
29
+ // Your source code
30
+ import { gql } from "@/graphql-system";
31
+
32
+ export const userQuery = gql.default(({ query }, { $var }) =>
33
+ query(
34
+ { name: "GetUser", variables: [$var("id").scalar("ID:!")] },
35
+ ({ f, $ }) => [f.user({ id: $.id })(({ f }) => [f.id(), f.name()])],
36
+ ),
37
+ );
38
+
39
+ // After transformation (automatically handled by build plugin)
40
+ import { gqlRuntime } from "@soda-gql/runtime";
41
+
42
+ export const userQuery = gqlRuntime.getOperation("canonicalId");
43
+ ```
44
+
45
+ ## API Reference
46
+
47
+ ### `gqlRuntime`
48
+
49
+ The main runtime object for operation management:
50
+
51
+ ```typescript
52
+ import { gqlRuntime } from "@soda-gql/runtime";
53
+
54
+ // Get an operation by canonical ID
55
+ const operation = gqlRuntime.getOperation("path/to/file.ts::userQuery");
56
+ ```
57
+
58
+ ## Zero-Runtime Philosophy
59
+
60
+ This package is designed to have minimal impact on bundle size:
61
+
62
+ - Operations are registered at build time
63
+ - No heavy dependencies
64
+ - Tree-shakeable exports
65
+
66
+ ## Related Packages
67
+
68
+ - [@soda-gql/core](../core) - Core types and utilities
69
+ - [@soda-gql/babel-plugin](../babel-plugin) - Babel transformation plugin
70
+ - [@soda-gql/tsc-plugin](../tsc-plugin) - TypeScript transformation plugin
71
+
72
+ ## License
73
+
74
+ MIT
package/dist/index.cjs CHANGED
@@ -1,315 +1,8 @@
1
-
2
- //#region packages/core/src/types/type-foundation/var-ref.ts
3
- var VarRef = class {
4
- constructor(inner) {
5
- this.inner = inner;
6
- }
7
- static getInner(varRef) {
8
- return varRef.inner;
9
- }
10
- };
11
- const createVarRefFromVariable = (name) => {
12
- return new VarRef({
13
- type: "variable",
14
- name
15
- });
16
- };
17
-
18
- //#endregion
19
- //#region packages/core/src/utils/map-values.ts
20
- function mapValues(obj, fn) {
21
- return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, fn(value, key)]));
22
- }
23
-
24
- //#endregion
25
- //#region packages/core/src/composer/input.ts
26
- const createVarRefs = (definitions) => mapValues(definitions, (_ref, name) => createVarRefFromVariable(name));
27
-
28
- //#endregion
29
- //#region packages/core/src/types/runtime/projection.ts
30
- /**
31
- * Nominal type representing any slice selection regardless of schema specifics.
32
- * Encodes how individual slices map a concrete field path to a projection
33
- * function. Multiple selections allow slices to expose several derived values.
34
- */
35
- var Projection = class {
36
- constructor(paths, projector) {
37
- this.projector = projector;
38
- this.paths = paths.map((path) => createProjectionPath(path));
39
- }
40
- paths;
41
- };
42
- function createProjectionPath(path) {
43
- const segments = path.split(".");
44
- if (path === "$" || segments.length <= 1) throw new Error("Field path must not be only $ or empty");
45
- return {
46
- full: path,
47
- segments: segments.slice(1)
48
- };
49
- }
50
-
51
- //#endregion
52
- //#region packages/core/src/types/runtime/sliced-execution-result.ts
53
- /** Runtime guard interface shared by all slice result variants. */
54
- var SlicedExecutionResultGuards = class {
55
- isSuccess() {
56
- return this.type === "success";
57
- }
58
- isError() {
59
- return this.type === "error";
60
- }
61
- isEmpty() {
62
- return this.type === "empty";
63
- }
64
- constructor(type) {
65
- this.type = type;
66
- }
67
- };
68
- /** Variant representing an empty payload (no data, no error). */
69
- var SlicedExecutionResultEmpty = class extends SlicedExecutionResultGuards {
70
- constructor() {
71
- super("empty");
72
- }
73
- unwrap() {
74
- return null;
75
- }
76
- safeUnwrap() {
77
- return {
78
- data: void 0,
79
- error: void 0
80
- };
81
- }
82
- };
83
- /** Variant representing a successful payload. */
84
- var SlicedExecutionResultSuccess = class extends SlicedExecutionResultGuards {
85
- constructor(data, extensions) {
86
- super("success");
87
- this.data = data;
88
- this.extensions = extensions;
89
- }
90
- unwrap() {
91
- return this.data;
92
- }
93
- safeUnwrap(transform) {
94
- return {
95
- data: transform(this.data),
96
- error: void 0
97
- };
98
- }
99
- };
100
- /** Variant representing an error payload created by the adapter. */
101
- var SlicedExecutionResultError = class extends SlicedExecutionResultGuards {
102
- constructor(error, extensions) {
103
- super("error");
104
- this.error = error;
105
- this.extensions = extensions;
106
- }
107
- unwrap() {
108
- throw this.error;
109
- }
110
- safeUnwrap() {
111
- return {
112
- data: void 0,
113
- error: this.error
114
- };
115
- }
116
- };
117
-
118
- //#endregion
119
- //#region packages/core/src/runtime/parse-execution-result.ts
120
- function* generateErrorMapEntries(errors, projectionPathGraph) {
121
- for (const error of errors) {
122
- const errorPath = error.path ?? [];
123
- let stack = projectionPathGraph;
124
- for (let i = 0; i <= errorPath.length; i++) {
125
- const segment = errorPath[i];
126
- if (segment == null || typeof segment === "number") {
127
- yield* stack.matches.map(({ label, path }) => ({
128
- label,
129
- path,
130
- error
131
- }));
132
- break;
133
- }
134
- yield* stack.matches.filter(({ exact }) => exact).map(({ label, path }) => ({
135
- label,
136
- path,
137
- error
138
- }));
139
- const next = stack.children[segment];
140
- if (!next) break;
141
- stack = next;
142
- }
143
- }
144
- }
145
- const createErrorMaps = (errors, projectionPathGraph) => {
146
- const errorMaps = {};
147
- for (const { label, path, error } of generateErrorMapEntries(errors ?? [], projectionPathGraph)) {
148
- const mapPerLabel = errorMaps[label] || (errorMaps[label] = {});
149
- (mapPerLabel[path] || (mapPerLabel[path] = [])).push({ error });
150
- }
151
- return errorMaps;
152
- };
153
- const accessDataByPathSegments = (data, pathSegments) => {
154
- let current = data;
155
- for (const segment of pathSegments) {
156
- if (current == null) return { error: /* @__PURE__ */ new Error("No data") };
157
- if (typeof current !== "object") return { error: /* @__PURE__ */ new Error("Incorrect data type") };
158
- if (Array.isArray(current)) return { error: /* @__PURE__ */ new Error("Incorrect data type") };
159
- current = current[segment];
160
- }
161
- return { data: current };
162
- };
163
- const createExecutionResultParser = ({ fragments, projectionPathGraph }) => {
164
- const prepare = (result) => {
165
- if (result.type === "graphql") {
166
- const errorMaps = createErrorMaps(result.body.errors, projectionPathGraph);
167
- return {
168
- ...result,
169
- errorMaps
170
- };
171
- }
172
- if (result.type === "non-graphql-error") return {
173
- ...result,
174
- error: new SlicedExecutionResultError({
175
- type: "non-graphql-error",
176
- error: result.error
177
- })
178
- };
179
- if (result.type === "empty") return {
180
- ...result,
181
- error: new SlicedExecutionResultEmpty()
182
- };
183
- throw new Error("Invalid result type", { cause: result });
184
- };
185
- return (result) => {
186
- const prepared = prepare(result);
187
- const entries = Object.entries(fragments).map(([label, fragment]) => {
188
- const { projection } = fragment;
189
- if (prepared.type === "graphql") {
190
- const matchedErrors = projection.paths.flatMap(({ full: raw }) => prepared.errorMaps[label]?.[raw] ?? []);
191
- const uniqueErrors = Array.from(new Set(matchedErrors.map(({ error }) => error)).values());
192
- if (uniqueErrors.length > 0) return [label, projection.projector(new SlicedExecutionResultError({
193
- type: "graphql-error",
194
- errors: uniqueErrors
195
- }))];
196
- const dataResults = projection.paths.map(({ segments }) => prepared.body.data ? accessDataByPathSegments(prepared.body.data, segments) : { error: /* @__PURE__ */ new Error("No data") });
197
- if (dataResults.some(({ error }) => error)) {
198
- const errors = dataResults.flatMap(({ error }) => error ? [error] : []);
199
- return [label, projection.projector(new SlicedExecutionResultError({
200
- type: "parse-error",
201
- errors
202
- }))];
203
- }
204
- const dataList = dataResults.map(({ data }) => data);
205
- return [label, projection.projector(new SlicedExecutionResultSuccess(dataList))];
206
- }
207
- if (prepared.type === "non-graphql-error") return [label, projection.projector(prepared.error)];
208
- if (prepared.type === "empty") return [label, projection.projector(prepared.error)];
209
- throw new Error("Invalid result type", { cause: prepared });
210
- });
211
- return Object.fromEntries(entries);
212
- };
213
- };
214
-
215
- //#endregion
216
- //#region packages/core/src/runtime/runtime-registry.ts
217
- const composedOperationRegistry = /* @__PURE__ */ new Map();
218
- const inlineOperationRegistry = /* @__PURE__ */ new Map();
219
- const registerComposedOperation = (operation) => {
220
- composedOperationRegistry.set(operation.operationName, operation);
221
- };
222
- const registerInlineOperation = (operation) => {
223
- inlineOperationRegistry.set(operation.operationName, operation);
224
- };
225
- const getComposedOperation = (name) => {
226
- const operation = composedOperationRegistry.get(name);
227
- if (!operation) throw new Error(`Operation ${name} not found`);
228
- return operation;
229
- };
230
- const getInlineOperation = (name) => {
231
- const operation = inlineOperationRegistry.get(name);
232
- if (!operation) throw new Error(`Operation ${name} not found`);
233
- return operation;
234
- };
235
-
236
- //#endregion
237
- //#region packages/core/src/runtime/composed-operation.ts
238
- const createRuntimeComposedOperation = (input) => {
239
- const operation = {
240
- operationType: input.prebuild.operationType,
241
- operationName: input.prebuild.operationName,
242
- variableNames: input.prebuild.variableNames,
243
- projectionPathGraph: input.prebuild.projectionPathGraph,
244
- document: input.prebuild.document,
245
- parse: createExecutionResultParser({
246
- fragments: input.runtime.getSlices({ $: createVarRefs(Object.fromEntries(input.prebuild.variableNames.map((name) => [name, null]))) }),
247
- projectionPathGraph: input.prebuild.projectionPathGraph
248
- })
249
- };
250
- registerComposedOperation(operation);
251
- return operation;
252
- };
253
-
254
- //#endregion
255
- //#region packages/core/src/utils/hidden.ts
256
- const _dummy = () => {
257
- throw new Error("DO NOT CALL THIS FUNCTION -- we use function to safely transfer type information");
258
- };
259
- const hidden = () => _dummy;
260
-
261
- //#endregion
262
- //#region packages/core/src/runtime/inline-operation.ts
263
- const createRuntimeInlineOperation = (input) => {
264
- const operation = {
265
- operationType: input.prebuild.operationType,
266
- operationName: input.prebuild.operationName,
267
- variableNames: input.prebuild.variableNames,
268
- documentSource: hidden(),
269
- document: input.prebuild.document
270
- };
271
- registerInlineOperation(operation);
272
- return operation;
273
- };
274
-
275
- //#endregion
276
- //#region packages/core/src/runtime/model.ts
277
- const createRuntimeModel = (input) => ({
278
- typename: input.prebuild.typename,
279
- fragment: hidden(),
280
- normalize: input.runtime.normalize
281
- });
282
-
283
- //#endregion
284
- //#region packages/core/src/runtime/slice.ts
285
- const handleProjectionBuilder = (projectionBuilder) => projectionBuilder({ select: (path, projector) => new Projection(path, projector) });
286
- const createRuntimeSlice = (input) => {
287
- const projection = handleProjectionBuilder(input.runtime.buildProjection);
288
- return {
289
- operationType: input.prebuild.operationType,
290
- embed: (variables) => ({
291
- variables,
292
- getFields: hidden(),
293
- projection
294
- })
295
- };
296
- };
297
-
298
- //#endregion
299
- //#region packages/core/src/runtime/runtime-adapter.ts
300
- const createRuntimeAdapter = (factory) => factory({ type: hidden });
301
-
302
- //#endregion
303
- //#region packages/core/src/runtime/index.ts
304
- const gqlRuntime = {
305
- model: createRuntimeModel,
306
- composedOperation: createRuntimeComposedOperation,
307
- inlineOperation: createRuntimeInlineOperation,
308
- slice: createRuntimeSlice,
309
- getComposedOperation,
310
- getInlineOperation
311
- };
312
-
313
- //#endregion
314
- exports.createRuntimeAdapter = createRuntimeAdapter;
315
- exports.gqlRuntime = gqlRuntime;
1
+ let __soda_gql_core_runtime = require("@soda-gql/core/runtime");
2
+
3
+ Object.defineProperty(exports, 'gqlRuntime', {
4
+ enumerable: true,
5
+ get: function () {
6
+ return __soda_gql_core_runtime.gqlRuntime;
7
+ }
8
+ });