-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d7a41db
commit 7684c96
Showing
7 changed files
with
177 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
<script lang="ts"> | ||
import { page } from '$app/stores'; | ||
import Post from './components/Post.svelte'; | ||
import PostBox from './components/PostBox.svelte'; | ||
</script> | ||
|
||
<div class="px-4 pt-6 space-y-6 sm:w-full lg:w-1/2"> | ||
Welcome back {$page.data.user.name}! | ||
<PostBox /> | ||
{#each $page.data.posts || [] as { node: post }} | ||
<Post {post} /> | ||
{/each} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { PostEdge } from '$lib/graphql/schema.js'; | ||
|
||
/** @type {import('./$types').PageLoad} */ | ||
export async function load({ fetch }) { | ||
const response = await fetch('/get-posts'); | ||
const data = await response.json(); | ||
return { | ||
posts: data as PostEdge[], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,22 @@ | ||
<script lang="ts"> | ||
import Card from '$lib/components/Card.svelte'; | ||
import type { Post } from '$lib/graphql/schema'; | ||
import Avatar from './Navbar/Avatar.svelte'; | ||
export let post: any = { | ||
id: 1, | ||
title: 'title', | ||
content: 'body', | ||
createdAt: '24/12/2023', | ||
user: { | ||
id: 1, | ||
name: 'Michael', | ||
surname: 'Liendo', | ||
username: 'MichaelLiendo', | ||
}, | ||
}; | ||
// import Avatar from './Navbar/Avatar.svelte'; | ||
export let post: Post; | ||
</script> | ||
|
||
<Card> | ||
<header class="flex items-center justify-start"> | ||
<Avatar user={post.user} size="md" /> | ||
<!-- <Avatar user={{ name: 'John', surname: 'Doe' }} size="md" /> --> | ||
<div class="flex flex-col px-2"> | ||
<strong class="text-gray-800">{post.user.username}</strong> | ||
<h1 class="text-gray-800">{post.title}</h1> | ||
<span class="text-gray-400 text-sm" | ||
>{new Date(post.createdAt).toLocaleDateString()}</span | ||
> | ||
</div> | ||
</header> | ||
<main class="pt-4"> | ||
<h1 class="text-2xl font-bold">{post.title}</h1> | ||
<p>{post.content}</p> | ||
<p class="text-lg">{post.content}</p> | ||
</main> | ||
</Card> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { GetPostsDocument, type PostConnection } from '$lib/graphql/schema'; | ||
import { Client, cacheExchange, createClient, fetchExchange } from '@urql/core'; | ||
|
||
async function getPosts(urqlClient: Client) { | ||
const response = await urqlClient | ||
.query(GetPostsDocument, {}, { requestPolicy: 'network-only' }) | ||
.toPromise(); | ||
|
||
if (response?.error || response?.data?.posts?.error) { | ||
if (response?.data?.posts?.error) { | ||
const error = response?.data?.posts?.error; | ||
|
||
throw new Error(error); | ||
} | ||
|
||
throw response?.error; | ||
} | ||
|
||
return (response?.data?.posts as PostConnection).edges; | ||
} | ||
|
||
export const GET = async ({ request }: { request: Request }) => { | ||
try { | ||
const urqlClient = createClient({ | ||
url: import.meta.env.VITE_GRAPHQL_URL, | ||
exchanges: [cacheExchange, fetchExchange], | ||
}); | ||
|
||
const posts = await getPosts(urqlClient); | ||
|
||
return new Response(JSON.stringify(posts), { status: 200 }); | ||
} catch (err) { | ||
console.log(err); | ||
|
||
return new Response( | ||
JSON.stringify({ | ||
message: 'Internal Server Error', | ||
error: (err as { message: string })?.message, | ||
}), | ||
{ status: 500 }, | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
query GetPosts { | ||
posts { | ||
edges { | ||
node { | ||
...CurrentPost | ||
} | ||
} | ||
pageInfo { | ||
hasPreviousPage | ||
hasNextPage | ||
startCursor | ||
endCursor | ||
} | ||
nodes { | ||
...CurrentPost | ||
} | ||
} | ||
} | ||
|
||
fragment CurrentPost on Post { | ||
id | ||
authorId | ||
parentId | ||
head | ||
title | ||
content | ||
createdAt | ||
updatedAt | ||
} |