diff --git a/packages/groq/src/specific-issues.test.ts b/packages/groq/src/specific-issues.test.ts index 9a98adc4..c956ed05 100644 --- a/packages/groq/src/specific-issues.test.ts +++ b/packages/groq/src/specific-issues.test.ts @@ -3,6 +3,14 @@ import { expectType } from "@saiichihashimoto/test-utils"; import { evaluate, parse } from "groq-js"; import type { WritableDeep } from "type-fest"; +import { + defineArrayMember, + defineConfig, + defineField, + defineType, +} from "@sanity-typed/types"; +import type { InferSchemaValues } from "@sanity-typed/types"; + import type { ExecuteQuery, Parse } from "."; import type { ScopeFromPartialContext } from "./internal"; @@ -95,4 +103,197 @@ describe("specific issues", () => { > >().toStrictEqual>(); }); + + it("#641 FIXME", async () => { + // https://github.com/saiichihashimoto/sanity-typed/issues/641 + const query = ` + *[_type == "foo" && type == "links"]{ + _id, + title, + links[]{ + _key, + title, + url, + target + } + } +`; + + const tree = parse(query); + + const expectedTree = { + base: { + base: { type: "Everything" }, + expr: { + left: { + left: { name: "_type", type: "AccessAttribute" }, + op: "==", + right: { type: "Value", value: "foo" }, + type: "OpCall", + }, + right: { + left: { name: "type", type: "AccessAttribute" }, + op: "==", + right: { type: "Value", value: "links" }, + type: "OpCall", + }, + type: "And", + }, + type: "Filter", + }, + expr: { + base: { type: "This" }, + expr: { + attributes: [ + { + name: "_id", + type: "ObjectAttributeValue", + value: { name: "_id", type: "AccessAttribute" }, + }, + { + name: "title", + type: "ObjectAttributeValue", + value: { name: "title", type: "AccessAttribute" }, + }, + { + name: "links", + type: "ObjectAttributeValue", + value: { + base: { + base: { name: "links", type: "AccessAttribute" }, + type: "ArrayCoerce", + }, + expr: { + base: { type: "This" }, + expr: { + attributes: [ + { + name: "_key", + type: "ObjectAttributeValue", + value: { name: "_key", type: "AccessAttribute" }, + }, + { + name: "title", + type: "ObjectAttributeValue", + value: { name: "title", type: "AccessAttribute" }, + }, + { + name: "url", + type: "ObjectAttributeValue", + value: { name: "url", type: "AccessAttribute" }, + }, + { + name: "target", + type: "ObjectAttributeValue", + value: { name: "target", type: "AccessAttribute" }, + }, + ], + type: "Object", + }, + type: "Projection", + }, + type: "Map", + }, + }, + ], + type: "Object", + }, + type: "Projection", + }, + type: "Map", + } as const; + + expect(tree).toStrictEqual(expectedTree); + expectType>().toStrictEqual< + WritableDeep + >(); + + const config = defineConfig({ + dataset: "dataset", + projectId: "projectId", + schema: { + types: [ + defineType({ + name: "foo", + type: "document", + fields: [ + defineField({ + name: "type", + type: "string", + options: { + list: [ + { title: "About", value: "about" }, + { title: "Social", value: "social" }, + { title: "Links", value: "links" }, + ], + }, + validation: (Rule) => Rule.required(), + }), + defineField({ + name: "title", + type: "string", + validation: (Rule) => Rule.required(), + }), + defineField({ + name: "links", + type: "array", + of: [ + defineArrayMember({ + type: "object", + fields: [ + defineField({ + name: "title", + type: "string", + }), + defineField({ + name: "url", + type: "string", + }), + defineField({ + name: "target", + type: "string", + options: { + list: [ + { title: "Self", value: "_self" }, + { title: "Blank", value: "_blank" }, + ], + }, + }), + ], + }), + ], + }), + ], + }), + ], + }, + }); + + const dataset: InferSchemaValues["foo"][] = []; + + const result = await (await evaluate(tree, { dataset })).get(); + + const expectedResult = [] as { + _id: string; + links: + | { + _key: string; + target: "_blank" | "_self" | null; + title: string | null; + url: string | null; + }[] + | null; + title: string; + }[]; + + expect(result).toStrictEqual(expectedResult); + expectType< + ExecuteQuery< + typeof query, + ScopeFromPartialContext<{ + dataset: WritableDeep; + }> + > + >().toStrictEqual>(); + }); }); diff --git a/packages/types/src/specific-issues.test.ts b/packages/types/src/specific-issues.test.ts index 5f8b37f8..3bf181a7 100644 --- a/packages/types/src/specific-issues.test.ts +++ b/packages/types/src/specific-issues.test.ts @@ -173,7 +173,7 @@ describe("specific issues", () => { }>(); }); - it("#589 object -> array -> block -> object -> field", async () => { + it("#589 object -> array -> block -> object -> field", () => { const config = defineConfig({ dataset: "dataset", projectId: "projectId",