@soda-gql/runtime 0.1.0 → 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/README.md +9 -45
- package/dist/index.cjs +8 -317
- package/dist/index.d.cts +2 -751
- package/dist/index.d.ts +2 -751
- package/dist/index.js +2 -315
- package/package.json +23 -3
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ bun add @soda-gql/runtime
|
|
|
16
16
|
This package provides the minimal runtime needed to execute soda-gql GraphQL operations. It includes:
|
|
17
17
|
|
|
18
18
|
- Operation registry for storing compiled GraphQL documents
|
|
19
|
-
- Runtime
|
|
19
|
+
- Runtime utilities for operation retrieval
|
|
20
20
|
- Minimal footprint for production builds
|
|
21
21
|
|
|
22
22
|
## Usage
|
|
@@ -30,29 +30,17 @@ When using soda-gql with a build plugin (Babel, TypeScript, Vite, etc.), the run
|
|
|
30
30
|
import { gql } from "@/graphql-system";
|
|
31
31
|
|
|
32
32
|
export const userQuery = gql.default(({ query }, { $var }) =>
|
|
33
|
-
query.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
query.operation({
|
|
34
|
+
name: "GetUser",
|
|
35
|
+
variables: { ...$var("id").ID("!") },
|
|
36
|
+
fields: ({ f, $ }) => ({ ...f.user({ id: $.id })(({ f }) => ({ ...f.id(), ...f.name() })) }),
|
|
37
|
+
}),
|
|
37
38
|
);
|
|
38
39
|
|
|
39
40
|
// After transformation (automatically handled by build plugin)
|
|
40
41
|
import { gqlRuntime } from "@soda-gql/runtime";
|
|
41
42
|
|
|
42
|
-
export const userQuery = gqlRuntime.
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
### Runtime Adapter
|
|
46
|
-
|
|
47
|
-
For custom client integrations, create a runtime adapter that defines error types:
|
|
48
|
-
|
|
49
|
-
```typescript
|
|
50
|
-
import { createRuntimeAdapter } from "@soda-gql/runtime";
|
|
51
|
-
|
|
52
|
-
export const adapter = createRuntimeAdapter(({ type }) => ({
|
|
53
|
-
// Define the shape of non-GraphQL errors (network errors, etc.)
|
|
54
|
-
nonGraphqlErrorType: type<{ type: "non-graphql-error"; cause: unknown }>(),
|
|
55
|
-
}));
|
|
43
|
+
export const userQuery = gqlRuntime.getOperation("canonicalId");
|
|
56
44
|
```
|
|
57
45
|
|
|
58
46
|
## API Reference
|
|
@@ -64,32 +52,8 @@ The main runtime object for operation management:
|
|
|
64
52
|
```typescript
|
|
65
53
|
import { gqlRuntime } from "@soda-gql/runtime";
|
|
66
54
|
|
|
67
|
-
// Get
|
|
68
|
-
const operation = gqlRuntime.
|
|
69
|
-
|
|
70
|
-
// Get an inline operation
|
|
71
|
-
const inlineOp = gqlRuntime.getInlineOperation("canonicalId");
|
|
72
|
-
|
|
73
|
-
// Get a model
|
|
74
|
-
const model = gqlRuntime.getModel("canonicalId");
|
|
75
|
-
|
|
76
|
-
// Get a slice
|
|
77
|
-
const slice = gqlRuntime.getSlice("canonicalId");
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
### `createRuntimeAdapter(options)`
|
|
81
|
-
|
|
82
|
-
Create a custom runtime adapter for client integration:
|
|
83
|
-
|
|
84
|
-
```typescript
|
|
85
|
-
import { createRuntimeAdapter } from "@soda-gql/runtime";
|
|
86
|
-
|
|
87
|
-
const adapter = createRuntimeAdapter({
|
|
88
|
-
errorTypes: {
|
|
89
|
-
network: (message, cause) => new NetworkError(message, cause),
|
|
90
|
-
unknown: (message, cause) => new UnknownError(message, cause),
|
|
91
|
-
},
|
|
92
|
-
});
|
|
55
|
+
// Get an operation by canonical ID
|
|
56
|
+
const operation = gqlRuntime.getOperation("path/to/file.ts::userQuery");
|
|
93
57
|
```
|
|
94
58
|
|
|
95
59
|
## Zero-Runtime Philosophy
|
package/dist/index.cjs
CHANGED
|
@@ -1,317 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
metadata: input.prebuild.metadata
|
|
250
|
-
};
|
|
251
|
-
registerComposedOperation(operation);
|
|
252
|
-
return operation;
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
//#endregion
|
|
256
|
-
//#region packages/core/src/utils/hidden.ts
|
|
257
|
-
const _dummy = () => {
|
|
258
|
-
throw new Error("DO NOT CALL THIS FUNCTION -- we use function to safely transfer type information");
|
|
259
|
-
};
|
|
260
|
-
const hidden = () => _dummy;
|
|
261
|
-
|
|
262
|
-
//#endregion
|
|
263
|
-
//#region packages/core/src/runtime/inline-operation.ts
|
|
264
|
-
const createRuntimeInlineOperation = (input) => {
|
|
265
|
-
const operation = {
|
|
266
|
-
operationType: input.prebuild.operationType,
|
|
267
|
-
operationName: input.prebuild.operationName,
|
|
268
|
-
variableNames: input.prebuild.variableNames,
|
|
269
|
-
documentSource: hidden(),
|
|
270
|
-
document: input.prebuild.document,
|
|
271
|
-
metadata: input.prebuild.metadata
|
|
272
|
-
};
|
|
273
|
-
registerInlineOperation(operation);
|
|
274
|
-
return operation;
|
|
275
|
-
};
|
|
276
|
-
|
|
277
|
-
//#endregion
|
|
278
|
-
//#region packages/core/src/runtime/model.ts
|
|
279
|
-
const createRuntimeModel = (input) => ({
|
|
280
|
-
typename: input.prebuild.typename,
|
|
281
|
-
fragment: hidden(),
|
|
282
|
-
normalize: input.runtime.normalize
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
//#endregion
|
|
286
|
-
//#region packages/core/src/runtime/slice.ts
|
|
287
|
-
const handleProjectionBuilder = (projectionBuilder) => projectionBuilder({ select: (path, projector) => new Projection(path, projector) });
|
|
288
|
-
const createRuntimeSlice = (input) => {
|
|
289
|
-
const projection = handleProjectionBuilder(input.runtime.buildProjection);
|
|
290
|
-
return {
|
|
291
|
-
operationType: input.prebuild.operationType,
|
|
292
|
-
embed: (variables) => ({
|
|
293
|
-
variables,
|
|
294
|
-
getFields: hidden(),
|
|
295
|
-
projection
|
|
296
|
-
})
|
|
297
|
-
};
|
|
298
|
-
};
|
|
299
|
-
|
|
300
|
-
//#endregion
|
|
301
|
-
//#region packages/core/src/runtime/runtime-adapter.ts
|
|
302
|
-
const createRuntimeAdapter = (factory) => factory({ type: hidden });
|
|
303
|
-
|
|
304
|
-
//#endregion
|
|
305
|
-
//#region packages/core/src/runtime/index.ts
|
|
306
|
-
const gqlRuntime = {
|
|
307
|
-
model: createRuntimeModel,
|
|
308
|
-
composedOperation: createRuntimeComposedOperation,
|
|
309
|
-
inlineOperation: createRuntimeInlineOperation,
|
|
310
|
-
slice: createRuntimeSlice,
|
|
311
|
-
getComposedOperation,
|
|
312
|
-
getInlineOperation
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
//#endregion
|
|
316
|
-
exports.createRuntimeAdapter = createRuntimeAdapter;
|
|
317
|
-
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
|
+
});
|