feat(WEB-103): create request client to be able to use next caching
This commit is contained in:
@@ -24,6 +24,9 @@ export default async function CurrentContentPage({
|
|||||||
{
|
{
|
||||||
locale: params.lang,
|
locale: params.lang,
|
||||||
url: searchParams.uri,
|
url: searchParams.uri,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tags: [`${searchParams.uri}-${params.lang}`]
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -9,9 +9,15 @@ import type { GetFooterData } from "@/types/requests/footer"
|
|||||||
import type { LangParams } from "@/types/params"
|
import type { LangParams } from "@/types/params"
|
||||||
|
|
||||||
export default async function Footer({ lang }: LangParams) {
|
export default async function Footer({ lang }: LangParams) {
|
||||||
const response = await request<GetFooterData>(GetFooter, {
|
const response = await request<GetFooterData>(
|
||||||
locale: lang,
|
GetFooter,
|
||||||
})
|
{
|
||||||
|
locale: lang,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tags: [`footer-${lang}`]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const footerData = response.data.all_footer.items[0]
|
const footerData = response.data.all_footer.items[0]
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -15,8 +15,6 @@ import type { HeaderQueryData } from "@/types/requests/header"
|
|||||||
import type { HeaderProps } from "@/types/components/current/header"
|
import type { HeaderProps } from "@/types/components/current/header"
|
||||||
import type { LanguageSwitcherQueryData } from "@/types/requests/languageSwitcher"
|
import type { LanguageSwitcherQueryData } from "@/types/requests/languageSwitcher"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default async function Header({ lang, uid }: LangParams & HeaderProps) {
|
export default async function Header({ lang, uid }: LangParams & HeaderProps) {
|
||||||
try {
|
try {
|
||||||
const variables = {
|
const variables = {
|
||||||
@@ -24,14 +22,16 @@ export default async function Header({ lang, uid }: LangParams & HeaderProps) {
|
|||||||
uid,
|
uid,
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data } = await request<HeaderQueryData>(GetHeader, { locale: lang })
|
const { data } = await request<HeaderQueryData>(GetHeader, { locale: lang }, { tags: [`header-${lang}`] })
|
||||||
const { data: urls } = await batchRequest<LanguageSwitcherQueryData>([
|
const { data: urls } = await batchRequest<LanguageSwitcherQueryData>([
|
||||||
{
|
{
|
||||||
document: GetDaDeEnUrls,
|
document: GetDaDeEnUrls,
|
||||||
|
tags: [`DA-DE-EN-${uid}`],
|
||||||
variables,
|
variables,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
document: GetFiNoSvUrls,
|
document: GetFiNoSvUrls,
|
||||||
|
tags: [`FI-NO-SV-${uid}`],
|
||||||
variables,
|
variables,
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ import { request } from "./request"
|
|||||||
import type { Data } from "@/types/request"
|
import type { Data } from "@/types/request"
|
||||||
import type { BatchRequestDocument } from "graphql-request"
|
import type { BatchRequestDocument } from "graphql-request"
|
||||||
|
|
||||||
export async function batchRequest<T>(queries: BatchRequestDocument[]): Promise<Data<T>> {
|
export async function batchRequest<T>(queries: (BatchRequestDocument & NextFetchRequestConfig)[]): Promise<Data<T>> {
|
||||||
try {
|
try {
|
||||||
const response = await Promise.allSettled(
|
const response = await Promise.allSettled(
|
||||||
queries.map(query => request<T>(query.document, query.variables))
|
queries.map(query => request<T>(query.document, query.variables, { tags: query.tags }))
|
||||||
)
|
)
|
||||||
|
|
||||||
let data = {} as T
|
let data = {} as T
|
||||||
|
|||||||
@@ -1,21 +1,27 @@
|
|||||||
import "server-only"
|
import "server-only"
|
||||||
import { request as graphqlRequest } from "graphql-request"
|
import { GraphQLClient } from "graphql-request"
|
||||||
import { env } from "@/env/server"
|
import { env } from "@/env/server"
|
||||||
|
import { cache } from "react"
|
||||||
|
|
||||||
import type { Data } from "@/types/request"
|
import type { Data } from "@/types/request"
|
||||||
import type { DocumentNode } from "graphql"
|
import type { DocumentNode } from "graphql"
|
||||||
|
|
||||||
export async function request<T>(
|
export async function request<T>(
|
||||||
query: string | DocumentNode,
|
query: string | DocumentNode,
|
||||||
variables?: {}
|
variables?: {},
|
||||||
|
next?: NextFetchRequestConfig
|
||||||
): Promise<Data<T>> {
|
): Promise<Data<T>> {
|
||||||
try {
|
try {
|
||||||
if (env.PRINT_QUERY) {
|
const client = new GraphQLClient(env.CMS_URL, {
|
||||||
const graphqlRawRequest = (await import("graphql-request")).rawRequest
|
fetch: cache(async function (url: URL | RequestInfo, params: RequestInit | undefined) {
|
||||||
const print = (await import("graphql/language/printer")).print
|
return fetch(url, params)
|
||||||
|
}),
|
||||||
|
next,
|
||||||
|
})
|
||||||
|
|
||||||
const rawResponse = await graphqlRawRequest<T>(
|
if (env.PRINT_QUERY) {
|
||||||
env.CMS_URL,
|
const print = (await import("graphql/language/printer")).print
|
||||||
|
const rawResponse = await client.rawRequest<T>(
|
||||||
print(query as DocumentNode),
|
print(query as DocumentNode),
|
||||||
variables,
|
variables,
|
||||||
{
|
{
|
||||||
@@ -40,13 +46,12 @@ export async function request<T>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await graphqlRequest<T>({
|
const response = await client.request<T>({
|
||||||
document: query,
|
document: query,
|
||||||
requestHeaders: {
|
requestHeaders: {
|
||||||
access_token: env.CMS_ACCESS_TOKEN,
|
access_token: env.CMS_ACCESS_TOKEN,
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
url: env.CMS_URL,
|
|
||||||
variables,
|
variables,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user