Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Projection or ArrayPostfix on nullable array #642

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
201 changes: 201 additions & 0 deletions packages/groq/src/specific-issues.test.ts
Expand Up @@ -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";

Expand Down Expand Up @@ -95,4 +103,197 @@ describe("specific issues", () => {
>
>().toStrictEqual<WritableDeep<typeof expectedResult>>();
});

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<Parse<typeof query>>().toStrictEqual<
WritableDeep<typeof expectedTree>
>();

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<typeof config>["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<typeof dataset>;
}>
>
>().toStrictEqual<WritableDeep<typeof expectedResult>>();
});
});
2 changes: 1 addition & 1 deletion packages/types/src/specific-issues.test.ts
Expand Up @@ -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",
Expand Down