c2afb3d3b5
Release package / release (push) Successful in 7m28s
Co-authored-by: Thanh Quy - wolf <524H0124@student.tdtu.edu.vn> Reviewed-on: #37 Co-authored-by: TaNguyenThanhQuy <tanguyenthanhquy@noreply.localhost> Co-committed-by: TaNguyenThanhQuy <tanguyenthanhquy@noreply.localhost>
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import type { Eatery } from "./types";
|
|
|
|
export async function gqlFetch<T = Record<string, unknown>>(
|
|
query: string,
|
|
variables?: Record<string, unknown>,
|
|
): Promise<T> {
|
|
const res = await fetch("/api/eatery/graphql", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
credentials: "include",
|
|
body: JSON.stringify({ query, variables }),
|
|
});
|
|
|
|
if (!res.ok) throw new Error(`GraphQL request failed: ${res.status}`);
|
|
return res.json() as Promise<T>;
|
|
}
|
|
|
|
export async function addManagerMenuItem(
|
|
name: string,
|
|
price: number,
|
|
): Promise<{ id: string; name: string; price: number } | null> {
|
|
const data = await gqlFetch<{
|
|
addMenuItem: { id: string; name: string; price: number } | null;
|
|
}>(
|
|
`mutation($name: String!, $price: Float!) {
|
|
addMenuItem(name: $name, price: $price) { id name price }
|
|
}`,
|
|
{ name, price },
|
|
);
|
|
return data.addMenuItem;
|
|
}
|
|
|
|
export async function fetchManagerEatery(): Promise<Eatery | null> {
|
|
const data = await gqlFetch<{ eateriesByOwner: Eatery | null }>(`
|
|
query {
|
|
eateriesByOwner {
|
|
id
|
|
ownerId
|
|
name
|
|
isVerified
|
|
}
|
|
}
|
|
`);
|
|
return data.eateriesByOwner;
|
|
}
|