Files
web/lib/batchRequest.ts
2024-02-22 16:33:08 +01:00

29 lines
738 B
TypeScript

import "server-only"
import { request } from "./request"
import type { Data } from "@/types/request"
import type { BatchRequestDocument } from "graphql-request"
export async function batchRequest<T>(queries: BatchRequestDocument[]): Promise<Data<T>> {
try {
const response = await Promise.allSettled(
queries.map(query => request<T>(query.document, query.variables))
)
let data = {} as T
const reasons = []
response.forEach(res => {
if (res.status === "fulfilled") {
data = Object.assign({}, data, res.value.data)
} else {
reasons.push(res.reason)
}
})
return { data }
} catch (error) {
console.error(error)
throw new Error("Something went wrong")
}
}