feat: add edge request function
This commit is contained in:
@@ -5,7 +5,6 @@ import { GetCurrentBlockPageTrackingData } from "@/lib/graphql/Query/CurrentBloc
|
||||
import { request } from "@/lib/graphql/request"
|
||||
|
||||
import ContentPage from "@/components/Current/ContentPage"
|
||||
import Header from "@/components/Current/Header"
|
||||
import Tracking from "@/components/Current/Tracking"
|
||||
|
||||
import type { LangParams, PageArgs, UriParams } from "@/types/params"
|
||||
@@ -27,9 +26,7 @@ export default async function CurrentContentPage({
|
||||
locale: params.lang,
|
||||
url: searchParams.uri,
|
||||
},
|
||||
{
|
||||
next: { tags: [`${searchParams.uri}-${params.lang}`] },
|
||||
}
|
||||
{ tags: [`${searchParams.uri}-${params.lang}`] }
|
||||
)
|
||||
|
||||
if (!response.data?.all_current_blocks_page?.total) {
|
||||
@@ -43,9 +40,7 @@ export default async function CurrentContentPage({
|
||||
const pageDataForTracking = await request<TrackingData>(
|
||||
GetCurrentBlockPageTrackingData,
|
||||
{ uid: response.data.all_current_blocks_page.items[0].system.uid },
|
||||
{
|
||||
next: { tags: [`${searchParams.uri}-en`] },
|
||||
}
|
||||
{ tags: [`${searchParams.uri}-en`] }
|
||||
)
|
||||
|
||||
const pageData = response.data.all_current_blocks_page.items[0]
|
||||
|
||||
@@ -275,6 +275,7 @@
|
||||
.logo {
|
||||
width: 102.17px;
|
||||
height: 100%;
|
||||
padding-bottom: 4px;
|
||||
}
|
||||
|
||||
.listWrapper {
|
||||
|
||||
74
lib/graphql/_request.ts
Normal file
74
lib/graphql/_request.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import "server-only"
|
||||
|
||||
import { GraphQLClient } from "graphql-request"
|
||||
|
||||
import { env } from "@/env/server"
|
||||
|
||||
import type { DocumentNode } from "graphql"
|
||||
|
||||
import type { Data } from "@/types/request"
|
||||
|
||||
export async function request<T>(
|
||||
client: GraphQLClient,
|
||||
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 print = (await import("graphql/language/printer")).print
|
||||
const nr = Math.random()
|
||||
console.log(`START REQUEST ${nr}`)
|
||||
console.time(`OUTGOING REQUEST ${nr}`)
|
||||
console.log(`Sending reqeust to ${env.CMS_URL}`)
|
||||
console.log(`Query:`, print(query as DocumentNode))
|
||||
console.log(`Variables:`, variables)
|
||||
|
||||
const response = await client.request<T>({
|
||||
document: query,
|
||||
requestHeaders: {
|
||||
access_token: env.CMS_ACCESS_TOKEN,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
variables,
|
||||
})
|
||||
|
||||
console.timeEnd(`OUTGOING REQUEST ${nr}`)
|
||||
console.log({ response })
|
||||
|
||||
return { data: response }
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
throw new Error("Something went wrong")
|
||||
}
|
||||
}
|
||||
@@ -12,9 +12,7 @@ export async function batchRequest<T>(
|
||||
try {
|
||||
const response = await Promise.allSettled(
|
||||
queries.map((query) =>
|
||||
request<T>(query.document, query.variables, {
|
||||
next: { tags: query.tags },
|
||||
})
|
||||
request<T>(query.document, query.variables, { tags: query.tags })
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
20
lib/graphql/edgeRequest.ts
Normal file
20
lib/graphql/edgeRequest.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { DocumentNode } from "graphql"
|
||||
import { GraphQLClient } from "graphql-request"
|
||||
|
||||
import { env } from "@/env/server"
|
||||
|
||||
import { request as _request } from "./_request"
|
||||
|
||||
import { Data } from "@/types/request"
|
||||
|
||||
const client = new GraphQLClient(env.CMS_URL, {
|
||||
fetch: fetch,
|
||||
})
|
||||
|
||||
export async function edgeRequest<T>(
|
||||
query: string | DocumentNode,
|
||||
variables?: {},
|
||||
next?: NextFetchRequestConfig
|
||||
): Promise<Data<T>> {
|
||||
return _request(client, query, variables, next)
|
||||
}
|
||||
@@ -1,16 +1,14 @@
|
||||
import "server-only"
|
||||
|
||||
import { DocumentNode } from "graphql"
|
||||
import { GraphQLClient } from "graphql-request"
|
||||
import { cache } from "react"
|
||||
|
||||
import { env } from "@/env/server"
|
||||
|
||||
import type { DocumentNode } from "graphql"
|
||||
import { request as _request } from "./_request"
|
||||
|
||||
import type { Data } from "@/types/request"
|
||||
import { Data } from "@/types/request"
|
||||
|
||||
const client = new GraphQLClient(env.CMS_URL, {
|
||||
cache: "force-cache",
|
||||
fetch: cache(async function (
|
||||
url: URL | RequestInfo,
|
||||
params: RequestInit | undefined
|
||||
@@ -22,50 +20,7 @@ const client = new GraphQLClient(env.CMS_URL, {
|
||||
export async function request<T>(
|
||||
query: string | DocumentNode,
|
||||
variables?: {},
|
||||
options?: Pick<RequestInit, "cache" | "next">
|
||||
next?: NextFetchRequestConfig
|
||||
): Promise<Data<T>> {
|
||||
if (options?.cache) {
|
||||
client.requestConfig.cache = options.cache
|
||||
}
|
||||
if (options?.next) {
|
||||
client.requestConfig.next = options.next
|
||||
}
|
||||
|
||||
if (env.PRINT_QUERY) {
|
||||
const { print } = await import("graphql")
|
||||
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 }
|
||||
return _request(client, query, variables, next)
|
||||
}
|
||||
|
||||
@@ -37,9 +37,7 @@ export const accountPageQueryRouter = router({
|
||||
uid,
|
||||
},
|
||||
{
|
||||
next: {
|
||||
tags: [generateRefsResponseTag(lang, uid)],
|
||||
},
|
||||
tags: [generateRefsResponseTag(lang, uid)],
|
||||
}
|
||||
)
|
||||
|
||||
@@ -71,7 +69,7 @@ export const accountPageQueryRouter = router({
|
||||
locale: lang,
|
||||
uid,
|
||||
},
|
||||
{ next: { tags } }
|
||||
{ tags }
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
|
||||
@@ -68,9 +68,7 @@ export type Variables = {
|
||||
|
||||
export async function getRefsResponse<T>(query: string, variables: Variables) {
|
||||
const refsResponse = await request<T>(query, variables, {
|
||||
next: {
|
||||
tags: [generateRefsResponseTag(variables.locale, variables.url, affix)],
|
||||
},
|
||||
tags: [generateRefsResponseTag(variables.locale, variables.url, affix)],
|
||||
})
|
||||
if (!refsResponse.data) {
|
||||
throw notFound(refsResponse)
|
||||
@@ -91,7 +89,7 @@ export async function getResponse<T>(
|
||||
variables: Variables,
|
||||
tags: string[]
|
||||
) {
|
||||
const response = await request<T>(query, variables, { next: { tags } })
|
||||
const response = await request<T>(query, variables, { tags })
|
||||
if (!response.data) {
|
||||
throw notFound(response)
|
||||
}
|
||||
|
||||
@@ -54,9 +54,7 @@ export const loyaltyPageQueryRouter = router({
|
||||
uid,
|
||||
},
|
||||
{
|
||||
next: {
|
||||
tags: [generateRefsResponseTag(lang, uid)],
|
||||
},
|
||||
tags: [generateRefsResponseTag(lang, uid)],
|
||||
}
|
||||
)
|
||||
|
||||
@@ -90,7 +88,7 @@ export const loyaltyPageQueryRouter = router({
|
||||
locale: lang,
|
||||
uid,
|
||||
},
|
||||
{ next: { tags } }
|
||||
{ tags }
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
|
||||
@@ -60,9 +60,7 @@ export const navigationQueryRouter = router({
|
||||
GetNavigationMyPagesRefs,
|
||||
{ locale: lang },
|
||||
{
|
||||
next: {
|
||||
tags: [generateRefsResponseTag(lang, "navigation_my_pages")],
|
||||
},
|
||||
tags: [generateRefsResponseTag(lang, "navigation_my_pages")],
|
||||
}
|
||||
)
|
||||
|
||||
@@ -90,7 +88,7 @@ export const navigationQueryRouter = router({
|
||||
const response = await request<GetNavigationMyPagesData>(
|
||||
GetNavigationMyPages,
|
||||
{ locale: lang },
|
||||
{ next: { tags } }
|
||||
{ tags }
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
|
||||
@@ -1,31 +1,23 @@
|
||||
import { DocumentNode, print } from "graphql"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
import { env } from "@/env/server"
|
||||
import { edgeRequest } from "@/lib/graphql/edgeRequest"
|
||||
import { ResolveEntryByUrl } from "@/lib/graphql/Query/ResolveEntry.graphql"
|
||||
import { internalServerError } from "@/server/errors/next"
|
||||
|
||||
import { validateEntryResolveSchema } from "@/types/requests/entry"
|
||||
|
||||
export async function resolve(url: string, lang = Lang.en) {
|
||||
const result = await fetch(env.CMS_URL, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
access_token: env.CMS_ACCESS_TOKEN,
|
||||
"Content-Type": "application/json",
|
||||
const response = await edgeRequest(
|
||||
ResolveEntryByUrl,
|
||||
{
|
||||
locale: lang,
|
||||
url,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
query: print(ResolveEntryByUrl as DocumentNode),
|
||||
variables: {
|
||||
locale: lang,
|
||||
url,
|
||||
},
|
||||
}),
|
||||
})
|
||||
{
|
||||
revalidate: 3600,
|
||||
}
|
||||
)
|
||||
|
||||
const { data } = await result.json()
|
||||
|
||||
const validatedData = validateEntryResolveSchema.safeParse(data)
|
||||
const validatedData = validateEntryResolveSchema.safeParse(response.data)
|
||||
|
||||
if (!validatedData.success) {
|
||||
throw internalServerError(validatedData.error)
|
||||
|
||||
Reference in New Issue
Block a user