feat: graphql client with fetches for initial pages
This commit is contained in:
14
lib/graphql/Fragments/Aside/Contact.graphql
Normal file
14
lib/graphql/Fragments/Aside/Contact.graphql
Normal file
@@ -0,0 +1,14 @@
|
||||
#import "../Contact.graphql"
|
||||
|
||||
fragment ContactAside on CurrentBlocksPageAsideContact {
|
||||
contact {
|
||||
contactConnection {
|
||||
edges {
|
||||
node {
|
||||
...Contact
|
||||
}
|
||||
}
|
||||
totalCount
|
||||
}
|
||||
}
|
||||
}
|
||||
14
lib/graphql/Fragments/Aside/Puff.graphql
Normal file
14
lib/graphql/Fragments/Aside/Puff.graphql
Normal file
@@ -0,0 +1,14 @@
|
||||
#import "../Puff.graphql"
|
||||
|
||||
fragment PuffAside on CurrentBlocksPageAsidePuff {
|
||||
puff {
|
||||
puffConnection {
|
||||
totalCount
|
||||
edges {
|
||||
node {
|
||||
...Puff
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
49
lib/graphql/Fragments/Blocks/List.graphql
Normal file
49
lib/graphql/Fragments/Blocks/List.graphql
Normal 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
|
||||
}
|
||||
}
|
||||
21
lib/graphql/Fragments/Blocks/Preamble.graphql
Normal file
21
lib/graphql/Fragments/Blocks/Preamble.graphql
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
15
lib/graphql/Fragments/Blocks/Puff.graphql
Normal file
15
lib/graphql/Fragments/Blocks/Puff.graphql
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
22
lib/graphql/Fragments/Blocks/Text.graphql
Normal file
22
lib/graphql/Fragments/Blocks/Text.graphql
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
17
lib/graphql/Fragments/Contact.graphql
Normal file
17
lib/graphql/Fragments/Contact.graphql
Normal file
@@ -0,0 +1,17 @@
|
||||
fragment Contact on ContactBlock {
|
||||
mailing_address {
|
||||
city
|
||||
country
|
||||
name
|
||||
street
|
||||
zip
|
||||
}
|
||||
phone
|
||||
title
|
||||
visiting_address {
|
||||
city
|
||||
country
|
||||
street
|
||||
zip
|
||||
}
|
||||
}
|
||||
17
lib/graphql/Fragments/Hero.graphql
Normal file
17
lib/graphql/Fragments/Hero.graphql
Normal file
@@ -0,0 +1,17 @@
|
||||
#import "./PageLinks.graphql"
|
||||
|
||||
fragment Hero on Hero {
|
||||
imagesConnection {
|
||||
totalCount
|
||||
edges {
|
||||
node {
|
||||
dimension {
|
||||
height
|
||||
width
|
||||
}
|
||||
title
|
||||
url
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
9
lib/graphql/Fragments/PageLinks.graphql
Normal file
9
lib/graphql/Fragments/PageLinks.graphql
Normal file
@@ -0,0 +1,9 @@
|
||||
fragment CurrentBlocksPageLink on CurrentBlocksPage {
|
||||
title
|
||||
url
|
||||
}
|
||||
|
||||
fragment TempPageLink on TempPage {
|
||||
title
|
||||
url
|
||||
}
|
||||
43
lib/graphql/Fragments/Puff.graphql
Normal file
43
lib/graphql/Fragments/Puff.graphql
Normal 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
|
||||
}
|
||||
32
lib/graphql/Query/CurrentBlockPage.graphql
Normal file
32
lib/graphql/Query/CurrentBlockPage.graphql
Normal 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
52
lib/request.ts
Normal 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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user