From 3a09b7e18197c0ead4d2406e8ef292fb9d9db450 Mon Sep 17 00:00:00 2001 From: Carson Full Date: Mon, 4 Mar 2024 10:39:59 -0600 Subject: [PATCH 1/2] Accept readonly arrays on generated query file args This is safe because it's saying we expect less. Rather than giving less on output, which could be a breaking change. --- .../driver/src/reflection/analyzeQuery.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/driver/src/reflection/analyzeQuery.ts b/packages/driver/src/reflection/analyzeQuery.ts index a7983fa79..1c40d155f 100644 --- a/packages/driver/src/reflection/analyzeQuery.ts +++ b/packages/driver/src/reflection/analyzeQuery.ts @@ -31,6 +31,7 @@ export async function analyzeQuery( const args = walkCodec(inCodec, { indent: "", optionalNulls: true, + readonly: true, imports, }); @@ -38,6 +39,7 @@ export async function analyzeQuery( walkCodec(outCodec, { indent: "", optionalNulls: false, + readonly: false, imports, }), cardinality @@ -91,7 +93,12 @@ export function applyCardinalityToTsType( export { walkCodec as walkCodecToTsType }; function walkCodec( codec: ICodec, - ctx: { indent: string; optionalNulls: boolean; imports: Set } + ctx: { + indent: string; + optionalNulls: boolean; + readonly: boolean; + imports: Set; + } ): string { if (codec instanceof NullCodec) { return "null"; @@ -111,7 +118,7 @@ function walkCodec( ? codec.getFields() : codec.getNames().map((name) => ({ name, cardinality: ONE })); const subCodecs = codec.getSubcodecs(); - return `{\n${fields + const objectShape = `{\n${fields .map((field, i) => { let subCodec = subCodecs[i]; if (subCodec instanceof SetCodec) { @@ -130,12 +137,16 @@ function walkCodec( )};`; }) .join("\n")}\n${ctx.indent}}`; + return ctx.readonly ? `Readonly<${objectShape}>` : objectShape; } if (codec instanceof ArrayCodec) { - return `${walkCodec(codec.getSubcodecs()[0], ctx)}[]`; + return `${ctx.readonly ? "readonly " : ""}${walkCodec( + codec.getSubcodecs()[0], + ctx + )}[]`; } if (codec instanceof TupleCodec) { - return `[${codec + return `${ctx.readonly ? "readonly " : ""}[${codec .getSubcodecs() .map((subCodec) => walkCodec(subCodec, ctx)) .join(", ")}]`; From e6cc9729fc6d29c42fb4d703ddf4bf039ed2c556 Mon Sep 17 00:00:00 2001 From: Carson Full Date: Mon, 4 Mar 2024 11:11:27 -0600 Subject: [PATCH 2/2] Add test --- .../lts/dbschema/queries/get_movies_starring.edgeql | 4 ++-- integration-tests/lts/queries.test.ts | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/integration-tests/lts/dbschema/queries/get_movies_starring.edgeql b/integration-tests/lts/dbschema/queries/get_movies_starring.edgeql index 4391e356b..f2494f51d 100644 --- a/integration-tests/lts/dbschema/queries/get_movies_starring.edgeql +++ b/integration-tests/lts/dbschema/queries/get_movies_starring.edgeql @@ -11,5 +11,5 @@ select Movie { version := sys::get_version(), range := range(123, 456), local_date := '2022-09-08', -} filter .characters.name = $name; - +} filter .characters.name = $name + and .release_year in array_unpack(>$years); diff --git a/integration-tests/lts/queries.test.ts b/integration-tests/lts/queries.test.ts index f11506433..5efb21f3a 100644 --- a/integration-tests/lts/queries.test.ts +++ b/integration-tests/lts/queries.test.ts @@ -22,7 +22,10 @@ describe("queries", () => { }); test("basic select", async () => { - const result = await getMoviesStarring(client, { name: "Iron Man" }); + const result = await getMoviesStarring(client, { + name: "Iron Man", + years: [2012, 2016] as const, // readonly arrays accepted + }); type result = typeof result; tc.assert< @@ -58,6 +61,7 @@ describe("queries", () => { GetMoviesStarringArgs, { name?: string | null; + years: readonly number[]; } > >(true);