@soda-gql/runtime 0.0.1
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/LICENSE +21 -0
- package/dist/index.cjs +308 -0
- package/dist/index.d.cts +601 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +601 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +307 -0
- package/dist/index.js.map +1 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Shota Hatada
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
|
|
2
|
+
//#region packages/core/src/types/fragment/var-ref.ts
|
|
3
|
+
/** Nominal reference used to defer variable binding while carrying type info. */
|
|
4
|
+
var VarRef = class VarRef {
|
|
5
|
+
constructor(name) {
|
|
6
|
+
this.name = name;
|
|
7
|
+
}
|
|
8
|
+
static create(name) {
|
|
9
|
+
return new VarRef(name);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region packages/core/src/utils/map-values.ts
|
|
15
|
+
const mapValues = (obj, fn) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, fn(value, key)]));
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region packages/core/src/composer/input.ts
|
|
19
|
+
const createVarRefs = (definitions) => mapValues(definitions, (_ref, name) => VarRef.create(name));
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region packages/core/src/types/runtime/projection.ts
|
|
23
|
+
/**
|
|
24
|
+
* Nominal type representing any slice selection regardless of schema specifics.
|
|
25
|
+
* Encodes how individual slices map a concrete field path to a projection
|
|
26
|
+
* function. Multiple selections allow slices to expose several derived values.
|
|
27
|
+
*/
|
|
28
|
+
var Projection = class {
|
|
29
|
+
constructor(paths, projector) {
|
|
30
|
+
this.projector = projector;
|
|
31
|
+
this.paths = paths.map((path) => createProjectionPath(path));
|
|
32
|
+
}
|
|
33
|
+
paths;
|
|
34
|
+
};
|
|
35
|
+
function createProjectionPath(path) {
|
|
36
|
+
const segments = path.split(".");
|
|
37
|
+
if (path === "$" || segments.length <= 1) throw new Error("Field path must not be only $ or empty");
|
|
38
|
+
return {
|
|
39
|
+
full: path,
|
|
40
|
+
segments: segments.slice(1)
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region packages/core/src/types/runtime/sliced-execution-result.ts
|
|
46
|
+
/** Runtime guard interface shared by all slice result variants. */
|
|
47
|
+
var SlicedExecutionResultGuards = class {
|
|
48
|
+
isSuccess() {
|
|
49
|
+
return this.type === "success";
|
|
50
|
+
}
|
|
51
|
+
isError() {
|
|
52
|
+
return this.type === "error";
|
|
53
|
+
}
|
|
54
|
+
isEmpty() {
|
|
55
|
+
return this.type === "empty";
|
|
56
|
+
}
|
|
57
|
+
constructor(type) {
|
|
58
|
+
this.type = type;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
/** Variant representing an empty payload (no data, no error). */
|
|
62
|
+
var SlicedExecutionResultEmpty = class extends SlicedExecutionResultGuards {
|
|
63
|
+
constructor() {
|
|
64
|
+
super("empty");
|
|
65
|
+
}
|
|
66
|
+
unwrap() {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
safeUnwrap() {
|
|
70
|
+
return {
|
|
71
|
+
data: void 0,
|
|
72
|
+
error: void 0
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
/** Variant representing a successful payload. */
|
|
77
|
+
var SlicedExecutionResultSuccess = class extends SlicedExecutionResultGuards {
|
|
78
|
+
constructor(data, extensions) {
|
|
79
|
+
super("success");
|
|
80
|
+
this.data = data;
|
|
81
|
+
this.extensions = extensions;
|
|
82
|
+
}
|
|
83
|
+
unwrap() {
|
|
84
|
+
return this.data;
|
|
85
|
+
}
|
|
86
|
+
safeUnwrap(transform) {
|
|
87
|
+
return {
|
|
88
|
+
data: transform(this.data),
|
|
89
|
+
error: void 0
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
/** Variant representing an error payload created by the adapter. */
|
|
94
|
+
var SlicedExecutionResultError = class extends SlicedExecutionResultGuards {
|
|
95
|
+
constructor(error, extensions) {
|
|
96
|
+
super("error");
|
|
97
|
+
this.error = error;
|
|
98
|
+
this.extensions = extensions;
|
|
99
|
+
}
|
|
100
|
+
unwrap() {
|
|
101
|
+
throw this.error;
|
|
102
|
+
}
|
|
103
|
+
safeUnwrap() {
|
|
104
|
+
return {
|
|
105
|
+
data: void 0,
|
|
106
|
+
error: this.error
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region packages/core/src/runtime/parse-execution-result.ts
|
|
113
|
+
function* generateErrorMapEntries(errors, projectionPathGraph) {
|
|
114
|
+
for (const error of errors) {
|
|
115
|
+
const errorPath = error.path ?? [];
|
|
116
|
+
let stack = projectionPathGraph;
|
|
117
|
+
for (let i = 0; i <= errorPath.length; i++) {
|
|
118
|
+
const segment = errorPath[i];
|
|
119
|
+
if (segment == null || typeof segment === "number") {
|
|
120
|
+
yield* stack.matches.map(({ label, path }) => ({
|
|
121
|
+
label,
|
|
122
|
+
path,
|
|
123
|
+
error
|
|
124
|
+
}));
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
yield* stack.matches.filter(({ exact }) => exact).map(({ label, path }) => ({
|
|
128
|
+
label,
|
|
129
|
+
path,
|
|
130
|
+
error
|
|
131
|
+
}));
|
|
132
|
+
const next = stack.children[segment];
|
|
133
|
+
if (!next) break;
|
|
134
|
+
stack = next;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const createErrorMaps = (errors, projectionPathGraph) => {
|
|
139
|
+
const errorMaps = {};
|
|
140
|
+
for (const { label, path, error } of generateErrorMapEntries(errors ?? [], projectionPathGraph)) {
|
|
141
|
+
const mapPerLabel = errorMaps[label] || (errorMaps[label] = {});
|
|
142
|
+
(mapPerLabel[path] || (mapPerLabel[path] = [])).push({ error });
|
|
143
|
+
}
|
|
144
|
+
return errorMaps;
|
|
145
|
+
};
|
|
146
|
+
const accessDataByPathSegments = (data, pathSegments) => {
|
|
147
|
+
let current = data;
|
|
148
|
+
for (const segment of pathSegments) {
|
|
149
|
+
if (current == null) return { error: /* @__PURE__ */ new Error("No data") };
|
|
150
|
+
if (typeof current !== "object") return { error: /* @__PURE__ */ new Error("Incorrect data type") };
|
|
151
|
+
if (Array.isArray(current)) return { error: /* @__PURE__ */ new Error("Incorrect data type") };
|
|
152
|
+
current = current[segment];
|
|
153
|
+
}
|
|
154
|
+
return { data: current };
|
|
155
|
+
};
|
|
156
|
+
const createExecutionResultParser = ({ fragments, projectionPathGraph }) => {
|
|
157
|
+
const prepare = (result) => {
|
|
158
|
+
if (result.type === "graphql") {
|
|
159
|
+
const errorMaps = createErrorMaps(result.body.errors, projectionPathGraph);
|
|
160
|
+
return {
|
|
161
|
+
...result,
|
|
162
|
+
errorMaps
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
if (result.type === "non-graphql-error") return {
|
|
166
|
+
...result,
|
|
167
|
+
error: new SlicedExecutionResultError({
|
|
168
|
+
type: "non-graphql-error",
|
|
169
|
+
error: result.error
|
|
170
|
+
})
|
|
171
|
+
};
|
|
172
|
+
if (result.type === "empty") return {
|
|
173
|
+
...result,
|
|
174
|
+
error: new SlicedExecutionResultEmpty()
|
|
175
|
+
};
|
|
176
|
+
throw new Error("Invalid result type", { cause: result });
|
|
177
|
+
};
|
|
178
|
+
return (result) => {
|
|
179
|
+
const prepared = prepare(result);
|
|
180
|
+
const entries = Object.entries(fragments).map(([label, fragment]) => {
|
|
181
|
+
const { projection } = fragment;
|
|
182
|
+
if (prepared.type === "graphql") {
|
|
183
|
+
const matchedErrors = projection.paths.flatMap(({ full: raw }) => prepared.errorMaps[label]?.[raw] ?? []);
|
|
184
|
+
const uniqueErrors = Array.from(new Set(matchedErrors.map(({ error }) => error)).values());
|
|
185
|
+
if (uniqueErrors.length > 0) return [label, projection.projector(new SlicedExecutionResultError({
|
|
186
|
+
type: "graphql-error",
|
|
187
|
+
errors: uniqueErrors
|
|
188
|
+
}))];
|
|
189
|
+
const dataResults = projection.paths.map(({ segments }) => prepared.body.data ? accessDataByPathSegments(prepared.body.data, segments) : { error: /* @__PURE__ */ new Error("No data") });
|
|
190
|
+
if (dataResults.some(({ error }) => error)) {
|
|
191
|
+
const errors = dataResults.flatMap(({ error }) => error ? [error] : []);
|
|
192
|
+
return [label, projection.projector(new SlicedExecutionResultError({
|
|
193
|
+
type: "parse-error",
|
|
194
|
+
errors
|
|
195
|
+
}))];
|
|
196
|
+
}
|
|
197
|
+
const dataList = dataResults.map(({ data }) => data);
|
|
198
|
+
return [label, projection.projector(new SlicedExecutionResultSuccess(dataList))];
|
|
199
|
+
}
|
|
200
|
+
if (prepared.type === "non-graphql-error") return [label, projection.projector(prepared.error)];
|
|
201
|
+
if (prepared.type === "empty") return [label, projection.projector(prepared.error)];
|
|
202
|
+
throw new Error("Invalid result type", { cause: prepared });
|
|
203
|
+
});
|
|
204
|
+
return Object.fromEntries(entries);
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
//#endregion
|
|
209
|
+
//#region packages/core/src/runtime/runtime-registry.ts
|
|
210
|
+
const composedOperationRegistry = /* @__PURE__ */ new Map();
|
|
211
|
+
const inlineOperationRegistry = /* @__PURE__ */ new Map();
|
|
212
|
+
const registerComposedOperation = (operation) => {
|
|
213
|
+
composedOperationRegistry.set(operation.operationName, operation);
|
|
214
|
+
};
|
|
215
|
+
const registerInlineOperation = (operation) => {
|
|
216
|
+
inlineOperationRegistry.set(operation.operationName, operation);
|
|
217
|
+
};
|
|
218
|
+
const getComposedOperation = (name) => {
|
|
219
|
+
const operation = composedOperationRegistry.get(name);
|
|
220
|
+
if (!operation) throw new Error(`Operation ${name} not found`);
|
|
221
|
+
return operation;
|
|
222
|
+
};
|
|
223
|
+
const getInlineOperation = (name) => {
|
|
224
|
+
const operation = inlineOperationRegistry.get(name);
|
|
225
|
+
if (!operation) throw new Error(`Operation ${name} not found`);
|
|
226
|
+
return operation;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
//#endregion
|
|
230
|
+
//#region packages/core/src/runtime/composed-operation.ts
|
|
231
|
+
const createRuntimeComposedOperation = (input) => {
|
|
232
|
+
const operation = {
|
|
233
|
+
operationType: input.prebuild.operationType,
|
|
234
|
+
operationName: input.prebuild.operationName,
|
|
235
|
+
variableNames: input.prebuild.variableNames,
|
|
236
|
+
projectionPathGraph: input.prebuild.projectionPathGraph,
|
|
237
|
+
document: input.prebuild.document,
|
|
238
|
+
parse: createExecutionResultParser({
|
|
239
|
+
fragments: input.runtime.getSlices({ $: createVarRefs(Object.fromEntries(input.prebuild.variableNames.map((name) => [name, null]))) }),
|
|
240
|
+
projectionPathGraph: input.prebuild.projectionPathGraph
|
|
241
|
+
})
|
|
242
|
+
};
|
|
243
|
+
registerComposedOperation(operation);
|
|
244
|
+
return operation;
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
//#endregion
|
|
248
|
+
//#region packages/core/src/utils/hidden.ts
|
|
249
|
+
const _dummy = () => {
|
|
250
|
+
throw new Error("DO NOT CALL THIS FUNCTION -- we use function to safely transfer type information");
|
|
251
|
+
};
|
|
252
|
+
const hidden = () => _dummy;
|
|
253
|
+
|
|
254
|
+
//#endregion
|
|
255
|
+
//#region packages/core/src/runtime/inline-operation.ts
|
|
256
|
+
const createRuntimeInlineOperation = (input) => {
|
|
257
|
+
const operation = {
|
|
258
|
+
operationType: input.prebuild.operationType,
|
|
259
|
+
operationName: input.prebuild.operationName,
|
|
260
|
+
variableNames: input.prebuild.variableNames,
|
|
261
|
+
documentSource: hidden(),
|
|
262
|
+
document: input.prebuild.document
|
|
263
|
+
};
|
|
264
|
+
registerInlineOperation(operation);
|
|
265
|
+
return operation;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
//#endregion
|
|
269
|
+
//#region packages/core/src/runtime/model.ts
|
|
270
|
+
const createRuntimeModel = (input) => ({
|
|
271
|
+
typename: input.prebuild.typename,
|
|
272
|
+
fragment: hidden(),
|
|
273
|
+
normalize: input.runtime.normalize
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
//#endregion
|
|
277
|
+
//#region packages/core/src/runtime/slice.ts
|
|
278
|
+
const handleProjectionBuilder = (projectionBuilder) => projectionBuilder({ select: (path, projector) => new Projection(path, projector) });
|
|
279
|
+
const createRuntimeSlice = (input) => {
|
|
280
|
+
const projection = handleProjectionBuilder(input.runtime.buildProjection);
|
|
281
|
+
return {
|
|
282
|
+
operationType: input.prebuild.operationType,
|
|
283
|
+
embed: (variables) => ({
|
|
284
|
+
variables,
|
|
285
|
+
getFields: hidden(),
|
|
286
|
+
projection
|
|
287
|
+
})
|
|
288
|
+
};
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
//#endregion
|
|
292
|
+
//#region packages/core/src/runtime/runtime-adapter.ts
|
|
293
|
+
const createRuntimeAdapter = (factory) => factory({ type: hidden });
|
|
294
|
+
|
|
295
|
+
//#endregion
|
|
296
|
+
//#region packages/core/src/runtime/index.ts
|
|
297
|
+
const gqlRuntime = {
|
|
298
|
+
model: createRuntimeModel,
|
|
299
|
+
composedOperation: createRuntimeComposedOperation,
|
|
300
|
+
inlineOperation: createRuntimeInlineOperation,
|
|
301
|
+
slice: createRuntimeSlice,
|
|
302
|
+
getComposedOperation,
|
|
303
|
+
getInlineOperation
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
//#endregion
|
|
307
|
+
exports.createRuntimeAdapter = createRuntimeAdapter;
|
|
308
|
+
exports.gqlRuntime = gqlRuntime;
|