Merged in feat/SW-1442-destination-overview-page (pull request #1188)
feat(SW-1442): added destination overview page * feat(SW-1442): added destination overview page Approved-by: Fredrik Thorsson Approved-by: Matilda Landström
This commit is contained in:
41
lib/graphql/batchEdgeRequest.ts
Normal file
41
lib/graphql/batchEdgeRequest.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user