diff --git a/frontend/package-lock.json b/frontend/package-lock.json index c6b9187c4..ef9b77e2f 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -60,6 +60,7 @@ "@vitest/coverage-v8": "^2.1.5", "autoprefixer": "^10.4.20", "browserslist-to-esbuild": "^2.1.1", + "graphql": "^16.9.0", "happy-dom": "^15.11.4", "i18next-parser": "^9.0.2", "msw": "^2.6.4", diff --git a/frontend/package.json b/frontend/package.json index bac173afb..5a2869602 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -69,6 +69,7 @@ "@vitest/coverage-v8": "^2.1.5", "autoprefixer": "^10.4.20", "browserslist-to-esbuild": "^2.1.1", + "graphql": "^16.9.0", "happy-dom": "^15.11.4", "i18next-parser": "^9.0.2", "msw": "^2.6.4", diff --git a/frontend/src/graphql.ts b/frontend/src/graphql.ts index f3e273006..318373494 100644 --- a/frontend/src/graphql.ts +++ b/frontend/src/graphql.ts @@ -4,6 +4,7 @@ // SPDX-License-Identifier: AGPL-3.0-only // Please see LICENSE in the repository root for full details. +import type { ExecutionResult } from "graphql"; import appConfig from "./config"; import type { TypedDocumentString } from "./gql/graphql"; @@ -16,19 +17,19 @@ if (import.meta.env.TEST && !window) { const graphqlEndpoint = new URL(appConfig.graphqlEndpoint, base).toString(); -type RequestOptions = { - query: TypedDocumentString; +type RequestOptions = { + query: TypedDocumentString; signal?: AbortSignal; // biome-ignore lint/suspicious/noExplicitAny: this is for inference } & (TVariables extends Record ? { variables?: TVariables } : { variables: TVariables }); -export const graphqlRequest = async ({ +export const graphqlRequest = async ({ query, variables, signal, -}: RequestOptions): Promise => { +}: RequestOptions): Promise => { const response = await fetch(graphqlEndpoint, { method: "POST", headers: { @@ -45,10 +46,14 @@ export const graphqlRequest = async ({ throw new Error(`GraphQL request failed: ${response.status}`); } - const json = await response.json(); + const json: ExecutionResult = await response.json(); if (json.errors) { throw new Error(JSON.stringify(json.errors)); } + if (!json.data) { + throw new Error("GraphQL request returned no data"); + } + return json.data; };