Skip to content

Commit

Permalink
`types(groq-builder): use @sanity-typed/types to infer schema
Browse files Browse the repository at this point in the history
  • Loading branch information
scottrippey committed Dec 7, 2023
1 parent c4b20dc commit c4333f4
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 351 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"lint-staged": "^13.1.0",
"prettier": "^2.8.2",
"start-server-and-test": "^1.15.2",
"typescript": "4.9.4"
"typescript": "^5.3.3"
},
"lint-staged": {
"*.{js,jsx,ts,tsx,css,md,json}": [
Expand Down
4 changes: 3 additions & 1 deletion packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
},
"devDependencies": {
"@portabletext/types": "^2.0.0",
"@sanity-typed/types": "^5.8.13",
"@sanity/cli": "^3.9.1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
Expand All @@ -75,7 +76,8 @@
"jest-environment-jsdom": "^29.3.1",
"msw": "^0.47.4",
"postcss": "^8.4.16",
"tailwindcss": "^3.1.8"
"tailwindcss": "^3.1.8",
"ts-simplify": "^0.1.2"
},
"msw": {
"workerDirectory": "public"
Expand Down
8 changes: 6 additions & 2 deletions packages/nextjs/sanity-studio/sanity.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig } from "sanity";
import { defineConfig, InferSchemaValues } from "@sanity-typed/types";
import { deskTool } from "sanity/desk";
import { visionTool } from "@sanity/vision";
import { dashboardTool, projectUsersWidget, projectInfoWidget } from "@sanity/dashboard";
Expand All @@ -7,7 +7,7 @@ import { groqdPlaygroundTool } from "groqd-playground";
import schemaTypes from "./schemas/schema";
import { customStructure } from "./deskStructure";

export default defineConfig({
export const config = defineConfig({
projectId: "5bsv02jj",
dataset: "production",
basePath: "/studio",
Expand All @@ -23,3 +23,7 @@ export default defineConfig({
types: schemaTypes,
},
});

export default config;

export type SchemaValues = InferSchemaValues<typeof config>;
8 changes: 4 additions & 4 deletions packages/nextjs/sanity-studio/schemas/category.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MdCategory } from "react-icons/md";
import { defineField, defineType } from "sanity";
import { defineArrayMember, defineField, defineType } from "@sanity-typed/types";

export default defineType({
name: "category",
Expand Down Expand Up @@ -33,10 +33,10 @@ export default defineType({
title: "Images",
type: "array",
of: [
{
defineArrayMember({
type: "reference",
to: [{ type: "categoryImage" }],
},
to: [{ type: "categoryImage" } as const],
}),
],
}),
],
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/sanity-studio/schemas/categoryImage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BsFillImageFill } from "react-icons/bs";
import { defineType, defineField } from "sanity";
import { defineType, defineField } from "@sanity-typed/types";

export default defineType({
name: "categoryImage",
Expand Down
6 changes: 3 additions & 3 deletions packages/nextjs/sanity-studio/schemas/description.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { defineType } from "sanity";
import { defineArrayMember, defineType } from "@sanity-typed/types";

export default defineType({
name: "description",
title: "Description",
type: "array",
of: [
{
defineArrayMember({
type: "block",
},
}),
],
});
2 changes: 1 addition & 1 deletion packages/nextjs/sanity-studio/schemas/flavour.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GiSaltShaker } from "react-icons/gi";
import { defineField, defineType } from "sanity";
import { defineField, defineType } from "@sanity-typed/types";

export default defineType({
name: "flavour",
Expand Down
14 changes: 7 additions & 7 deletions packages/nextjs/sanity-studio/schemas/product.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MdShoppingCart } from "react-icons/md";
import { defineField, defineType } from "sanity";
import { defineArrayMember, defineField, defineType } from "@sanity-typed/types";

export default defineType({
name: "product",
Expand Down Expand Up @@ -33,10 +33,10 @@ export default defineType({
title: "Categories",
type: "array",
of: [
{
defineArrayMember({
type: "reference",
to: [{ type: "category" }],
},
to: [{ type: "category" } as const],
}),
],
}),
defineField({
Expand All @@ -56,10 +56,10 @@ export default defineType({
title: "Variants",
type: "array",
of: [
{
defineArrayMember({
type: "reference",
to: [{ type: "variant" }],
},
to: [{ type: "variant" } as const],
}),
],
},
],
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/sanity-studio/schemas/productImage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BsFillImageFill } from "react-icons/bs";
import { defineField, defineType } from "sanity";
import { defineField, defineType } from "@sanity-typed/types";

export default defineType({
name: "productImage",
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/sanity-studio/schemas/siteSettings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineField, defineType } from "sanity";
import { defineField, defineType } from "@sanity-typed/types";

export default defineType({
name: "siteSettings",
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/sanity-studio/schemas/style.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RiKnifeLine } from "react-icons/ri";
import { defineField, defineType } from "sanity";
import { defineField, defineType } from "@sanity-typed/types";

export default defineType({
name: "style",
Expand Down
21 changes: 11 additions & 10 deletions packages/nextjs/sanity-studio/schemas/variant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GrMultiple } from "react-icons/gr";
import groq from "groq";
import { defineField, ValidationContext } from "sanity";
import { ValidationContext } from "sanity";
import { defineArrayMember, defineField, defineType } from "@sanity-typed/types";
import { sanityClient } from "utils/sanityClient";

const isUniqueId = (value: string, context: ValidationContext) => {
Expand All @@ -21,7 +22,7 @@ const isUniqueId = (value: string, context: ValidationContext) => {
return sanityClient.fetch(query, params);
};

export default defineField({
export default defineType({
name: "variant",
title: "Variant",
description: "Variant of the product",
Expand Down Expand Up @@ -79,33 +80,33 @@ export default defineField({
title: "Images",
type: "array",
of: [
{
defineArrayMember({
name: "productImage",
title: "Product Image",
type: "productImage",
},
}),
],
}),
defineField({
name: "flavour",
title: "Flavour",
type: "array",
of: [
{
defineArrayMember({
type: "reference",
to: [{ type: "flavour" }],
},
to: [{ type: "flavour" } as const],
}),
],
}),
defineField({
name: "style",
title: "Style (options)",
type: "array",
of: [
{
defineArrayMember({
type: "reference",
to: [{ type: "style" }],
},
to: [{ type: "style" } as const],
}),
],
}),
],
Expand Down
Loading

0 comments on commit c4333f4

Please sign in to comment.