Skip to content

Commit 40889ee

Browse files
committed
Better graphql response typing
1 parent 424d886 commit 40889ee

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

frontend/package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"@vitest/coverage-v8": "^2.1.4",
7070
"autoprefixer": "^10.4.20",
7171
"browserslist-to-esbuild": "^2.1.1",
72+
"graphql": "^16.9.0",
7273
"happy-dom": "^15.11.2",
7374
"i18next-parser": "^9.0.2",
7475
"msw": "^2.6.4",

frontend/src/graphql.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// SPDX-License-Identifier: AGPL-3.0-only
55
// Please see LICENSE in the repository root for full details.
66

7+
import type { ExecutionResult } from "graphql";
78
import appConfig from "./config";
89
import type { TypedDocumentString } from "./gql/graphql";
910

@@ -16,19 +17,19 @@ if (import.meta.env.TEST && !window) {
1617

1718
const graphqlEndpoint = new URL(appConfig.graphqlEndpoint, base).toString();
1819

19-
type RequestOptions<TResult, TVariables> = {
20-
query: TypedDocumentString<TResult, TVariables>;
20+
type RequestOptions<TData, TVariables> = {
21+
query: TypedDocumentString<TData, TVariables>;
2122
signal?: AbortSignal;
2223
// biome-ignore lint/suspicious/noExplicitAny: this is for inference
2324
} & (TVariables extends Record<any, never>
2425
? { variables?: TVariables }
2526
: { variables: TVariables });
2627

27-
export const graphqlRequest = async <TResult, TVariables>({
28+
export const graphqlRequest = async <TData, TVariables>({
2829
query,
2930
variables,
3031
signal,
31-
}: RequestOptions<TResult, TVariables>): Promise<TResult> => {
32+
}: RequestOptions<TData, TVariables>): Promise<TData> => {
3233
const response = await fetch(graphqlEndpoint, {
3334
method: "POST",
3435
headers: {
@@ -45,10 +46,14 @@ export const graphqlRequest = async <TResult, TVariables>({
4546
throw new Error(`GraphQL request failed: ${response.status}`);
4647
}
4748

48-
const json = await response.json();
49+
const json: ExecutionResult<TData> = await response.json();
4950
if (json.errors) {
5051
throw new Error(JSON.stringify(json.errors));
5152
}
5253

54+
if (!json.data) {
55+
throw new Error("GraphQL request returned no data");
56+
}
57+
5358
return json.data;
5459
};

0 commit comments

Comments
 (0)