25 lines
513 B
TypeScript
25 lines
513 B
TypeScript
/**
|
|
* Shared GraphQL client for all MPR UI apps.
|
|
*/
|
|
|
|
const GRAPHQL_URL = "/api/graphql";
|
|
|
|
export async function gql<T>(
|
|
query: string,
|
|
variables?: Record<string, unknown>,
|
|
): Promise<T> {
|
|
const response = await fetch(GRAPHQL_URL, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ query, variables }),
|
|
});
|
|
|
|
const json = await response.json();
|
|
|
|
if (json.errors?.length) {
|
|
throw new Error(json.errors[0].message);
|
|
}
|
|
|
|
return json.data as T;
|
|
}
|