Skip to content

Commit

Permalink
examples: Reduce TypeGen boilerplate in Sanity Example (#69134)
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan authored Aug 26, 2024
1 parent 854a7db commit 74f01e0
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 27 deletions.
6 changes: 3 additions & 3 deletions examples/cms-sanity/app/(blog)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import MoreStories from "./more-stories";
import Onboarding from "./onboarding";
import PortableText from "./portable-text";

import type { HeroQueryResult, SettingsQueryResult } from "@/sanity.types";
import type { HeroQueryResult } from "@/sanity.types";
import * as demo from "@/sanity/lib/demo";
import { sanityFetch } from "@/sanity/lib/fetch";
import { heroQuery, settingsQuery } from "@/sanity/lib/queries";
Expand Down Expand Up @@ -75,10 +75,10 @@ function HeroPost({

export default async function Page() {
const [settings, heroPost] = await Promise.all([
sanityFetch<SettingsQueryResult>({
sanityFetch({
query: settingsQuery,
}),
sanityFetch<HeroQueryResult>({ query: heroQuery }),
sanityFetch({ query: heroQuery }),
]);

return (
Expand Down
2 changes: 1 addition & 1 deletion examples/cms-sanity/app/(blog)/posts/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineQuery } from "groq";
import { defineQuery } from "next-sanity";
import type { Metadata, ResolvingMetadata } from "next";
import { type PortableTextBlock } from "next-sanity";
import Link from "next/link";
Expand Down
17 changes: 8 additions & 9 deletions examples/cms-sanity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,28 @@
"typegen": "sanity schema extract && sanity typegen generate"
},
"dependencies": {
"@sanity/assist": "^3.0.5",
"@sanity/assist": "^3.0.6",
"@sanity/icons": "^3.3.1",
"@sanity/image-url": "^1.0.2",
"@sanity/preview-url-secret": "^1.6.19",
"@sanity/vision": "^3.53.0",
"@tailwindcss/typography": "^0.5.13",
"@sanity/preview-url-secret": "^1.6.20",
"@sanity/vision": "^3.55.0",
"@tailwindcss/typography": "^0.5.14",
"@types/node": "^20.14.13",
"@types/react": "^18.3.3",
"@types/react": "^18.3.4",
"@types/react-dom": "^18.3.0",
"@vercel/speed-insights": "^1.0.12",
"autoprefixer": "^10.4.20",
"date-fns": "^3.6.0",
"groq": "^3.53.0",
"next": "^14.2.5",
"next-sanity": "^9.4.3",
"next-sanity": "^9.4.7",
"postcss": "^8.4.41",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"sanity": "^3.53.0",
"sanity": "^3.55.0",
"sanity-plugin-asset-source-unsplash": "^3.0.1",
"server-only": "^0.0.1",
"styled-components": "^6.1.12",
"tailwindcss": "^3.4.7",
"tailwindcss": "^3.4.10",
"typescript": "5.5.4"
},
"devDependencies": {
Expand Down
1 change: 0 additions & 1 deletion examples/cms-sanity/sanity-typegen.json

This file was deleted.

23 changes: 11 additions & 12 deletions examples/cms-sanity/sanity.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ export type SettingsQueryResult = {
};
} | null;
// Variable: heroQuery
// Query: *[_type == "post" && defined(slug.current)] | order(date desc, _updatedAt desc) [0] { content, _id, "status": select(_originalId in path("drafts.**") => "draft", "published"), "title": coalesce(title, "Untitled"), "slug": slug.current, excerpt, coverImage, "date": coalesce(date, _updatedAt), "author": author->{"name": coalesce(name, "Anonymous"), picture}, }
// Query: *[_type == "post" && defined(slug.current)] | order(date desc, _updatedAt desc) [0] { content, _id, "status": select(_originalId in path("drafts.**") => "draft", "published"), "title": coalesce(title, "Untitled"), "slug": slug.current, excerpt, coverImage, "date": coalesce(date, _updatedAt), "author": author->{"name": coalesce(name, "Anonymous"), picture}, }
export type HeroQueryResult = {
content: Array<{
children?: Array<{
Expand Down Expand Up @@ -537,7 +537,7 @@ export type HeroQueryResult = {
} | null;
} | null;
// Variable: moreStoriesQuery
// Query: *[_type == "post" && _id != $skip && defined(slug.current)] | order(date desc, _updatedAt desc) [0...$limit] { _id, "status": select(_originalId in path("drafts.**") => "draft", "published"), "title": coalesce(title, "Untitled"), "slug": slug.current, excerpt, coverImage, "date": coalesce(date, _updatedAt), "author": author->{"name": coalesce(name, "Anonymous"), picture}, }
// Query: *[_type == "post" && _id != $skip && defined(slug.current)] | order(date desc, _updatedAt desc) [0...$limit] { _id, "status": select(_originalId in path("drafts.**") => "draft", "published"), "title": coalesce(title, "Untitled"), "slug": slug.current, excerpt, coverImage, "date": coalesce(date, _updatedAt), "author": author->{"name": coalesce(name, "Anonymous"), picture}, }
export type MoreStoriesQueryResult = Array<{
_id: string;
status: "draft" | "published";
Expand Down Expand Up @@ -574,7 +574,7 @@ export type MoreStoriesQueryResult = Array<{
} | null;
}>;
// Variable: postQuery
// Query: *[_type == "post" && slug.current == $slug] [0] { content, _id, "status": select(_originalId in path("drafts.**") => "draft", "published"), "title": coalesce(title, "Untitled"), "slug": slug.current, excerpt, coverImage, "date": coalesce(date, _updatedAt), "author": author->{"name": coalesce(name, "Anonymous"), picture}, }
// Query: *[_type == "post" && slug.current == $slug] [0] { content, _id, "status": select(_originalId in path("drafts.**") => "draft", "published"), "title": coalesce(title, "Untitled"), "slug": slug.current, excerpt, coverImage, "date": coalesce(date, _updatedAt), "author": author->{"name": coalesce(name, "Anonymous"), picture}, }
export type PostQueryResult = {
content: Array<{
children?: Array<{
Expand Down Expand Up @@ -628,23 +628,22 @@ export type PostQueryResult = {
} | null;
} | null;
} | null;
import "@sanity/client";
declare module "@sanity/client" {
interface SanityQueries {
'*[_type == "settings"][0]': SettingsQueryResult;
'\n *[_type == "post" && defined(slug.current)] | order(date desc, _updatedAt desc) [0] {\n content,\n \n _id,\n "status": select(_originalId in path("drafts.**") => "draft", "published"),\n "title": coalesce(title, "Untitled"),\n "slug": slug.current,\n excerpt,\n coverImage,\n "date": coalesce(date, _updatedAt),\n "author": author->{"name": coalesce(name, "Anonymous"), picture},\n\n }\n': HeroQueryResult;
'\n *[_type == "post" && _id != $skip && defined(slug.current)] | order(date desc, _updatedAt desc) [0...$limit] {\n \n _id,\n "status": select(_originalId in path("drafts.**") => "draft", "published"),\n "title": coalesce(title, "Untitled"),\n "slug": slug.current,\n excerpt,\n coverImage,\n "date": coalesce(date, _updatedAt),\n "author": author->{"name": coalesce(name, "Anonymous"), picture},\n\n }\n': MoreStoriesQueryResult;
'\n *[_type == "post" && slug.current == $slug] [0] {\n content,\n \n _id,\n "status": select(_originalId in path("drafts.**") => "draft", "published"),\n "title": coalesce(title, "Untitled"),\n "slug": slug.current,\n excerpt,\n coverImage,\n "date": coalesce(date, _updatedAt),\n "author": author->{"name": coalesce(name, "Anonymous"), picture},\n\n }\n': PostQueryResult;
}
} // Source: ./app/(blog)/posts/[slug]/page.tsx

// Source: ./app/(blog)/posts/[slug]/page.tsx
// Variable: postSlugs
// Query: *[_type == "post" && defined(slug.current)]{"slug": slug.current}
export type PostSlugsResult = Array<{
slug: string | null;
}>;

// Query TypeMap
import "@sanity/client";
declare module "@sanity/client" {
interface SanityQueries {
'*[_type == "settings"][0]': SettingsQueryResult;
'\n *[_type == "post" && defined(slug.current)] | order(date desc, _updatedAt desc) [0] {\n content,\n \n _id,\n "status": select(_originalId in path("drafts.**") => "draft", "published"),\n "title": coalesce(title, "Untitled"),\n "slug": slug.current,\n excerpt,\n coverImage,\n "date": coalesce(date, _updatedAt),\n "author": author->{"name": coalesce(name, "Anonymous"), picture},\n\n }\n': HeroQueryResult;
'\n *[_type == "post" && _id != $skip && defined(slug.current)] | order(date desc, _updatedAt desc) [0...$limit] {\n \n _id,\n "status": select(_originalId in path("drafts.**") => "draft", "published"),\n "title": coalesce(title, "Untitled"),\n "slug": slug.current,\n excerpt,\n coverImage,\n "date": coalesce(date, _updatedAt),\n "author": author->{"name": coalesce(name, "Anonymous"), picture},\n\n }\n': MoreStoriesQueryResult;
'\n *[_type == "post" && slug.current == $slug] [0] {\n content,\n \n _id,\n "status": select(_originalId in path("drafts.**") => "draft", "published"),\n "title": coalesce(title, "Untitled"),\n "slug": slug.current,\n excerpt,\n coverImage,\n "date": coalesce(date, _updatedAt),\n "author": author->{"name": coalesce(name, "Anonymous"), picture},\n\n }\n': PostQueryResult;
'*[_type == "post" && defined(slug.current)]{"slug": slug.current}': PostSlugsResult;
}
}
2 changes: 1 addition & 1 deletion examples/cms-sanity/sanity/lib/queries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineQuery } from "groq";
import { defineQuery } from "next-sanity";

export const settingsQuery = defineQuery(`*[_type == "settings"][0]`);

Expand Down

0 comments on commit 74f01e0

Please sign in to comment.