Skip to content

Commit 20158df

Browse files
committed
migrate sveltekit
1 parent 30b9953 commit 20158df

File tree

5 files changed

+226
-147
lines changed

5 files changed

+226
-147
lines changed
File renamed without changes.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<script lang="ts">
2+
import { browser } from '$app/env';
3+
import Pane from './../lib/components/Pane.svelte';
4+
5+
/** @type {import('./$types').PageData}*/
6+
export let data;
7+
8+
9+
if (browser) {
10+
import('svimg/dist/s-image');
11+
}
12+
13+
export let error = null;
14+
if (error) console.error(error);
15+
16+
function joinAuthors(authors) {
17+
return authors?.map((author) => author.name).join(', ');
18+
}
19+
</script>
20+
21+
<div class="grid grid-cols-2 divide-x-2 divide-black">
22+
<Pane label="JSON Output">
23+
<pre class="overflow-auto p-3" style="height: calc(100vh - 3.5rem);">
24+
<code class="text-sm">
25+
{JSON.stringify(data, null, 2)}
26+
</code>
27+
</pre>
28+
</Pane>
29+
<Pane label="Frontend">
30+
{#each data.allPostCategories as post, _ (post.id)}
31+
<article class="m-3 border border-opacity-20 bg-gray-50 p-3">
32+
<h3 class="text-xl font-medium">{post.title}</h3>
33+
<ul>
34+
<li class="text-xs font-semibold text-gray-500">
35+
<div class="flex">
36+
{#each post.authors as author}
37+
<div>
38+
<div class="h-12 w-12">
39+
<s-image {...author.image} />
40+
</div>
41+
{author.name}
42+
</div>
43+
{/each}
44+
</div>
45+
</li>
46+
<li class="text-xs font-semibold text-gray-500">
47+
Rating: {post.rating}
48+
</li>
49+
</ul>
50+
<div>{@html post._content.html}</div>
51+
</article>
52+
{/each}
53+
</Pane>
54+
</div>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { error } from '@sveltejs/kit';
2+
3+
export const load = async ({ fetch }) => {
4+
const query = `
5+
query PostCategory {
6+
allPostCategories (sortBy: "title", order: DESC) {
7+
_metadata {
8+
sourceContext {
9+
filename
10+
slug
11+
}
12+
collection
13+
}
14+
id
15+
title
16+
category
17+
slug
18+
rating
19+
_content {
20+
raw
21+
html
22+
excerpt
23+
timeToRead
24+
}
25+
authors {
26+
_metadata {
27+
sourceContext {
28+
slug
29+
}
30+
}
31+
id
32+
name
33+
entity
34+
enjoys
35+
image {
36+
srcset
37+
srcsetwebp
38+
srcsetavif
39+
placeholder
40+
aspectratio
41+
}
42+
friend {
43+
name
44+
date_joined
45+
}
46+
date_joined
47+
skills {
48+
sitting
49+
breathing
50+
liquid_consumption
51+
existence
52+
sports
53+
}
54+
}
55+
}
56+
}
57+
`;
58+
59+
try {
60+
const response = await fetch('http://localhost:5057/graphql', {
61+
body: JSON.stringify({
62+
query,
63+
}),
64+
headers: {
65+
'Content-Type': 'application/json',
66+
Accept: 'application/json',
67+
},
68+
method: 'POST',
69+
});
70+
71+
const { data, errors } = await response.json();
72+
73+
if (errors)
74+
throw error(500, errors.map(({ message }) => message).join('\\n'));
75+
return data;
76+
} catch (e) {
77+
throw error(500, 'Failed to load data');
78+
}
79+
};

examples/sveltekit/src/routes/index.svelte

Lines changed: 0 additions & 143 deletions
This file was deleted.

0 commit comments

Comments
 (0)