Scaffold explicit query placement and authored GraphQL documents

This commit is contained in:
Timothy J. Aveni
2026-09-21 01:30:57 -07:00
parent 20ef63007c
commit 01a3ec5ae2
4 changed files with 157 additions and 5 deletions
+79
View File
@@ -0,0 +1,79 @@
import fs from "node:fs/promises";
import path from "node:path";
import { parse } from "graphql";
import type { ScaffoldRecipe } from "./scaffold-recipes.js";
import type { StructuralRequest } from "./structural-plan.js";
import { parseQx, walkSyntax } from "./source.js";
/** Insert the authored query and its document together; placement never changes implicitly. */
export async function scaffoldQuery(root: string, prefix: string, spec: ScaffoldRecipe): Promise<StructuralRequest> {
if (
!spec.name ||
!/^[A-Za-z_][A-Za-z0-9_]*$/.test(spec.name) ||
!spec.id ||
!spec.declaration ||
typeof spec.queryDocument !== "string"
)
throw new Error("Query scaffold requires name, id, declaration and queryDocument");
const source = `package Draft id "draft" revision "draft@1" { ${spec.declaration} }`;
const syntax = parseQx(source);
const pkg = syntax.root.children.find((node) => node.kind === "packageResourceDecl");
const queries = [...walkSyntax(syntax.root)].filter((node) => node.kind === "packageQuery");
const query = queries[0];
if (
syntax.diagnostics.length ||
syntax.root.children.length !== 1 ||
!pkg ||
queries.length !== 1 ||
!query ||
pkg.children.filter((node) => node.kind === "packageExport").length !== 1 ||
pkg.children.some((node) => !["identifier", "stringLiteral", "packageExport"].includes(node.kind))
)
throw new Error("Expected one QX query declaration");
const identifier = query.children.find((node) => node.kind === "identifier")!;
const literals = query.children
.filter((node) => node.kind === "stringLiteral")
.map((node) => JSON.parse(source.slice(node.start, node.end)) as string);
const [id, document, operation] = literals;
if (source.slice(identifier.start, identifier.end) !== spec.name || id !== spec.id)
throw new Error("Query declaration name/ID must match its registration");
if (!document || !/^queries\/(?:[A-Za-z0-9_-]+\/)*[A-Za-z0-9_-]+\.graphql$/.test(document))
throw new Error("Query document must be a contained queries/*.graphql path");
const graphql = parse(spec.queryDocument);
const operations = graphql.definitions.filter((definition) => definition.kind === "OperationDefinition");
if (operations.length !== 1 || operations[0]!.operation !== "query" || operations[0]!.name?.value !== operation)
throw new Error("GraphQL document must contain the declared named query operation");
const authored = await fs.readFile(path.join(root, prefix, "package.qx"), "utf8");
const existing = parseQx(authored);
const parent = existing.root.children.find((node) => node.kind === "packageResourceDecl");
if (existing.diagnostics.length || !parent) throw new Error("Fix package.qx before adding a query");
for (const node of parent.children.filter((node) => node.kind === "packageExport").flatMap((node) => node.children)) {
const name = node.children.find((child) => child.kind === "identifier");
const literal = node.children.find((child) => child.kind === "stringLiteral");
if (
(name && authored.slice(name.start, name.end) === spec.name) ||
(literal && JSON.parse(authored.slice(literal.start, literal.end)) === spec.id)
)
throw new Error("New query requires a unique name and ID");
}
const parentID = parent.children.find((node) => node.kind === "stringLiteral")!;
return {
kind: "package",
source: spec.source,
resourceRoot: spec.directory,
validation: "syntax",
files: [
{
file: prefix + "package.qx",
edits: [
{
operation: "append",
parent: { kind: "packageResourceDecl", id: JSON.parse(authored.slice(parentID.start, parentID.end)) },
source: spec.declaration,
},
],
},
{ file: prefix + document, create: spec.queryDocument },
],
};
}
+4 -1
View File
@@ -12,6 +12,7 @@ import type { StructuralRequest } from "./structural-plan.js";
import { addImplementation } from "./implementation-edit.js";
import { reactPlatformTypes } from "../bindings/react-platform.js";
import { executionScaffold, isExecutionScaffold } from "./execution-scaffold.js";
import { scaffoldQuery } from "./query-scaffold.js";
type Source = { repository: string; commit: string };
type PackageEditModel = {
@@ -29,6 +30,7 @@ export type ScaffoldRecipe = {
id?: string;
revision?: string;
declaration?: string;
queryDocument?: string;
/** Initial authored files for a composed recipe; only used for a new package. */
initialFiles?: Record<string, string>;
tools?: { quixos: Source; protocol: Source; helpers: Source; sdk: Source };
@@ -71,7 +73,7 @@ const ownedJson = async <T>(root: string, file: string): Promise<T> => {
* Implementation files are created once; later edits preserve authored wiring. */
export const scaffoldRecipe = async (
root: string,
command: "package" | "function" | "migration" | "refresh",
command: "package" | "function" | "migration" | "refresh" | "query",
spec: ScaffoldRecipe,
): Promise<StructuralRequest> => {
if (command === "refresh")
@@ -82,6 +84,7 @@ export const scaffoldRecipe = async (
if (spec.directory && !/^[A-Za-z0-9_-][A-Za-z0-9_.-]*(?:\/[A-Za-z0-9_-][A-Za-z0-9_.-]*)*$/.test(spec.directory))
throw new Error("Scaffold directory must be contained");
const prefix = spec.directory ? `${spec.directory}/` : "";
if (command === "query") return scaffoldQuery(root, prefix, spec);
if (
spec.template === "typescript-execution" ||
(command !== "package" && (await isExecutionScaffold(root, prefix)))
+17 -4
View File
@@ -12,6 +12,7 @@ import os from "node:os";
import { spawnSync } from "node:child_process";
import { planStructure, applyStructure, resumeStructure, type StructuralRequest } from "./structural-plan.js";
import { scaffoldRecipe, type ScaffoldRecipe } from "./scaffold-recipes.js";
import { isExecutionScaffold } from "./execution-scaffold.js";
import { checkBundleSources } from "../bindings/bundle-policy.js";
import { sealMigrations } from "./migration-seal.js";
import { generatePackageDescriptor } from "../bindings/index.js";
@@ -201,7 +202,7 @@ const main = async () => {
}
if (!command || command === "--help" || args.includes("--help")) {
process.stdout.write(
"quixos-qx: author-check, author-contract, converge, worklist, inspect, resources, check-committed, source-baseline, scaffold-package, scaffold-interface, scaffold-function, scaffold-dependency, scaffold-structure, scaffold-resume, pin-upgrade, parse, lint, format\n" +
"quixos-qx: author-check, author-contract, converge, worklist, inspect, resources, check-committed, source-baseline, scaffold-package, scaffold-interface, scaffold-function, scaffold-query, scaffold-dependency, scaffold-structure, scaffold-resume, pin-upgrade, parse, lint, format\n" +
"inspect WORKBENCH [RESOURCE] shows provisional contracts, with explicit historical fallback; never verification evidence.\n" +
"resources WORKBENCH lists registered editable repositories. Use qx-workspace for the workspace authoring workflow.\n",
);
@@ -387,7 +388,11 @@ const main = async () => {
throw new Error(
"Refresh is no longer required: edit declarations and typed implementation wiring, then run qx-workspace check. Dependency installation uses scaffold install.",
);
if (["scaffold-package", "scaffold-function", "scaffold-migration", "scaffold-install"].includes(command)) {
if (
["scaffold-package", "scaffold-function", "scaffold-query", "scaffold-migration", "scaffold-install"].includes(
command,
)
) {
const [root, specFile, ...flags] = args;
let spec: ScaffoldRecipe;
if (command === "scaffold-function" && /^[A-Za-z_][A-Za-z0-9_]*$/.test(specFile ?? "")) {
@@ -421,16 +426,24 @@ const main = async () => {
(flags.includes("--install") && !flags.includes("--write"))
)
throw new Error(
"usage: quixos-qx scaffold-package|function|migration|refresh ROOT SPEC_JSON [--write [--install]]",
"usage: quixos-qx scaffold-package|function|query|migration ROOT SPEC_JSON [--write [--install]]",
);
const plan =
command === "scaffold-install"
? undefined
: await planStructure(
root,
await scaffoldRecipe(root, command.slice(9) as "package" | "function" | "migration", spec),
await scaffoldRecipe(root, command.slice(9) as "package" | "function" | "query" | "migration", spec),
process.env.QUIXOS_SNAPSHOT_MAP,
);
if (
flags.includes("--install") &&
(spec.template === "typescript-execution" ||
(await isExecutionScaffold(root, spec.directory ? `${spec.directory}/` : "")))
)
throw new Error(
"Execution package dependencies are supplied by checkedArtifacts; omit --install and run workspace-root check after authoring.",
);
const applied = plan && flags.includes("--write") ? await applyStructure(plan) : undefined;
if (flags.includes("--install")) {
const cwd = path.resolve(root, spec.directory ?? "");