Implement implicit object references and capability query foundations

This commit is contained in:
Timothy J. Aveni
2026-09-26 11:16:46 -07:00
parent 58a75da83f
commit 3728612e8c
13 changed files with 401 additions and 35 deletions
+38 -1
View File
@@ -8,6 +8,7 @@ import {
GraphQLInputObjectType,
GraphQLList,
GraphQLNonNull,
visit,
type GraphQLInputType,
type GraphQLOutputType,
type GraphQLInputFieldConfigMap,
@@ -54,6 +55,16 @@ export interface RelationalSchemaEnvironment {
}
export function relationalSchema(env: RelationalSchemaEnvironment, document?: DocumentNode) {
const requestedInterfaces = new Set<string>();
if (document)
visit(document, {
ObjectField(node) {
if (node.name.value !== "_camino" || node.value.kind !== "ObjectValue") return;
const qualified = node.value.fields.find((field) => field.name.value === "as");
if (qualified?.value.kind === "ObjectValue")
for (const field of qualified.value.fields) requestedInterfaces.add(field.name.value);
},
});
const outputCache = new Map<string, GraphQLObjectType>();
const inputCache = new Map<string, GraphQLInputObjectType>();
const referenceCache = new Map<string, GraphQLScalarType>();
@@ -303,14 +314,40 @@ export function relationalSchema(env: RelationalSchemaEnvironment, document?: Do
not: { type: where(row) },
};
for (const [name, field] of fields(row)) {
if (row.kind === "object" && field.kind === "row") continue;
if (name in result) throw new QueryCompileError("QUERY_SCHEMA_NAME", `Reserved predicate name ${name}`);
if (row.kind === "object" && field.kind === "row") {
result[name] = {
type: compare(
valueType.interfaceRef(
field.row.kind === "object"
? field.row.interfaceRevisionId
: (() => {
throw new Error("Expected relationship object");
})(),
),
),
};
continue;
}
if (field.kind === "leaf" && !comparableValue(field.type)) continue;
result[name] = { type: field.kind === "row" ? where(field.row) : compare(field.type) };
}
result._camino = {
type: inp("WhereMeta", row, () => {
const meta: GraphQLInputFieldConfigMap = {};
if (row.kind === "object" && requestedInterfaces.size)
meta.as = {
type: inp("Interfaces", row, () =>
Object.fromEntries(
[...env.contracts.values()]
.filter((contract) => !contract.template && requestedInterfaces.has(env.name(contract.revisionId)))
.map((contract) => [
env.name(contract.revisionId),
{ type: where(objectRow(contract.revisionId)) },
]),
),
),
};
for (const [name, type] of leafFields(row)) meta[name] = { type: compare(type) };
const edges = relations(row);
if (edges.length)