Abstract graphql querying logic into its own function
This commit is contained in:
parent
b0f8d2c6c8
commit
63c50f2d93
1 changed files with 6 additions and 6 deletions
|
|
@ -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!;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue