Make backend and frontend work together well, and add datasets/ page to frontend
This commit is contained in:
parent
a5cda1d51c
commit
b0f8d2c6c8
8 changed files with 107 additions and 5 deletions
49
frontend/src/lib/server/backend.ts
Normal file
49
frontend/src/lib/server/backend.ts
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
type GraphQLResponse<T> = {
|
||||
data?: T;
|
||||
errors?: Array<{
|
||||
message: string;
|
||||
locations?: { line: number; column: number }[];
|
||||
path?: string[];
|
||||
}>;
|
||||
};
|
||||
|
||||
export type Dataset = {
|
||||
id: number;
|
||||
name: string;
|
||||
description: string;
|
||||
};
|
||||
|
||||
export async function list_datasets(): Promise<Dataset[]> {
|
||||
const query = `
|
||||
query {
|
||||
datasets {
|
||||
id
|
||||
name
|
||||
description
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const response = await fetch('http://host.containers.internal:3000/graphql', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ query }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Error - status ${response.status}`);
|
||||
}
|
||||
|
||||
const result = await response.json() as GraphQLResponse<{
|
||||
datasets: Dataset[];
|
||||
}>;
|
||||
|
||||
if (result.errors) {
|
||||
throw new Error(result.errors.map(e => e.message).join(', '));
|
||||
}
|
||||
|
||||
return result.data!.datasets;
|
||||
}
|
||||
|
||||
9
frontend/src/routes/datasets/+page.server.ts
Normal file
9
frontend/src/routes/datasets/+page.server.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import * as backend from '$lib/server/backend';
|
||||
import type { PageServerLoad } from './$types';
|
||||
|
||||
export const load: PageServerLoad = async ({ params }) => {
|
||||
const datasets: backend.Dataset[] = await backend.list_datasets()
|
||||
return {
|
||||
datasets
|
||||
};
|
||||
};
|
||||
14
frontend/src/routes/datasets/+page.svelte
Normal file
14
frontend/src/routes/datasets/+page.svelte
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<script lang="ts">
|
||||
import type { PageProps } from "./$types";
|
||||
|
||||
let { data }: PageProps = $props();
|
||||
</script>
|
||||
|
||||
<ul>
|
||||
{#each data.datasets as dataset}
|
||||
<li>
|
||||
<strong>{dataset.name}</strong><br />
|
||||
{dataset.description}
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
Loading…
Add table
Add a link
Reference in a new issue