feat(SW-1442): added destination overview page * feat(SW-1442): added destination overview page Approved-by: Fredrik Thorsson Approved-by: Matilda Landström
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import deepmerge from "deepmerge"
|
|
|
|
import { arrayMerge } from "@/utils/merge"
|
|
|
|
import { edgeRequest } from "./edgeRequest"
|
|
|
|
import type { BatchRequestDocument } from "graphql-request"
|
|
|
|
import type { Data } from "@/types/request"
|
|
|
|
export async function batchEdgeRequest<T>(
|
|
queries: BatchRequestDocument[]
|
|
): Promise<Data<T>> {
|
|
try {
|
|
const response = await Promise.allSettled(
|
|
queries.map((query) => edgeRequest<T>(query.document, query.variables))
|
|
)
|
|
|
|
let data = {} as T
|
|
const reasons: PromiseRejectedResult["reason"][] = []
|
|
response.forEach((res) => {
|
|
if (res.status === "fulfilled") {
|
|
data = deepmerge(data, res.value.data, { arrayMerge })
|
|
} else {
|
|
reasons.push(res.reason)
|
|
}
|
|
})
|
|
|
|
if (reasons.length) {
|
|
reasons.forEach((reason) => {
|
|
console.error(`Batch request failed`, reason)
|
|
})
|
|
}
|
|
|
|
return { data }
|
|
} catch (error) {
|
|
console.error("Error in batched graphql request")
|
|
console.error(error)
|
|
throw new Error("Something went wrong")
|
|
}
|
|
}
|