feat(WEB-127): add trpc to handle requests both serverside and clientside
This commit is contained in:
32
lib/graphql/batchRequest.ts
Normal file
32
lib/graphql/batchRequest.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
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 & NextFetchRequestConfig)[]
|
||||
): Promise<Data<T>> {
|
||||
try {
|
||||
const response = await Promise.allSettled(
|
||||
queries.map((query) =>
|
||||
request<T>(query.document, query.variables, { tags: query.tags })
|
||||
)
|
||||
)
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user