Second frontend, written in Next.JS + Typescript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1022 B

4 years ago
  1. import compress from "gql-compress";
  2. class GQLBrowserClient implements GQLClient {
  3. async post(query: string, variables?: {[x:string]: any}, operationName?: string): Promise<GQLRespone> {
  4. const res = await fetch(`/api/graphql`, {
  5. method: "POST",
  6. body: JSON.stringify({query, variables, operationName}),
  7. credentials: "include",
  8. headers: {
  9. "Content-Type": "application/json",
  10. },
  11. })
  12. const json = await res.json();
  13. if (json.errors) {
  14. throw new Error(`${json.errors[0].message} (${json.errors[0].path})`)
  15. }
  16. return json.data
  17. }
  18. }
  19. export interface GQLClient {
  20. post(query: string, variables?: {[x:string]: any}, operationName?: string): Promise<GQLRespone>
  21. }
  22. export interface GQLRespone {[x:string]: any}
  23. export interface GQLError {
  24. message: string
  25. path: string[]
  26. }
  27. export function gql(a: TemplateStringsArray): string {
  28. return compress(a.join("")) as string;
  29. }
  30. const gqlBrowserClient = new GQLBrowserClient();
  31. export default gqlBrowserClient;