Files
web/packages/trpc/lib/graphql/batchRequest.ts
Joakim Jäderberg f5dd6740d0 Merged in chore/upgrade-vitest4 (pull request #3253)
chore: upgrade to vitest@4

* chore: upgrade to vitest@4


Approved-by: Anton Gunnarsson
2025-12-01 12:41:12 +00:00

76 lines
1.9 KiB
TypeScript

import "server-only"
import deepmerge from "deepmerge"
import merge from "deepmerge"
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
import { request } from "./request"
import type { CacheTime } from "@scandic-hotels/common/dataCache"
import type { DocumentNode } from "graphql"
import type { BatchRequestDocument } from "graphql-request"
import type { Data } from "../types/requestData"
export async function batchRequest<T>(
queries: (BatchRequestDocument & {
cacheOptions?: {
key: string | string[]
ttl: CacheTime
}
})[]
): Promise<Data<T>> {
const batchLogger = createLogger("graphql-batch-request")
try {
const response = await Promise.allSettled(
queries.map((query) =>
request<T>(
query.document as string | DocumentNode,
query.variables,
query.cacheOptions
)
)
)
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) => {
batchLogger.error(`Batch request failed`, reason)
})
}
return { data }
} catch (error) {
batchLogger.error("Error in batched graphql request", error)
throw error
}
}
function arrayMerge(
target: any[],
source: any[],
options: merge.ArrayMergeOptions
) {
const destination = target.slice()
source.forEach((item, index) => {
if (typeof destination[index] === "undefined") {
destination[index] = options.cloneUnlessOtherwiseSpecified(item, options)
} else if (options?.isMergeableObject(item)) {
destination[index] = merge(target[index], item, options)
} else if (target.indexOf(item) === -1) {
destination.push(item)
}
})
return destination
}