Skip to content

Commit

Permalink
refactor: try groqd for typed sanity queries
Browse files Browse the repository at this point in the history
  • Loading branch information
jbranchaud committed Dec 21, 2023
1 parent 0308afc commit c797622
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 66 deletions.
1 change: 1 addition & 0 deletions apps/course-builder-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"drizzle-orm": "^0.28.5",
"framer-motion": "^10.16.5",
"groq": "^3.18.1",
"groqd": "^0.15.10",
"i": "^0.3.7",
"inngest": "^3.7.0",
"intl-segmenter-polyfill": "^0.4.4",
Expand Down
9 changes: 7 additions & 2 deletions apps/course-builder-web/src/app/tips/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ import {TipPlayer} from '@/app/tips/_components/tip-player'
import * as React from 'react'
import {getServerAuthSession} from '@/server/auth'
import {getAbility} from '@/lib/ability'
import {getTip} from '@/lib/tips'
import {getTip, getTipGroqD} from '@/lib/tips'
import ReactMarkdown from 'react-markdown'
import {notFound} from 'next/navigation'

export default async function TipPage({params}: {params: {slug: string}}) {
const session = await getServerAuthSession()
const ability = getAbility({user: session?.user})
const tip = await getTip(params.slug)
const tip = await getTipGroqD(params.slug)

if (!tip) {
notFound()
}

return (
<div>
Expand Down
33 changes: 32 additions & 1 deletion apps/course-builder-web/src/lib/tips.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {z} from 'zod'
import {sanityQuery} from '@/server/sanity.server'
import {sanityQuery, runQuery} from '@/server/sanity.server'
import {q} from 'groqd'

const TipSchema = z.object({
_id: z.string(),
Expand Down Expand Up @@ -31,6 +32,36 @@ export async function getTip(slugOrId: string) {
}`)
}

export async function getTipGroqD(slugOrId: string) {
const fromVideoResource = q('resources')
.filter("@->._type == 'videoResource'")
.slice(0)
.deref()

const groqQuery = q('*')
.filter(
`_type == "tip" && (_id == "${slugOrId}" || slug.current == "${slugOrId}")`,
)
.grab({
_id: q.string(),
_type: q.string(),
_updatedAt: q.string(),
title: q.string(),
summary: q.string().nullable(),
body: q.string().nullable(),
muxPlaybackId: fromVideoResource.grabOne('muxPlaybackId', q.string()),
videoResourceId: fromVideoResource.grabOne('_id', q.string()),
transcript: fromVideoResource.grabOne('transcript', q.string()),
slug: ['slug.current', q.string()],
})
.slice(0)
.nullable()

console.log({groqQuery})

return await runQuery(groqQuery)
}

export async function getTipsModule() {
return await sanityQuery<{
_id: string
Expand Down
18 changes: 17 additions & 1 deletion apps/course-builder-web/src/server/sanity.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {env} from '@/env.mjs'
import {Logger} from 'next-axiom'
import {makeSafeQueryRunner} from 'groqd'

export async function sanityMutation(
mutations: any[],
Expand Down Expand Up @@ -44,6 +45,16 @@ export async function sanityQuery<T = any>(
revalidate: 10,
},
): Promise<T> {
return (await sanityQueryWithFetch(query, options)) as T
}

async function sanityQueryWithFetch(
query: string,
options: {useCdn?: boolean; revalidate?: number} = {
useCdn: true,
revalidate: 10,
},
): Promise<any> {
const log = new Logger()
return await fetch(
`https://${env.SANITY_STUDIO_PROJECT_ID}.${
Expand All @@ -68,7 +79,7 @@ export async function sanityQuery<T = any>(
)
}
const {result} = await response.json()
return result as T
return result
})
.catch((error) => {
log.error(error)
Expand All @@ -78,3 +89,8 @@ export async function sanityQuery<T = any>(
log.flush()
})
}

// 👇 Safe query runner
export const runQuery = makeSafeQueryRunner((query) =>
sanityQueryWithFetch(query),
)
107 changes: 45 additions & 62 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c797622

Please sign in to comment.