@simtlix/simfinity-core 3.6.0 → 3.7.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 +2 -0
- package/package.json +1 -1
- package/src/runtime.js +307 -8
- package/types/auth/index.d.ts +1 -0
- package/types/index.d.ts +58 -4
- package/types/plugins.d.ts +1 -1
package/README.md
CHANGED
|
@@ -18,6 +18,8 @@ For relational storage, `@simtlix/simfinity-sql` builds on core and delegates ph
|
|
|
18
18
|
|
|
19
19
|
The adapter supplies model creation, identifiers, transactions, record reads and writes, queries, counts, aggregations, and inverse collection reads. `DatabaseAdapter`, `Runtime`, controller, middleware, registration, and state-machine interfaces are exported in the TypeScript declarations. A runtime binds one adapter; create another runtime to use another database or configuration. An adapter can also define `getByIds(model, ids, { context })`, which returns the records it finds in `getById` shape and any order; the runtime then batches generated single-reference reads per request and type, matching records by `castId`, splitting batches at `maxPageSize`, and reading an ID with `getById` when a batch read fails or when `castId` would normalize or reject it. `castId` should throw a `NOT_VALID_ID` (400) `SimfinityError` for a malformed value and must not create a new identifier. An adapter can define `readByIdIsInert(model)`, returning `true` when a `getById` of that model has no side effects and `update()` and `delete()` reject the same malformed IDs with `NOT_VALID_ID` (400) and return null for a missing record; the runtime then skips the reads before deletes without an `onDelete` hook and before updates that merge no embedded value and have neither `onUpdating` nor nested collection items. The SQL adapter always reports it, and the MongoDB adapter does for models without `find`, `findOne`, `findOneAndUpdate` or `findOneAndDelete` middleware and without application `_id` setters or overrides of the statics those reads and writes call. When `getById` rejects a stored reference value that way, including one that `castId` rejected first, the generated reference field fails with an `InternalServerError` (500), since the client did not send the value. `update()` resolves to the updated record as stored, or null; nested collection children of that record are linked to and checked against its `_id`, else `id`, else the requested ID. An adapter that hydrates records can define `readEmbeddedValue(value)`: every nullable, singular embedded object field without a resolver then passes its value through it, so an explicitly stored `null` reads as `null`. The field is read as graphql's default resolver reads it, so a method on the parent is called and a promise it returns is awaited before the hook receives the value. Simfinity then reads as `null` an object that holds no data when a required member would read as `null`, not counting `id`, `readOnly`, interface and union members, members with an application resolver and referenced collections; runtimes that share the type read the same. It reads the stored members, never calling a getter, and counts a stored `_id` as `id`; it reads nothing from objects whose type has no member that counts, and an error while reading returns the object unchanged. An adapter whose hydrated records render members through getters can also define `rawEmbeddedValue(value)`, which returns a record's stored data as a plain object, or the record unchanged when that data does not show what it renders; a value that is not a plain object counts as data. The MongoDB adapter defines both.
|
|
20
20
|
|
|
21
|
+
An adapter can also define `findChildrenBatch(model, type, connectionField, parentIds, args, { context })`, the batch form of `findChildren` for generated collection fields, and a synchronous `batchesChildren(model, type, connectionField)` predicate. The runtime calls both as adapter methods and checks them on every read. When the method is missing or the predicate returns `false`, each field calls `findChildren` at once with the arguments object it built, as before. Otherwise, within one request (a context object), the runtime groups the collection reads of one related type and connection storage field whose effective arguments (after middleware, the scope and joined scopes) hold the same plain data and are requested in the same tick, that is before the promise jobs queued so far have run. It calls the method with 2 to 100 distinct parent IDs, each a raw ID that `castId` accepts without changing its string form, and with a copy of the arguments that the method may change; a group with one distinct parent calls `findChildren`. Return one array per parent ID, in order, each holding exactly the records, order and page that `findChildren` returns for that parent; records should show their parent under the connection field, or not show one. Fields of one parent share its records, each with its own array. After a rejection, or a result of another shape or with records of another parent, each field reads alone with `findChildren`; failures other than a `SimfinityError`, and results of another shape, are logged once per type. Pairing rule: the batch must reproduce `this.findChildren`, so `batchesChildren` must return `false` when `this.findChildren` is not the adapter's own function, as with a spread copy or an `Object.assign` override that changes how children are read. The bundled adapters follow this rule: both define the two members only with `batchedCollections: true`: the SQL adapter when the schema is prepared and its plugin's `compileChildrenQuery` is paired with its `compileQuery`, and the MongoDB adapter at creation. To opt out on one adapter instance, set `adapter.findChildrenBatch = undefined` (deleting it does nothing for a class whose method is on the prototype), or make `batchesChildren` return `false`. A copy that overrides `getById` still inherits the base `getByIds`, as before.
|
|
22
|
+
|
|
21
23
|
Entity types, those registered with `connect()` and the targets of non-embedded relations, resolve `id` to `_id`, falling back to `id`, whatever the `createSchema()` allowlists include; types used only as embedded values keep their declared `id`. Embedded copies of an entity type resolve the same way, so they return their declared `id` unless the adapter stores an `_id` for them, as MongoDB does for embedded list items. The resolver is set on the type object, so it applies in every runtime that shares the type. `createSchema()` marks these registrations with `storedIdentity: true` before calling `adapter.prepare()`.
|
|
22
24
|
|
|
23
25
|
When a runtime first creates a schema, it binds each GraphQL object type whose non-embedded relation fields receive its generated resolvers. It also reserves each reachable object type that still has a non-embedded relation field without a resolver, such as an unregistered custom mutation result. Registering a bound or reserved type in another runtime, or building another runtime's schema that reaches it through fields, interfaces, union members or custom mutation results, throws `TYPE_BOUND_TO_OTHER_RUNTIME` (409). `createSchema()` also rejects a type with a relation field copied, by `toConfig()` or a field spread, after another runtime generated that field's resolver. Create type objects once per runtime, for example with a factory function. Types without non-embedded relation fields, and types whose relation fields all have application resolvers, can be shared, and another runtime can use a `toConfig()` copy made before the first runtime built its schema. A shared custom mutation result type with an unresolved relation field is rejected.
|
package/package.json
CHANGED
package/src/runtime.js
CHANGED
|
@@ -1916,6 +1916,312 @@ const resolveById = async (type, args, context, requiredId) => {
|
|
|
1916
1916
|
return results[0] || null;
|
|
1917
1917
|
};
|
|
1918
1918
|
|
|
1919
|
+
// Batched collection reads. A generated collection field reads its children with one
|
|
1920
|
+
// adapter.findChildren call per parent unless the adapter defines findChildrenBatch and its
|
|
1921
|
+
// batchesChildren predicate does not return false. Then, within one request (a context object), the
|
|
1922
|
+
// reads of one related type and connection storage field whose effective arguments (after
|
|
1923
|
+
// middleware, the scope and joined scopes) hold the same plain data wait until the promise jobs
|
|
1924
|
+
// queued so far have run, as references do, and are read together, at most CHILD_BATCH_SIZE
|
|
1925
|
+
// distinct parents per call.
|
|
1926
|
+
const CHILD_BATCH_SIZE = 100;
|
|
1927
|
+
// Arguments whose canonical key would exceed this many characters, such as a long IN list, read
|
|
1928
|
+
// alone, so a field never spends more than this on canonicalization.
|
|
1929
|
+
const CHILD_ARGS_KEY_BUDGET = 64 * 1024;
|
|
1930
|
+
const NOT_PLAIN = Symbol('not plain data');
|
|
1931
|
+
|
|
1932
|
+
// The canonical key of a collection read's effective arguments: two reads share it only when their
|
|
1933
|
+
// arguments hold the same data. Only plain data qualifies: strings, numbers (-0, NaN and Infinity
|
|
1934
|
+
// kept distinct), booleans, undefined, null, bigints, dates (by time, with no own properties), dense
|
|
1935
|
+
// arrays, and objects with the Object or a null prototype whose own string-keyed properties are
|
|
1936
|
+
// enumerable data properties, all writable and configurable, or all fixed by freezing. The key keeps
|
|
1937
|
+
// property order, null prototypes, frozen objects and shared references. Anything else, such as a
|
|
1938
|
+
// function, a symbol, a class instance (an ObjectId included), an accessor, a non-enumerable,
|
|
1939
|
+
// sealed or non-extensible member, a sparse or decorated array, a cycle, an exotic object that fails
|
|
1940
|
+
// while it is read, or a key over the budget, returns null, and the read runs alone.
|
|
1941
|
+
const childArgsKey = (args) => {
|
|
1942
|
+
if (args === null || typeof args !== 'object' || Array.isArray(args)) return null;
|
|
1943
|
+
const parts = [];
|
|
1944
|
+
let size = 0;
|
|
1945
|
+
const push = (part) => {
|
|
1946
|
+
size += part.length;
|
|
1947
|
+
if (size > CHILD_ARGS_KEY_BUDGET) throw NOT_PLAIN;
|
|
1948
|
+
parts.push(part);
|
|
1949
|
+
};
|
|
1950
|
+
const seen = new Map();
|
|
1951
|
+
const visit = (value) => {
|
|
1952
|
+
switch (typeof value) {
|
|
1953
|
+
case 'string':
|
|
1954
|
+
if (value.length > CHILD_ARGS_KEY_BUDGET) throw NOT_PLAIN;
|
|
1955
|
+
push(JSON.stringify(value));
|
|
1956
|
+
return;
|
|
1957
|
+
case 'number': push(Object.is(value, -0) ? '-0' : String(value)); return;
|
|
1958
|
+
case 'boolean':
|
|
1959
|
+
case 'undefined': push(String(value)); return;
|
|
1960
|
+
case 'bigint': push(`${value}n`); return;
|
|
1961
|
+
case 'object': if (value !== null) break; push('null'); return;
|
|
1962
|
+
default: throw NOT_PLAIN;
|
|
1963
|
+
}
|
|
1964
|
+
const known = seen.get(value);
|
|
1965
|
+
if (known) {
|
|
1966
|
+
// A shared reference repeats the index of its first visit; a cycle is not plain data.
|
|
1967
|
+
if (!known.done) throw NOT_PLAIN;
|
|
1968
|
+
push(`@${known.index}`);
|
|
1969
|
+
return;
|
|
1970
|
+
}
|
|
1971
|
+
const entry = { index: seen.size, done: false };
|
|
1972
|
+
seen.set(value, entry);
|
|
1973
|
+
const prototype = Object.getPrototypeOf(value);
|
|
1974
|
+
const frozen = Object.isFrozen(value);
|
|
1975
|
+
const keys = Reflect.ownKeys(value);
|
|
1976
|
+
if (prototype === Date.prototype) {
|
|
1977
|
+
if (keys.length) throw NOT_PLAIN;
|
|
1978
|
+
// Throws a TypeError, caught below, for an object that only inherits from Date.prototype.
|
|
1979
|
+
push(`D${frozen ? 'F' : ''}${Date.prototype.getTime.call(value)}`);
|
|
1980
|
+
} else {
|
|
1981
|
+
const array = Array.isArray(value);
|
|
1982
|
+
if (array ? prototype !== Array.prototype : prototype !== Object.prototype && prototype !== null) throw NOT_PLAIN;
|
|
1983
|
+
// A dense array owns its indices, in order, then its length, and nothing else.
|
|
1984
|
+
if (array && (keys.length !== value.length + 1
|
|
1985
|
+
|| keys.some((key, index) => key !== (index < value.length ? String(index) : 'length'))
|
|
1986
|
+
|| Reflect.getOwnPropertyDescriptor(value, 'length').writable === frozen)) throw NOT_PLAIN;
|
|
1987
|
+
if (!frozen && !Object.isExtensible(value)) throw NOT_PLAIN;
|
|
1988
|
+
push(`${array ? '[' : '{'}${prototype === null ? '0' : ''}${frozen ? 'F' : ''}`);
|
|
1989
|
+
for (const key of keys) {
|
|
1990
|
+
if (typeof key === 'symbol') throw NOT_PLAIN;
|
|
1991
|
+
if (array && key === 'length') continue;
|
|
1992
|
+
const descriptor = Reflect.getOwnPropertyDescriptor(value, key);
|
|
1993
|
+
if (!descriptor || !('value' in descriptor) || !descriptor.enumerable
|
|
1994
|
+
|| descriptor.writable === frozen || descriptor.configurable === frozen) throw NOT_PLAIN;
|
|
1995
|
+
if (!array) push(`${JSON.stringify(key)}:`);
|
|
1996
|
+
visit(descriptor.value);
|
|
1997
|
+
push(',');
|
|
1998
|
+
}
|
|
1999
|
+
push(array ? ']' : '}');
|
|
2000
|
+
}
|
|
2001
|
+
entry.done = true;
|
|
2002
|
+
};
|
|
2003
|
+
try {
|
|
2004
|
+
visit(args);
|
|
2005
|
+
return parts.join('');
|
|
2006
|
+
} catch {
|
|
2007
|
+
// Not plain data, too large, or an exotic object (such as a proxy) that failed while it was read.
|
|
2008
|
+
return null;
|
|
2009
|
+
}
|
|
2010
|
+
};
|
|
2011
|
+
|
|
2012
|
+
// A copy of arguments that childArgsKey accepted, with the same prototypes, frozen objects, dates
|
|
2013
|
+
// and shared references, so the adapter may change it without reaching the request's objects.
|
|
2014
|
+
const clonePlainArgs = (value, copies = new Map()) => {
|
|
2015
|
+
if (value === null || typeof value !== 'object') return value;
|
|
2016
|
+
if (copies.has(value)) return copies.get(value);
|
|
2017
|
+
const prototype = Object.getPrototypeOf(value);
|
|
2018
|
+
if (prototype === Date.prototype) {
|
|
2019
|
+
const date = new Date(Date.prototype.getTime.call(value));
|
|
2020
|
+
copies.set(value, date);
|
|
2021
|
+
return Object.isFrozen(value) ? Object.freeze(date) : date;
|
|
2022
|
+
}
|
|
2023
|
+
const copy = Array.isArray(value) ? [] : Object.create(prototype);
|
|
2024
|
+
copies.set(value, copy);
|
|
2025
|
+
for (const key of Object.keys(value)) {
|
|
2026
|
+
Object.defineProperty(copy, key, {
|
|
2027
|
+
value: clonePlainArgs(value[key], copies), writable: true, enumerable: true, configurable: true,
|
|
2028
|
+
});
|
|
2029
|
+
}
|
|
2030
|
+
return Object.isFrozen(value) ? Object.freeze(copy) : copy;
|
|
2031
|
+
};
|
|
2032
|
+
|
|
2033
|
+
// The batch key of a parent ID: its string form, when the adapter casts the ID without changing
|
|
2034
|
+
// that form. A missing ID (which keeps the adapter's own semantics, such as MongoDB matching
|
|
2035
|
+
// children without a parent), an object whose string form says nothing of its value, an ID castId
|
|
2036
|
+
// rejects and one it normalizes have none, and read alone.
|
|
2037
|
+
const childParentKey = (parentId) => {
|
|
2038
|
+
try {
|
|
2039
|
+
if (parentId == null) return null;
|
|
2040
|
+
const key = String(parentId);
|
|
2041
|
+
if (typeof parentId === 'object' && key === Object.prototype.toString.call(parentId)) return null;
|
|
2042
|
+
return String(adapter.castId(parentId)) === key ? key : null;
|
|
2043
|
+
} catch {
|
|
2044
|
+
return null;
|
|
2045
|
+
}
|
|
2046
|
+
};
|
|
2047
|
+
|
|
2048
|
+
// Collection reads of one request that wait for a batch: context -> related type registration ->
|
|
2049
|
+
// (connection storage field + canonical arguments) -> group. Nothing is cached: each group is read
|
|
2050
|
+
// once and dropped.
|
|
2051
|
+
const childBatches = new WeakMap();
|
|
2052
|
+
|
|
2053
|
+
const readChildrenAlone = (type, connection, parentId, args) => adapter.findChildren(
|
|
2054
|
+
type.model, type.gqltype, connection.storageFieldName, parentId, args, null,
|
|
2055
|
+
);
|
|
2056
|
+
|
|
2057
|
+
// Whether the adapter reads this type's children in batches, checked on every read as an adapter
|
|
2058
|
+
// method call. A predicate that throws keeps per-parent reads; an error other than a SimfinityError
|
|
2059
|
+
// is reported once per type.
|
|
2060
|
+
const batchesChildren = (type, connection) => {
|
|
2061
|
+
if (adapter.batchesChildren == null) return true;
|
|
2062
|
+
try {
|
|
2063
|
+
return adapter.batchesChildren(type.model, type.gqltype, connection.storageFieldName) !== false;
|
|
2064
|
+
} catch (error) {
|
|
2065
|
+
if (!(error instanceof SimfinityError)) {
|
|
2066
|
+
warnOnce(type.gqltype, 'batchesChildren', 'Configuration issue: adapter.batchesChildren failed for '
|
|
2067
|
+
+ `${type.gqltype.name} (${error?.message ?? String(error)}); its collection fields read one parent at a time.`);
|
|
2068
|
+
}
|
|
2069
|
+
return false;
|
|
2070
|
+
}
|
|
2071
|
+
};
|
|
2072
|
+
|
|
2073
|
+
// Whether a batched child record may belong to the parent with this key: it is an object, and the
|
|
2074
|
+
// parent it stores under the connection storage field (or its GraphQL name), when it shows one, has
|
|
2075
|
+
// that key by string form or by castId. A stored list (a multikey connection) needs one such entry.
|
|
2076
|
+
const belongsToParent = (record, connection, key) => {
|
|
2077
|
+
if (record === null || typeof record !== 'object') return false;
|
|
2078
|
+
let stored = ownValue(record, connection.storageFieldName);
|
|
2079
|
+
if (stored === undefined && connection.graphqlFieldName) stored = ownValue(record, connection.graphqlFieldName);
|
|
2080
|
+
if (stored === undefined) return true;
|
|
2081
|
+
const matches = (value) => {
|
|
2082
|
+
const id = value?._id ?? value;
|
|
2083
|
+
if (id == null) return false;
|
|
2084
|
+
try {
|
|
2085
|
+
return String(id) === key || String(adapter.castId(id)) === key;
|
|
2086
|
+
} catch {
|
|
2087
|
+
return false;
|
|
2088
|
+
}
|
|
2089
|
+
};
|
|
2090
|
+
return Array.isArray(stored) ? stored.some(matches) : matches(stored);
|
|
2091
|
+
};
|
|
2092
|
+
|
|
2093
|
+
// The fields of one parent share its records, as references do, but each gets its own list.
|
|
2094
|
+
const settleChildren = (waiters, list) => waiters.forEach(({ resolve }, index) => {
|
|
2095
|
+
resolve(index === 0 ? list : [...list]);
|
|
2096
|
+
});
|
|
2097
|
+
|
|
2098
|
+
// After a failed read, each field reads alone, in its own promise job, with its own copy of the
|
|
2099
|
+
// arguments and its own parent ID, so an error fails only the fields it would fail alone.
|
|
2100
|
+
const readEachChildAlone = (type, group, waiters) => {
|
|
2101
|
+
for (const { parentId, resolve, reject } of waiters) {
|
|
2102
|
+
Promise.resolve()
|
|
2103
|
+
.then(() => readChildrenAlone(type, group.connection, parentId, clonePlainArgs(group.args)))
|
|
2104
|
+
.then(resolve, reject);
|
|
2105
|
+
}
|
|
2106
|
+
};
|
|
2107
|
+
|
|
2108
|
+
// Whether a batch result holds one list per parent, in order, of records of that parent. Every index is
|
|
2109
|
+
// checked, holes included, since Array.prototype.every skips empty slots.
|
|
2110
|
+
const listsMatchParents = (lists, parents, connection) => {
|
|
2111
|
+
if (!Array.isArray(lists) || lists.length !== parents.length) return false;
|
|
2112
|
+
for (let index = 0; index < lists.length; index++) {
|
|
2113
|
+
const records = Object.hasOwn(lists, index) ? lists[index] : undefined;
|
|
2114
|
+
if (!Array.isArray(records)) return false;
|
|
2115
|
+
for (let position = 0; position < records.length; position++) {
|
|
2116
|
+
if (!Object.hasOwn(records, position) || !belongsToParent(records[position], connection, parents[index].key)) return false;
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2119
|
+
return true;
|
|
2120
|
+
};
|
|
2121
|
+
|
|
2122
|
+
const readChildren = async (type, group, parents, context) => {
|
|
2123
|
+
const { connection } = group;
|
|
2124
|
+
if (parents.length === 1) {
|
|
2125
|
+
// One distinct parent reads with findChildren, as before, shared by the fields that read it.
|
|
2126
|
+
const { waiters } = parents[0];
|
|
2127
|
+
let list;
|
|
2128
|
+
try {
|
|
2129
|
+
list = await readChildrenAlone(type, connection, waiters[0].parentId, clonePlainArgs(group.args));
|
|
2130
|
+
} catch (error) {
|
|
2131
|
+
if (waiters.length === 1) waiters[0].reject(error);
|
|
2132
|
+
else readEachChildAlone(type, group, waiters);
|
|
2133
|
+
return;
|
|
2134
|
+
}
|
|
2135
|
+
if (waiters.length > 1 && !Array.isArray(list)) readEachChildAlone(type, group, waiters);
|
|
2136
|
+
else settleChildren(waiters, list);
|
|
2137
|
+
return;
|
|
2138
|
+
}
|
|
2139
|
+
let lists;
|
|
2140
|
+
try {
|
|
2141
|
+
lists = await adapter.findChildrenBatch(type.model, type.gqltype, connection.storageFieldName,
|
|
2142
|
+
parents.map(({ waiters }) => waiters[0].parentId), clonePlainArgs(group.args), { context });
|
|
2143
|
+
} catch (error) {
|
|
2144
|
+
if (!(error instanceof SimfinityError)) {
|
|
2145
|
+
warnOnce(type.gqltype, 'findChildrenBatch', `adapter.findChildrenBatch failed for ${type.gqltype.name} `
|
|
2146
|
+
+ `(${error?.message ?? String(error)}); the fields it read were read one parent at a time. Later failures `
|
|
2147
|
+
+ 'for this type are not reported.');
|
|
2148
|
+
}
|
|
2149
|
+
parents.forEach(({ waiters }) => readEachChildAlone(type, group, waiters));
|
|
2150
|
+
return;
|
|
2151
|
+
}
|
|
2152
|
+
if (!listsMatchParents(lists, parents, connection)) {
|
|
2153
|
+
warnOnce(type.gqltype, 'findChildrenBatch', 'Configuration issue: adapter.findChildrenBatch did not return one '
|
|
2154
|
+
+ `list of ${type.gqltype.name} records per parent ID, in parentIds order; the fields it read were read one `
|
|
2155
|
+
+ 'parent at a time.');
|
|
2156
|
+
parents.forEach(({ waiters }) => readEachChildAlone(type, group, waiters));
|
|
2157
|
+
return;
|
|
2158
|
+
}
|
|
2159
|
+
parents.forEach(({ waiters }, index) => settleChildren(waiters, lists[index]));
|
|
2160
|
+
};
|
|
2161
|
+
|
|
2162
|
+
const flushChildren = (type, group, context) => {
|
|
2163
|
+
const parents = [...group.parents].map(([key, { waiters }]) => ({ key, waiters }));
|
|
2164
|
+
for (let start = 0; start < parents.length; start += CHILD_BATCH_SIZE) {
|
|
2165
|
+
const chunk = parents.slice(start, start + CHILD_BATCH_SIZE);
|
|
2166
|
+
// An unexpected error settles every field of the chunk that is still waiting.
|
|
2167
|
+
readChildren(type, group, chunk, context).catch((error) => {
|
|
2168
|
+
for (const { waiters } of chunk) waiters.forEach(({ reject }) => reject(error));
|
|
2169
|
+
});
|
|
2170
|
+
}
|
|
2171
|
+
};
|
|
2172
|
+
|
|
2173
|
+
// A generated collection read. It runs at once, with the arguments object it received, as before,
|
|
2174
|
+
// unless the adapter batches this type's children, the request has a context object, the parent ID
|
|
2175
|
+
// has a batch key, and the effective arguments are plain data with valid pagination and a key within
|
|
2176
|
+
// the budget. Then it joins the request's group for its related type, connection storage field and
|
|
2177
|
+
// arguments: the first read of a group copies the arguments, and later reads compute only the key.
|
|
2178
|
+
const loadChildren = (type, connection, parentId, args, context) => {
|
|
2179
|
+
if (typeof adapter.findChildrenBatch !== 'function' || typeof context !== 'object' || context === null
|
|
2180
|
+
|| !batchesChildren(type, connection)) return readChildrenAlone(type, connection, parentId, args);
|
|
2181
|
+
const key = childParentKey(parentId);
|
|
2182
|
+
const argsKey = key === null ? null : childArgsKey(args);
|
|
2183
|
+
if (argsKey === null) return readChildrenAlone(type, connection, parentId, args);
|
|
2184
|
+
try {
|
|
2185
|
+
// Invalid pagination fails each field alone, as findChildren reports it.
|
|
2186
|
+
paginationStages(args.pagination, true);
|
|
2187
|
+
} catch {
|
|
2188
|
+
return readChildrenAlone(type, connection, parentId, args);
|
|
2189
|
+
}
|
|
2190
|
+
let byType = childBatches.get(context);
|
|
2191
|
+
if (!byType) {
|
|
2192
|
+
byType = new Map();
|
|
2193
|
+
childBatches.set(context, byType);
|
|
2194
|
+
}
|
|
2195
|
+
let groups = byType.get(type);
|
|
2196
|
+
if (!groups) {
|
|
2197
|
+
groups = new Map();
|
|
2198
|
+
byType.set(type, groups);
|
|
2199
|
+
}
|
|
2200
|
+
const groupKey = `${JSON.stringify(connection.storageFieldName)}${argsKey}`;
|
|
2201
|
+
let group = groups.get(groupKey);
|
|
2202
|
+
if (!group) {
|
|
2203
|
+
let snapshot;
|
|
2204
|
+
try {
|
|
2205
|
+
snapshot = clonePlainArgs(args);
|
|
2206
|
+
} catch {
|
|
2207
|
+
return readChildrenAlone(type, connection, parentId, args);
|
|
2208
|
+
}
|
|
2209
|
+
group = { connection, args: snapshot, parents: new Map() };
|
|
2210
|
+
groups.set(groupKey, group);
|
|
2211
|
+
const created = group;
|
|
2212
|
+
// As for references, flush once the promise jobs queued so far have run.
|
|
2213
|
+
Promise.resolve().then(() => process.nextTick(() => {
|
|
2214
|
+
groups.delete(groupKey);
|
|
2215
|
+
if (!groups.size && byType.get(type) === groups) byType.delete(type);
|
|
2216
|
+
flushChildren(type, created, context);
|
|
2217
|
+
}));
|
|
2218
|
+
}
|
|
2219
|
+
return new Promise((resolve, reject) => {
|
|
2220
|
+
if (!group.parents.has(key)) group.parents.set(key, { waiters: [] });
|
|
2221
|
+
group.parents.get(key).waiters.push({ parentId, resolve, reject });
|
|
2222
|
+
});
|
|
2223
|
+
};
|
|
2224
|
+
|
|
1919
2225
|
// Why a type's generated create or update input has no fields, for the warnings of buildMutation. An
|
|
1920
2226
|
// update input also needs a writable id.
|
|
1921
2227
|
const withoutWritableFields = (type, { noWritableId = false } = {}) => {
|
|
@@ -2555,14 +2861,7 @@ const autoGenerateResolvers = (gqltype) => {
|
|
|
2555
2861
|
await executeMiddleware(params);
|
|
2556
2862
|
await executeScope(params);
|
|
2557
2863
|
await applyJoinedScopes(joinedScopes, params);
|
|
2558
|
-
return
|
|
2559
|
-
relatedTypeInfo.model,
|
|
2560
|
-
relatedTypeInfo.gqltype,
|
|
2561
|
-
connection.storageFieldName,
|
|
2562
|
-
parent._id ?? parent.id,
|
|
2563
|
-
params.args,
|
|
2564
|
-
null,
|
|
2565
|
-
);
|
|
2864
|
+
return loadChildren(relatedTypeInfo, connection, parent._id ?? parent.id, params.args, context);
|
|
2566
2865
|
}, { simfinityQuery: { typeName: relatedType.name, operation: 'find' } });
|
|
2567
2866
|
runtimeBoundTypes.set(gqltype, runtimeIdentity);
|
|
2568
2867
|
} else if (fieldEntry.type instanceof GraphQLObjectType
|
package/types/auth/index.d.ts
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -340,6 +340,41 @@ export interface DatabaseAdapter<Model = any, Session = any> {
|
|
|
340
340
|
count(model: Model, type: GraphQLObjectType, args: any, session?: Session | null): Promise<number> | number;
|
|
341
341
|
aggregate(model: Model, type: GraphQLObjectType, args: any, session?: Session | null): any;
|
|
342
342
|
findChildren(model: Model, type: GraphQLObjectType, connectionField: string, parentId: any, args: any, session?: Session | null): any;
|
|
343
|
+
/**
|
|
344
|
+
* Optional batch form of `findChildren` for generated collection fields. The runtime calls it as
|
|
345
|
+
* an adapter method (`this` is the adapter) only when `batchesChildren` does not return false, and
|
|
346
|
+
* checks both members on every read. Within one request (a GraphQL context object), it groups the
|
|
347
|
+
* collection reads of one related type and connection storage field whose effective arguments
|
|
348
|
+
* (after middleware, the scope and joined scopes) hold the same plain data and are requested
|
|
349
|
+
* before the promise jobs queued so far have run, and calls this method with 2 to 100 distinct
|
|
350
|
+
* parent IDs: each one the raw ID the first of its fields read, never null, and one that `castId`
|
|
351
|
+
* accepts without changing its string form. A group with one distinct parent calls `findChildren`
|
|
352
|
+
* instead. `args` is a copy the method may change; it holds only plain data and dates, and its
|
|
353
|
+
* pagination is valid. `options.context` is the request context; the runtime passes no session.
|
|
354
|
+
*
|
|
355
|
+
* Return, or resolve to, one array per parent ID, in `parentIds` order, each holding exactly the
|
|
356
|
+
* records, order and page that `findChildren(model, type, connectionField, parentIds[i], args, null)`
|
|
357
|
+
* returns for that parent; the result must not depend on `options.context`. Records should show
|
|
358
|
+
* their parent under `connectionField` (or the connection field's GraphQL name), as `findChildren`
|
|
359
|
+
* records do, or not show one. Fields that read the same parent share its records, each with its
|
|
360
|
+
* own array. After a rejection or throw, or a result of another shape (not an array, another
|
|
361
|
+
* length, an entry that is not an array, or a record that shows another parent), each field reads
|
|
362
|
+
* alone with `findChildren`; a failure other than a `SimfinityError` and a result of another shape
|
|
363
|
+
* are reported once per type with `console.warn`. To keep per-parent reads for a model or field,
|
|
364
|
+
* return false from `batchesChildren`.
|
|
365
|
+
*/
|
|
366
|
+
findChildrenBatch?: ((model: Model, type: GraphQLObjectType, connectionField: string, parentIds: any[], args: any, options?: { context?: any }) => any[][] | PromiseLike<any[][]>) | undefined;
|
|
367
|
+
/**
|
|
368
|
+
* Optional, synchronous. Return false to read the children of `model` through `connectionField`
|
|
369
|
+
* with one `findChildren` call per field, as an adapter without `findChildrenBatch` does: at once,
|
|
370
|
+
* with the arguments object the field built. The runtime calls it as an adapter method on every
|
|
371
|
+
* read that could join a batch. Return false whenever `findChildrenBatch` would not reproduce
|
|
372
|
+
* `this.findChildren`, such as when `this.findChildren` is not the adapter's own function (a copy
|
|
373
|
+
* or an `Object.assign` override that changes how children are read), or for a model whose reads
|
|
374
|
+
* have hooks or settings that a batched read would bypass. When it is absent, the runtime batches
|
|
375
|
+
* every read it can.
|
|
376
|
+
*/
|
|
377
|
+
batchesChildren?: ((model: Model, type: GraphQLObjectType, connectionField: string) => boolean) | undefined;
|
|
343
378
|
}
|
|
344
379
|
export function createRuntime<Model = any, Session = any>(adapter: DatabaseAdapter<Model, Session>): Runtime<Model, Session>;
|
|
345
380
|
|
|
@@ -363,15 +398,34 @@ export function configureQueryLimits(options?: { maxPageSize?: number }): void;
|
|
|
363
398
|
export function configureMutationLimits(options?: MutationLimitsOptions): void;
|
|
364
399
|
export function paginationStages(pagination: { page: number; size: number } | null | undefined, withDefault: boolean): Array<{ $skip: number } | { $limit: number }>;
|
|
365
400
|
|
|
366
|
-
/**
|
|
401
|
+
/**
|
|
402
|
+
* Envelop-style schema plugin, as MCP `schemaPlugins` accepts it; `createAuthPlugin` returns the
|
|
403
|
+
* narrower {@link AuthSchemaPlugin}. `@simtlix/simfinity-mcp` re-exports this declaration, so
|
|
404
|
+
* `export *` of both packages exports one `EnvelopSchemaPlugin`.
|
|
405
|
+
*/
|
|
367
406
|
export interface EnvelopSchemaPlugin {
|
|
407
|
+
/**
|
|
408
|
+
* Wraps resolvers in place. MCP `schemaPlugins` awaits a returned promise; Envelop and Yoga do
|
|
409
|
+
* not, so plugins shared with them should install synchronously.
|
|
410
|
+
*/
|
|
368
411
|
onSchemaChange?: (payload: {
|
|
369
412
|
schema: GraphQLSchema;
|
|
370
413
|
replaceSchema: (schema: GraphQLSchema) => void;
|
|
371
|
-
}) => void
|
|
414
|
+
}) => void | Promise<void>;
|
|
372
415
|
[key: string]: unknown;
|
|
373
416
|
}
|
|
374
417
|
|
|
418
|
+
/**
|
|
419
|
+
* What `createAuthPlugin` returns: an {@link EnvelopSchemaPlugin} whose hook wraps the resolvers
|
|
420
|
+
* synchronously and returns nothing.
|
|
421
|
+
*/
|
|
422
|
+
export interface AuthSchemaPlugin extends EnvelopSchemaPlugin {
|
|
423
|
+
onSchemaChange?: (payload: {
|
|
424
|
+
schema: GraphQLSchema;
|
|
425
|
+
replaceSchema: (schema: GraphQLSchema) => void;
|
|
426
|
+
}) => void;
|
|
427
|
+
}
|
|
428
|
+
|
|
375
429
|
/** A single field validator (throws SimfinityError VALIDATION_ERROR on failure). */
|
|
376
430
|
export interface FieldValidator {
|
|
377
431
|
validate(typeName: string, fieldName: string, value: unknown, session?: unknown): Promise<void>;
|
|
@@ -505,7 +559,7 @@ export const auth: {
|
|
|
505
559
|
* `onSchemaChange` throws TypeError, before wrapping any field, when a schema type was created
|
|
506
560
|
* by another copy of the graphql module.
|
|
507
561
|
*/
|
|
508
|
-
createAuthPlugin(permissions: PermissionSchema, options?: AuthPluginOptions):
|
|
562
|
+
createAuthPlugin(permissions: PermissionSchema, options?: AuthPluginOptions): AuthSchemaPlugin;
|
|
509
563
|
/** @deprecated Use createAuthPlugin instead. graphql-middleware compatible middleware. */
|
|
510
564
|
createAuthMiddleware(
|
|
511
565
|
permissions: PermissionSchema,
|
|
@@ -575,7 +629,7 @@ export const auth: {
|
|
|
575
629
|
|
|
576
630
|
/** GraphQL server plugins: auth plugin factory plus count-extension plugins. */
|
|
577
631
|
export const plugins: {
|
|
578
|
-
createAuthPlugin(permissions: PermissionSchema, options?: AuthPluginOptions):
|
|
632
|
+
createAuthPlugin(permissions: PermissionSchema, options?: AuthPluginOptions): AuthSchemaPlugin;
|
|
579
633
|
/** Apollo Server plugin that copies `contextValue.count` into `extensions.count`. */
|
|
580
634
|
apolloCountPlugin(): {
|
|
581
635
|
requestDidStart(): Promise<{
|
package/types/plugins.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { plugins } from './index.js';
|
|
6
6
|
|
|
7
|
-
export type { AuthPluginOptions, EnvelopSchemaPlugin, PermissionSchema } from './index.js';
|
|
7
|
+
export type { AuthPluginOptions, AuthSchemaPlugin, EnvelopSchemaPlugin, PermissionSchema } from './index.js';
|
|
8
8
|
// The /auth declaration itself, as the runtime re-exports that binding, so the two subpaths can be
|
|
9
9
|
// re-exported together.
|
|
10
10
|
export { createAuthPlugin } from './auth/index.js';
|