Abstract graphql querying logic into its own function

This commit is contained in:
Zeph Levy 2026-01-14 12:05:39 +01:00
parent b0f8d2c6c8
commit 63c50f2d93

View file

@ -23,7 +23,10 @@ export async function list_datasets(): Promise<Dataset[]> {
} }
} }
`; `;
return (await query_graphql<{datasets: Dataset[]}>(query)).datasets;
}
async function query_graphql<T>(query: string): Promise<T> {
const response = await fetch('http://host.containers.internal:3000/graphql', { const response = await fetch('http://host.containers.internal:3000/graphql', {
method: 'POST', method: 'POST',
headers: { headers: {
@ -36,14 +39,11 @@ export async function list_datasets(): Promise<Dataset[]> {
throw new Error(`Error - status ${response.status}`); throw new Error(`Error - status ${response.status}`);
} }
const result = await response.json() as GraphQLResponse<{ const result = await response.json() as GraphQLResponse<T>;
datasets: Dataset[];
}>;
if (result.errors) { if (result.errors) {
throw new Error(result.errors.map(e => e.message).join(', ')); throw new Error(result.errors.map(e => e.message).join(', '));
} }
return result.data!.datasets; return result.data!;
} }