still writing groq queries.

This commit is contained in:
christian 2024-04-28 21:10:38 +02:00
parent eead28f05a
commit c2c2ad29eb
3 changed files with 25 additions and 50 deletions

View File

@ -2,30 +2,24 @@
* Example of how you could re-use GROQ queries across different contexts with Javascript.
* As your schema evolves, this pattern will be useful to keep your data in sync across all surfaces.
*/
export function getPostsQuery() {
export async function getPostsQuery() {
const data = await client.fetch(`*[_type == "post"] {
title,
slug,
coverImage {
...,
asset->
},
body,
}`);
return `*[_type == "post"] {
_id,
_type,
_createdAt,
_updatedAt,
title,
slug,
coverImage {
...,
asset->
},
duration {
start,
end
},
client,
site,
tags[],
body
}`
if (data) {
return {
posts: data
};
}
return {
status: 500,
body: new Error("Internal Server Error")
};
}

View File

@ -1,24 +1,4 @@
import { client } from '$lib/sanity/client.js'
// import { getPostsQuery } from '$lib/queries.js'
// // Fetch all valid posts & authors to display in the homepage
// export async function load() {
// const data = await client.fetch(/* groq */ `{
// "posts": ${getPostsQuery()},
// }`)
// if (data) {
// return {
// status: 200,
// body: data
// }
// }
// return {
// status: 404
// }
// }
export async function load({ params }) {
const data = await client.fetch(`*[_type == "post"] {

View File

@ -1,14 +1,15 @@
<script>
import { text } from "@sveltejs/kit";
export let data;
function setElement(style, text) {
const targetElement =
}
</script>
{#each data.posts as post}
<h2>{post.title}</h2>
{#each post.body as block}
{#if }
{/if}
<>{block}</p>
{#each post.body as block, i}
<p id="post-text-{i}">{setElement(block.style, block.children[0].text)}</p>
{/each}
<img src="{post.coverImage.asset.url}" alt="yes">