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