feat: graphql client with fetches for initial pages

This commit is contained in:
Simon Emanuelsson
2024-02-01 16:19:16 +01:00
parent a91781137a
commit 5e974aa3da
56 changed files with 2580 additions and 1512 deletions

View File

@@ -0,0 +1,14 @@
#import "../Contact.graphql"
fragment ContactAside on CurrentBlocksPageAsideContact {
contact {
contactConnection {
edges {
node {
...Contact
}
}
totalCount
}
}
}

View File

@@ -0,0 +1,14 @@
#import "../Puff.graphql"
fragment PuffAside on CurrentBlocksPageAsidePuff {
puff {
puffConnection {
totalCount
edges {
node {
...Puff
}
}
}
}
}

View File

@@ -0,0 +1,49 @@
#import "../PageLinks.graphql"
fragment ListItem on CurrentBlocksPageBlocksListBlockListItemsListItem {
list_item {
list_item_style
subtitle
title
}
}
fragment ListItemExternalLink on CurrentBlocksPageBlocksListBlockListItemsListItemExternalLink {
list_item_external_link {
link {
href
title
}
list_item_style
subtitle
}
}
fragment ListItemInternalLink on CurrentBlocksPageBlocksListBlockListItemsListItemInternalLink {
list_item_internal_link {
link_text
list_item_style
subtitle
pageConnection {
totalCount
edges {
node {
...CurrentBlocksPageLink
...TempPageLink
}
}
}
}
}
fragment ListBlock on CurrentBlocksPageBlocksList {
list {
list_items {
__typename
...ListItem
...ListItemExternalLink
...ListItemInternalLink
}
title
}
}

View File

@@ -0,0 +1,21 @@
#import "../PageLinks.graphql"
fragment PreambleBlock on CurrentBlocksPageBlocksPreamble {
preamble {
text {
json
embedded_itemsConnection(limit: 30) {
edges {
node {
...CurrentBlocksPageLink
...TempPageLink
... on SysAsset {
title
url
}
}
}
}
}
}
}

View File

@@ -0,0 +1,15 @@
#import "../Puff.graphql"
fragment PuffBlock on CurrentBlocksPageBlocksPuffs {
puffs {
# We have to manually add a limit since Contentstack handles its complexity calculation in a certain way
puffsConnection(limit: 9) {
totalCount
edges {
node {
...Puff
}
}
}
}
}

View File

@@ -0,0 +1,22 @@
#import "../PageLinks.graphql"
fragment TextBlock on CurrentBlocksPageBlocksText {
text {
content {
embedded_itemsConnection {
totalCount
edges {
node {
...CurrentBlocksPageLink
...TempPageLink
... on SysAsset {
title
url
}
}
}
}
json
}
}
}

View File

@@ -0,0 +1,17 @@
fragment Contact on ContactBlock {
mailing_address {
city
country
name
street
zip
}
phone
title
visiting_address {
city
country
street
zip
}
}

View File

@@ -0,0 +1,17 @@
#import "./PageLinks.graphql"
fragment Hero on Hero {
imagesConnection {
totalCount
edges {
node {
dimension {
height
width
}
title
url
}
}
}
}

View File

@@ -0,0 +1,9 @@
fragment CurrentBlocksPageLink on CurrentBlocksPage {
title
url
}
fragment TempPageLink on TempPage {
title
url
}

View File

@@ -0,0 +1,43 @@
#import "./PageLinks.graphql"
fragment Puff on Puff {
imageConnection {
edges {
node {
title
url
}
}
}
is_internal
link {
href
title
}
link_text
pageConnection {
edges {
node {
__typename
...CurrentBlocksPageLink
...TempPageLink
}
}
}
text {
json
embedded_itemsConnection {
totalCount
edges {
node {
__typename
... on SysAsset {
title
url
}
}
}
}
}
title
}

View File

@@ -0,0 +1,32 @@
#import "../Fragments/Aside/Contact.graphql"
#import "../Fragments/Aside/Puff.graphql"
#import "../Fragments/Blocks/List.graphql"
#import "../Fragments/Blocks/Preamble.graphql"
#import "../Fragments/Blocks/Puff.graphql"
#import "../Fragments/Blocks/Text.graphql"
#import "../Fragments/Hero.graphql"
query GetCurrentBlockPage($locale: String!, $url: String!) {
all_current_blocks_page(limit: 1, locale: $locale, where: { url: $url }) {
items {
aside {
__typename
...ContactAside
...PuffAside
}
blocks {
__typename
...ListBlock
...PreambleBlock
...PuffBlock
...TextBlock
}
hero {
...Hero
}
title
url
}
total
}
}

52
lib/request.ts Normal file
View File

@@ -0,0 +1,52 @@
import "server-only"
import { request as graphqlRequest } from "graphql-request"
import { env } from "@/env/server.mjs"
import type { Data } from "@/types/request"
import type { DocumentNode } from "graphql";
export async function request<T>(query: string | DocumentNode, variables?: {}): Promise<Data<T>> {
try {
if (env.PRINT_QUERY) {
const graphqlRawRequest = (await import("graphql-request")).rawRequest
const print = (await import("graphql/language/printer")).print
const rawResponse = await graphqlRawRequest<T>(
env.CMS_URL,
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 graphqlRequest<T>({
document: query,
requestHeaders: {
"access_token": env.CMS_ACCESS_TOKEN,
"Content-Type": "application/json",
},
url: env.CMS_URL,
variables,
})
return { data: response }
} catch (error) {
console.error(error)
throw new Error("Something went wrong")
}
}