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")
|
||||
}
|
||||
}
|
||||
39
lib/graphql/previewRequest.ts
Normal file
39
lib/graphql/previewRequest.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import "server-only"
|
||||
import { request as graphqlRequest } from "graphql-request"
|
||||
|
||||
import { env } from "@/env/server"
|
||||
import ContentstackLivePreview from "@contentstack/live-preview-utils"
|
||||
|
||||
import type { Data } from "@/types/request"
|
||||
import type { DocumentNode } from "graphql"
|
||||
|
||||
export async function previewRequest<T>(
|
||||
query: string | DocumentNode,
|
||||
variables?: {}
|
||||
): Promise<Data<T>> {
|
||||
try {
|
||||
const hash = ContentstackLivePreview.hash
|
||||
|
||||
if (!hash) {
|
||||
throw new Error("No hash received")
|
||||
}
|
||||
|
||||
const headers = new Headers({
|
||||
access_token: env.CMS_ACCESS_TOKEN,
|
||||
preview_token: env.CMS_PREVIEW_TOKEN,
|
||||
live_preview: hash,
|
||||
})
|
||||
|
||||
const response = await graphqlRequest<T>({
|
||||
document: query,
|
||||
requestHeaders: headers,
|
||||
url: env.CMS_PREVIEW_URL,
|
||||
variables,
|
||||
})
|
||||
|
||||
return { data: response }
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
throw new Error("Something went wrong")
|
||||
}
|
||||
}
|
||||
69
lib/graphql/request.ts
Normal file
69
lib/graphql/request.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import "server-only"
|
||||
import { GraphQLClient } from "graphql-request"
|
||||
import { env } from "@/env/server"
|
||||
import { cache } from "react"
|
||||
|
||||
import type { Data } from "@/types/request"
|
||||
import type { DocumentNode } from "graphql"
|
||||
|
||||
const client = new GraphQLClient(env.CMS_URL, {
|
||||
fetch: cache(async function (
|
||||
url: URL | RequestInfo,
|
||||
params: RequestInit | undefined
|
||||
) {
|
||||
return fetch(url, params)
|
||||
}),
|
||||
})
|
||||
|
||||
export async function request<T>(
|
||||
query: string | DocumentNode,
|
||||
variables?: {},
|
||||
next?: NextFetchRequestConfig
|
||||
): Promise<Data<T>> {
|
||||
try {
|
||||
if (next) {
|
||||
client.requestConfig.next = next
|
||||
}
|
||||
|
||||
if (env.PRINT_QUERY) {
|
||||
const print = (await import("graphql/language/printer")).print
|
||||
const rawResponse = await client.rawRequest<T>(
|
||||
print(query as DocumentNode),
|
||||
variables,
|
||||
{
|
||||
access_token: env.CMS_ACCESS_TOKEN,
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
)
|
||||
|
||||
/**
|
||||
* TODO: Send to Monitoring (Logging and Metrics)
|
||||
*/
|
||||
console.log({
|
||||
complexityLimit: rawResponse.headers.get("x-query-complexity"),
|
||||
})
|
||||
console.log({
|
||||
referenceDepth: rawResponse.headers.get("x-reference-depth"),
|
||||
})
|
||||
console.log({ resolverCost: rawResponse.headers.get("x-resolver-cost") })
|
||||
|
||||
return {
|
||||
data: rawResponse.data,
|
||||
}
|
||||
}
|
||||
|
||||
const response = await client.request<T>({
|
||||
document: query,
|
||||
requestHeaders: {
|
||||
access_token: env.CMS_ACCESS_TOKEN,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
variables,
|
||||
})
|
||||
|
||||
return { data: response }
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
throw new Error("Something went wrong")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user