orchid-graphql 1.9.0 → 2.0.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 +3 -2
- package/dist/index.cjs +345 -492
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +144 -131
- package/dist/index.d.mts +205 -0
- package/dist/index.mjs +360 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +27 -18
- package/src/orm/orm.ts +34 -52
- package/LICENSE +0 -21
- package/dist/index.d.ts +0 -192
- package/dist/index.js +0 -481
- package/dist/index.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,520 +1,373 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
createGraphResolver: () => createGraphResolver,
|
|
24
|
-
cursor: () => defineCursorPaginator,
|
|
25
|
-
defineCursorPaginator: () => defineCursorPaginator,
|
|
26
|
-
defineFieldResolver: () => defineFieldResolver,
|
|
27
|
-
definePageResolver: () => definePageResolver,
|
|
28
|
-
defineRelationResolver: () => defineRelationResolver,
|
|
29
|
-
defineTableResolver: () => defineTableResolver,
|
|
30
|
-
field: () => defineFieldResolver,
|
|
31
|
-
filterQuery: () => filterQuery2,
|
|
32
|
-
graph: () => createGraphResolver,
|
|
33
|
-
page: () => definePageResolver,
|
|
34
|
-
relation: () => defineRelationResolver,
|
|
35
|
-
table: () => defineTableResolver
|
|
36
|
-
});
|
|
37
|
-
module.exports = __toCommonJS(index_exports);
|
|
38
|
-
|
|
39
|
-
// ../base/src/utils/is-plain-object.ts
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let graphql_parse_resolve_info = require("graphql-parse-resolve-info");
|
|
3
|
+
let orchid_pagination = require("orchid-pagination");
|
|
4
|
+
//#region ../base/src/utils/is-plain-object.ts
|
|
40
5
|
function isPlainObject(obj) {
|
|
41
|
-
|
|
6
|
+
return typeof obj === "object" && !Array.isArray(obj);
|
|
42
7
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
function filterQuery(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
} else {
|
|
60
|
-
const [table_field, op0] = field.split("__");
|
|
61
|
-
const op = op0?.toLowerCase();
|
|
62
|
-
query = orm2.where(query, table_field, op, value);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return query;
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region ../base/src/filter/filter.ts
|
|
10
|
+
function filterQuery$1(orm, query, filter, options) {
|
|
11
|
+
const { context, modifiers } = options ?? {};
|
|
12
|
+
if (!filter) return query;
|
|
13
|
+
if (!isPlainObject(filter)) throw new Error(`Invalid filter: ${filter}, must be plain object.`);
|
|
14
|
+
for (const [field, value] of Object.entries(filter)) {
|
|
15
|
+
if (value === void 0) continue;
|
|
16
|
+
if (modifiers === null || modifiers === void 0 ? void 0 : modifiers[field]) query = modifiers[field](query, value, context);
|
|
17
|
+
else {
|
|
18
|
+
const [tableField, op0] = field.split("__");
|
|
19
|
+
const op = op0 === null || op0 === void 0 ? void 0 : op0.toLowerCase();
|
|
20
|
+
query = orm.where(query, tableField, op, value);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return query;
|
|
66
24
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
var import_node_buffer = require("buffer");
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region ../base/src/paginators/cursor.ts
|
|
70
27
|
function defineCursorPaginator(options = {}) {
|
|
71
|
-
|
|
28
|
+
return new CursorPaginator(options);
|
|
72
29
|
}
|
|
73
30
|
var CursorPaginator = class {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (!orderFields.length) {
|
|
104
|
-
throw new Error("Query must be ordered.");
|
|
105
|
-
}
|
|
106
|
-
for (const { field, alias } of orderFields) {
|
|
107
|
-
query = orm2.select_field(query, { field, as: alias });
|
|
108
|
-
}
|
|
109
|
-
if (cursor) {
|
|
110
|
-
const parsedCursor = parseCursor(cursor);
|
|
111
|
-
const left = [];
|
|
112
|
-
const right = [];
|
|
113
|
-
for (let i = 0; i < orderFields.length; ++i) {
|
|
114
|
-
const { field, alias, dir } = orderFields[i];
|
|
115
|
-
const [expressions, placeholders] = dir === "ASC" ? [left, right] : [right, left];
|
|
116
|
-
expressions.push(`"${table}"."${field}"`);
|
|
117
|
-
placeholders.push("$" + alias);
|
|
118
|
-
}
|
|
119
|
-
const sqlExpr = `(${left.join(",")}) > (${right.join(",")})`;
|
|
120
|
-
const bindings = Object.fromEntries(
|
|
121
|
-
orderFields.map(({ alias }, i) => [alias, parsedCursor[i]])
|
|
122
|
-
);
|
|
123
|
-
query = orm2.where_raw(query, sqlExpr, bindings);
|
|
124
|
-
}
|
|
125
|
-
query = orm2.set_query_limit(query, pageSize + 1);
|
|
126
|
-
function createNodeCursor(node) {
|
|
127
|
-
return createCursor(
|
|
128
|
-
orderFields.map(({ field, alias }) => {
|
|
129
|
-
const value = node[alias];
|
|
130
|
-
if (value === void 0) {
|
|
131
|
-
throw new Error(
|
|
132
|
-
`Unable to create cursor: undefined field ${field} (${alias})`
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
return String(value);
|
|
136
|
-
})
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
return orm2.set_query_page_result(query, (nodes) => {
|
|
140
|
-
let cursor2;
|
|
141
|
-
if (nodes.length > pageSize) {
|
|
142
|
-
cursor2 = createNodeCursor(nodes[pageSize - 1]);
|
|
143
|
-
nodes = nodes.slice(0, pageSize);
|
|
144
|
-
}
|
|
145
|
-
return { nodes, cursor: cursor2 };
|
|
146
|
-
});
|
|
147
|
-
}
|
|
31
|
+
path = ["nodes"];
|
|
32
|
+
pageSize;
|
|
33
|
+
fields;
|
|
34
|
+
constructor(options = {}) {
|
|
35
|
+
var _options$fields;
|
|
36
|
+
this.pageSize = options.take ?? 10;
|
|
37
|
+
this.fields = (_options$fields = options.fields) === null || _options$fields === void 0 ? void 0 : _options$fields.map((field) => {
|
|
38
|
+
if (field.startsWith("-")) return {
|
|
39
|
+
field: field.slice(1),
|
|
40
|
+
dir: "DESC"
|
|
41
|
+
};
|
|
42
|
+
else return {
|
|
43
|
+
field,
|
|
44
|
+
dir: "ASC"
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
paginate(query, context) {
|
|
49
|
+
const { orm } = context.graph;
|
|
50
|
+
const { args } = context.tree;
|
|
51
|
+
const pageSize = args.take ?? this.pageSize;
|
|
52
|
+
const cursor = args.cursor;
|
|
53
|
+
const prepared = orm.prepareCursorPagination(query, {
|
|
54
|
+
cursor,
|
|
55
|
+
fields: this.fields,
|
|
56
|
+
limit: pageSize
|
|
57
|
+
});
|
|
58
|
+
return orm.setQueryPageResult(prepared.query, prepared.getPage);
|
|
59
|
+
}
|
|
148
60
|
};
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region ../base/src/utils/run-after.ts
|
|
63
|
+
function runAfterQuery(orm, query, transform) {
|
|
64
|
+
return orm.runAfterQuery(query, async (result) => {
|
|
65
|
+
if (Array.isArray(result)) return (await Promise.all(result.map(transform))).filter(Boolean);
|
|
66
|
+
else if (result) return transform(result);
|
|
67
|
+
else return result;
|
|
68
|
+
});
|
|
156
69
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
function
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
} else {
|
|
167
|
-
return result;
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// ../base/src/resolvers/field.ts
|
|
173
|
-
function parse_field_options(options) {
|
|
174
|
-
const { tableField, modelField } = options;
|
|
175
|
-
if (tableField && modelField) {
|
|
176
|
-
throw new Error(
|
|
177
|
-
`Both tableField and modelField are defined. Use only one of them.`
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
return { ...options, tableField: tableField || modelField };
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region ../base/src/resolvers/field.ts
|
|
72
|
+
function parseFieldOptions(options) {
|
|
73
|
+
const { tableField, modelField } = options;
|
|
74
|
+
if (tableField && modelField) throw new Error(`Both tableField and modelField are defined. Use only one of them.`);
|
|
75
|
+
return {
|
|
76
|
+
...options,
|
|
77
|
+
tableField: tableField || modelField
|
|
78
|
+
};
|
|
181
79
|
}
|
|
182
80
|
function defineFieldResolver(options = {}) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
return instance;
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
return query;
|
|
201
|
-
};
|
|
81
|
+
const { tableField, modify, transform } = parseFieldOptions(options);
|
|
82
|
+
return function resolve(query, context) {
|
|
83
|
+
const { field } = context;
|
|
84
|
+
if (modify) query = modify(query, context);
|
|
85
|
+
else query = context.graph.orm.selectField(query, {
|
|
86
|
+
field: tableField || field,
|
|
87
|
+
as: field
|
|
88
|
+
});
|
|
89
|
+
if (transform) query = runAfterQuery(context.graph.orm, query, async (instance) => {
|
|
90
|
+
instance[field] = await transform(instance[field], instance, context);
|
|
91
|
+
return instance;
|
|
92
|
+
});
|
|
93
|
+
return query;
|
|
94
|
+
};
|
|
202
95
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
var import_graphql_parse_resolve_info = require("graphql-parse-resolve-info");
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region ../base/src/resolvers/graph.ts
|
|
206
98
|
var GraphResolver = class {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
99
|
+
orm;
|
|
100
|
+
typeResolvers;
|
|
101
|
+
options;
|
|
102
|
+
constructor(orm, typeResolvers, options = {}) {
|
|
103
|
+
this.orm = orm;
|
|
104
|
+
this.typeResolvers = typeResolvers;
|
|
105
|
+
this.options = options;
|
|
106
|
+
}
|
|
107
|
+
resolve(query, { info, ...context }) {
|
|
108
|
+
const tree = this._getResolveTree(info);
|
|
109
|
+
return this._resolveType(query, {
|
|
110
|
+
...context,
|
|
111
|
+
tree
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
resolvePage(query, paginator, { info, ...context }) {
|
|
115
|
+
const tree = this._getResolveTree(info);
|
|
116
|
+
return this._resolvePage(query, paginator, {
|
|
117
|
+
...context,
|
|
118
|
+
tree
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
_getResolveTree(info) {
|
|
122
|
+
return (0, graphql_parse_resolve_info.parseResolveInfo)(info);
|
|
123
|
+
}
|
|
124
|
+
_resolveType(query, context) {
|
|
125
|
+
let { tree } = context;
|
|
126
|
+
if (context.path) for (const field of context.path) {
|
|
127
|
+
const type = Object.keys(tree.fieldsByTypeName)[0];
|
|
128
|
+
tree = tree.fieldsByTypeName[type][field];
|
|
129
|
+
if (!tree) return Promise.resolve();
|
|
130
|
+
}
|
|
131
|
+
const type = Object.keys(tree.fieldsByTypeName)[0];
|
|
132
|
+
const typeResolver = this.typeResolvers[type];
|
|
133
|
+
if (!typeResolver) throw new Error(`Resolver not found for type ${type}.`);
|
|
134
|
+
return typeResolver.resolve(query, {
|
|
135
|
+
...context,
|
|
136
|
+
graph: this,
|
|
137
|
+
tree,
|
|
138
|
+
type,
|
|
139
|
+
path: void 0
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
_resolvePage(query, paginator, context) {
|
|
143
|
+
if (context.path) throw new Error("Paginating under non-root path not yet supported.");
|
|
144
|
+
query = this._resolveType(query, {
|
|
145
|
+
...context,
|
|
146
|
+
path: paginator.path
|
|
147
|
+
});
|
|
148
|
+
return paginator.paginate(query, {
|
|
149
|
+
...context,
|
|
150
|
+
graph: this
|
|
151
|
+
});
|
|
152
|
+
}
|
|
257
153
|
};
|
|
258
|
-
|
|
259
|
-
|
|
154
|
+
//#endregion
|
|
155
|
+
//#region ../base/src/resolvers/page.ts
|
|
260
156
|
function definePageResolver(paginator, options = {}) {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
query = graph.orm.finish_query_pagination(query, field, pagination_context);
|
|
279
|
-
return query;
|
|
280
|
-
};
|
|
157
|
+
const { tableField, modify } = parseFieldOptions(options);
|
|
158
|
+
return function resolve(query, context) {
|
|
159
|
+
const { graph, field } = context;
|
|
160
|
+
const paginationContext = {};
|
|
161
|
+
query = graph.orm.selectRelation(query, {
|
|
162
|
+
relation: tableField || field,
|
|
163
|
+
as: field,
|
|
164
|
+
modify(subquery) {
|
|
165
|
+
subquery = modify ? modify(subquery, context) : subquery;
|
|
166
|
+
subquery = graph._resolvePage(subquery, paginator, context);
|
|
167
|
+
subquery = graph.orm.modifySubqueryPagination(subquery, paginationContext);
|
|
168
|
+
return subquery;
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
query = graph.orm.finishQueryPagination(query, field, paginationContext);
|
|
172
|
+
return query;
|
|
173
|
+
};
|
|
281
174
|
}
|
|
282
|
-
|
|
283
|
-
|
|
175
|
+
//#endregion
|
|
176
|
+
//#region ../base/src/resolvers/relation.ts
|
|
284
177
|
function defineRelationResolver(options = {}) {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
178
|
+
const { tableField, filters, modify } = parseFieldOptions(options);
|
|
179
|
+
return function resolve(query, context) {
|
|
180
|
+
const { graph, field } = context;
|
|
181
|
+
return graph.orm.selectRelation(query, {
|
|
182
|
+
relation: tableField || field,
|
|
183
|
+
as: field,
|
|
184
|
+
modify(subquery) {
|
|
185
|
+
subquery = graph._resolveType(subquery, {
|
|
186
|
+
...context,
|
|
187
|
+
filters
|
|
188
|
+
});
|
|
189
|
+
return modify ? modify(subquery, context) : subquery;
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
};
|
|
297
193
|
}
|
|
298
|
-
|
|
299
|
-
|
|
194
|
+
//#endregion
|
|
195
|
+
//#region ../base/src/resolvers/table.ts
|
|
300
196
|
var TableResolver = class {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
query = this.orm.prevent_select_all(query);
|
|
375
|
-
return query;
|
|
376
|
-
}
|
|
377
|
-
/**
|
|
378
|
-
* Create field resolver which will modify query to resolve GraphQL field.
|
|
379
|
-
*/
|
|
380
|
-
_get_default_field_resolver(field, tableField) {
|
|
381
|
-
const table_field_lookup = tableField || field;
|
|
382
|
-
if (this.options.modifiers?.[table_field_lookup] || this.virtual_fields.has(table_field_lookup)) {
|
|
383
|
-
return (query) => query;
|
|
384
|
-
} else if (this.relations.has(table_field_lookup)) {
|
|
385
|
-
return defineRelationResolver({ tableField });
|
|
386
|
-
} else {
|
|
387
|
-
return defineFieldResolver({ tableField });
|
|
388
|
-
}
|
|
389
|
-
}
|
|
197
|
+
orm;
|
|
198
|
+
table;
|
|
199
|
+
options;
|
|
200
|
+
relations;
|
|
201
|
+
virtualFields;
|
|
202
|
+
modifiers;
|
|
203
|
+
tableFieldResolvers;
|
|
204
|
+
constructor(orm, table, options = {}) {
|
|
205
|
+
this.orm = orm;
|
|
206
|
+
this.table = table;
|
|
207
|
+
this.options = options;
|
|
208
|
+
this.relations = new Set(orm.getTableRelations(table));
|
|
209
|
+
this.virtualFields = new Set(orm.getTableVirtualFields(table));
|
|
210
|
+
this.modifiers = {
|
|
211
|
+
...orm.getTableModifiers(table),
|
|
212
|
+
...options.modifiers
|
|
213
|
+
};
|
|
214
|
+
const { fields } = options;
|
|
215
|
+
if (fields) this.tableFieldResolvers = Object.keys(fields).reduce((resolvers, field) => {
|
|
216
|
+
const r0 = fields[field];
|
|
217
|
+
const r = typeof r0 === "function" ? r0 : r0 === true ? this._getDefaultFieldResolver(field) : typeof r0 === "string" ? this._getDefaultFieldResolver(field, r0) : void 0;
|
|
218
|
+
if (r === void 0) throw new Error(`Field resolver must be a function, string, or true; found ${typeof r0}.`);
|
|
219
|
+
resolvers[field] = r;
|
|
220
|
+
return resolvers;
|
|
221
|
+
}, {});
|
|
222
|
+
}
|
|
223
|
+
/** Modify query to select fields/relations and filter the result set. */
|
|
224
|
+
resolve(query, context) {
|
|
225
|
+
var _context$tree$args;
|
|
226
|
+
const { modify, transform } = this.options;
|
|
227
|
+
const { graph, tree, type, filters } = context;
|
|
228
|
+
const queryTable = this.orm.getQueryTable(query);
|
|
229
|
+
const tableName = this.orm.getTableName(this.table);
|
|
230
|
+
if (queryTable !== tableName) throw new Error(`Mismatching query table for type ${type}: expected ${tableName}, received ${queryTable}.`);
|
|
231
|
+
const allowAllFields = this.options.allowAllFields ?? graph.options.allowAllFields ?? !this.options.fields;
|
|
232
|
+
if (!allowAllFields && !this.tableFieldResolvers) throw new Error(`Resolver for type ${type} must either allow all fields or specify options.fields.`);
|
|
233
|
+
if (modify) query = modify(query, context);
|
|
234
|
+
for (const subtree of Object.values(tree.fieldsByTypeName[type])) {
|
|
235
|
+
var _this$tableFieldResol;
|
|
236
|
+
const field = subtree.name;
|
|
237
|
+
const r = ((_this$tableFieldResol = this.tableFieldResolvers) === null || _this$tableFieldResol === void 0 ? void 0 : _this$tableFieldResol[field]) || (allowAllFields ? this._getDefaultFieldResolver(field) : void 0);
|
|
238
|
+
if (!r) throw new Error(`No field resolver defined for field ${type}.${field}.`);
|
|
239
|
+
if (r) query = r(query, {
|
|
240
|
+
...context,
|
|
241
|
+
tree: subtree,
|
|
242
|
+
field
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
const filter = (_context$tree$args = context.tree.args) === null || _context$tree$args === void 0 ? void 0 : _context$tree$args.filter;
|
|
246
|
+
if (filter) {
|
|
247
|
+
if (this.options.allowAllFilters ?? graph.options.allowAllFilters ? true : filters) query = filterQuery$1(context.graph.orm, query, filter, {
|
|
248
|
+
modifiers: this.modifiers,
|
|
249
|
+
context
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
if (transform) query = runAfterQuery(this.orm, query, (instance) => {
|
|
253
|
+
return transform(instance, context);
|
|
254
|
+
});
|
|
255
|
+
query = this.orm.preventSelectAll(query);
|
|
256
|
+
return query;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Create field resolver which will modify query to resolve GraphQL field.
|
|
260
|
+
*/
|
|
261
|
+
_getDefaultFieldResolver(field, tableField) {
|
|
262
|
+
var _this$options$modifie;
|
|
263
|
+
const tableFieldLookup = tableField || field;
|
|
264
|
+
if (((_this$options$modifie = this.options.modifiers) === null || _this$options$modifie === void 0 ? void 0 : _this$options$modifie[tableFieldLookup]) || this.virtualFields.has(tableFieldLookup)) return (query) => query;
|
|
265
|
+
else if (this.relations.has(tableFieldLookup)) return defineRelationResolver({ tableField });
|
|
266
|
+
else return defineFieldResolver({ tableField });
|
|
267
|
+
}
|
|
390
268
|
};
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
});
|
|
453
|
-
} else {
|
|
454
|
-
throw new TypeError("Unsupported order type: " + orderItem);
|
|
455
|
-
}
|
|
456
|
-
}) || [];
|
|
457
|
-
},
|
|
458
|
-
set_query_limit(query, limit) {
|
|
459
|
-
return query.limit(limit);
|
|
460
|
-
},
|
|
461
|
-
// Pagination helpers
|
|
462
|
-
set_query_page_result(query, get_page) {
|
|
463
|
-
return query.transform((nodes) => get_page(nodes));
|
|
464
|
-
},
|
|
465
|
-
modify_subquery_pagination(subquery) {
|
|
466
|
-
return subquery;
|
|
467
|
-
},
|
|
468
|
-
finish_query_pagination(query) {
|
|
469
|
-
return query;
|
|
470
|
-
},
|
|
471
|
-
// Misc
|
|
472
|
-
run_after_query(query, fn) {
|
|
473
|
-
return query.afterQuery((data) => fn(data));
|
|
474
|
-
},
|
|
475
|
-
prevent_select_all(query) {
|
|
476
|
-
return query;
|
|
477
|
-
}
|
|
269
|
+
//#endregion
|
|
270
|
+
//#region src/orm/orm.ts
|
|
271
|
+
const orm = {
|
|
272
|
+
Table: void 0,
|
|
273
|
+
Query: void 0,
|
|
274
|
+
QueryTransform: void 0,
|
|
275
|
+
getTableName(table) {
|
|
276
|
+
return table.table;
|
|
277
|
+
},
|
|
278
|
+
getTableRelations(table) {
|
|
279
|
+
return Object.keys(table.relations);
|
|
280
|
+
},
|
|
281
|
+
getTableVirtualFields() {
|
|
282
|
+
return [];
|
|
283
|
+
},
|
|
284
|
+
getTableModifiers() {},
|
|
285
|
+
getQueryTable(query) {
|
|
286
|
+
if (!query.table) throw new Error("Query must have table.");
|
|
287
|
+
return query.table;
|
|
288
|
+
},
|
|
289
|
+
selectField(query, { field, as }) {
|
|
290
|
+
return query.select({ [as]: field });
|
|
291
|
+
},
|
|
292
|
+
selectRelation(query, { relation, as, modify }) {
|
|
293
|
+
return query.select({ [as]: (q) => modify(q[relation]) });
|
|
294
|
+
},
|
|
295
|
+
where(query, field, op, value) {
|
|
296
|
+
return query.where({ [field]: op ? { [op]: value } : value });
|
|
297
|
+
},
|
|
298
|
+
prepareCursorPagination(query, { cursor, fields, limit }) {
|
|
299
|
+
if (fields) {
|
|
300
|
+
query = query.clear("order");
|
|
301
|
+
for (const { field, dir } of fields) query = query.order({ [field]: dir });
|
|
302
|
+
}
|
|
303
|
+
const prepared = (0, orchid_pagination.prepareCursorPagination)(query, { limit }, { cursor });
|
|
304
|
+
return {
|
|
305
|
+
query: prepared.query,
|
|
306
|
+
getPage(nodes) {
|
|
307
|
+
const page = prepared.finalize(nodes);
|
|
308
|
+
return {
|
|
309
|
+
nodes: page.items,
|
|
310
|
+
cursor: page.nextCursor
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
},
|
|
315
|
+
setQueryPageResult(query, get_page) {
|
|
316
|
+
return query.transform((nodes) => get_page(nodes));
|
|
317
|
+
},
|
|
318
|
+
modifySubqueryPagination(subquery) {
|
|
319
|
+
return subquery;
|
|
320
|
+
},
|
|
321
|
+
finishQueryPagination(query) {
|
|
322
|
+
return query;
|
|
323
|
+
},
|
|
324
|
+
runAfterQuery(query, fn) {
|
|
325
|
+
return query.afterQuery((data) => fn(data));
|
|
326
|
+
},
|
|
327
|
+
preventSelectAll(query) {
|
|
328
|
+
return query;
|
|
329
|
+
}
|
|
478
330
|
};
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
function
|
|
482
|
-
|
|
331
|
+
//#endregion
|
|
332
|
+
//#region src/filter.ts
|
|
333
|
+
function filterQuery(query, filter, options) {
|
|
334
|
+
return filterQuery$1(orm, query, filter, options);
|
|
483
335
|
}
|
|
484
|
-
|
|
485
|
-
|
|
336
|
+
//#endregion
|
|
337
|
+
//#region src/resolvers/graph.ts
|
|
486
338
|
function createGraphResolver(types, options) {
|
|
487
|
-
|
|
339
|
+
return new OrchidGraphResolver(types, options);
|
|
488
340
|
}
|
|
489
341
|
var OrchidGraphResolver = class extends GraphResolver {
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
342
|
+
types;
|
|
343
|
+
options;
|
|
344
|
+
constructor(types, options = {}) {
|
|
345
|
+
super(orm, types, options);
|
|
346
|
+
this.types = types;
|
|
347
|
+
this.options = options;
|
|
348
|
+
}
|
|
349
|
+
resolve(query, options) {
|
|
350
|
+
return super.resolve(query, options);
|
|
351
|
+
}
|
|
498
352
|
};
|
|
499
|
-
|
|
500
|
-
|
|
353
|
+
//#endregion
|
|
354
|
+
//#region src/resolvers/table.ts
|
|
501
355
|
function defineTableResolver(table, options = {}) {
|
|
502
|
-
|
|
356
|
+
return new TableResolver(orm, table, options);
|
|
503
357
|
}
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
});
|
|
358
|
+
//#endregion
|
|
359
|
+
exports.createGraphResolver = createGraphResolver;
|
|
360
|
+
exports.cursor = defineCursorPaginator;
|
|
361
|
+
exports.defineCursorPaginator = defineCursorPaginator;
|
|
362
|
+
exports.defineFieldResolver = defineFieldResolver;
|
|
363
|
+
exports.definePageResolver = definePageResolver;
|
|
364
|
+
exports.defineRelationResolver = defineRelationResolver;
|
|
365
|
+
exports.defineTableResolver = defineTableResolver;
|
|
366
|
+
exports.field = defineFieldResolver;
|
|
367
|
+
exports.filterQuery = filterQuery;
|
|
368
|
+
exports.graph = createGraphResolver;
|
|
369
|
+
exports.page = definePageResolver;
|
|
370
|
+
exports.relation = defineRelationResolver;
|
|
371
|
+
exports.table = defineTableResolver;
|
|
372
|
+
|
|
520
373
|
//# sourceMappingURL=index.cjs.map
|