mirror of
https://github.com/element-hq/matrix-authentication-service.git
synced 2026-10-23 00:03:24 +00:00
Better graphql response typing
This commit is contained in:
1 parent
4564b6cca8
commit
fcd9a6601f
3 files changed
+12
-5
No files matched your search
Generated
+1
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
+10
-5
@@ -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<TResult, TVariables> = {
|
||||
query: TypedDocumentString<TResult, TVariables>;
|
||||
type RequestOptions<TData, TVariables> = {
|
||||
query: TypedDocumentString<TData, TVariables>;
|
||||
signal?: AbortSignal;
|
||||
// biome-ignore lint/suspicious/noExplicitAny: this is for inference
|
||||
} & (TVariables extends Record<any, never>
|
||||
? { variables?: TVariables }
|
||||
: { variables: TVariables });
|
||||
|
||||
export const graphqlRequest = async <TResult, TVariables>({
|
||||
export const graphqlRequest = async <TData, TVariables>({
|
||||
query,
|
||||
variables,
|
||||
signal,
|
||||
}: RequestOptions<TResult, TVariables>): Promise<TResult> => {
|
||||
}: RequestOptions<TData, TVariables>): Promise<TData> => {
|
||||
const response = await fetch(graphqlEndpoint, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -45,10 +46,14 @@ export const graphqlRequest = async <TResult, TVariables>({
|
||||
throw new Error(`GraphQL request failed: ${response.status}`);
|
||||
}
|
||||
|
||||
const json = await response.json();
|
||||
const json: ExecutionResult<TData> = 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;
|
||||
};
|
||||
Reference in new issue
Block a user