feat: init live preview sdk
This commit is contained in:
30
lib/graphql/Fragments/Blocks/Hero.graphql
Normal file
30
lib/graphql/Fragments/Blocks/Hero.graphql
Normal file
@@ -0,0 +1,30 @@
|
||||
#import "../PageLinks.graphql"
|
||||
|
||||
fragment HeroBlock on CurrentBlocksPageBlocksHero {
|
||||
hero {
|
||||
imagesConnection {
|
||||
totalCount
|
||||
edges {
|
||||
node {
|
||||
title
|
||||
url
|
||||
}
|
||||
}
|
||||
}
|
||||
ingress {
|
||||
json
|
||||
embedded_itemsConnection {
|
||||
edges {
|
||||
node {
|
||||
...CurrentBlocksPageLink
|
||||
...TempPageLink
|
||||
... on SysAsset {
|
||||
title
|
||||
url
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
34
lib/previewRequest.ts
Normal file
34
lib/previewRequest.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
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";
|
||||
import ContentstackLivePreview from "@contentstack/live-preview-utils";
|
||||
|
||||
export async function request<T>(
|
||||
query: string | DocumentNode,
|
||||
variables?: {}
|
||||
): Promise<Data<T>> {
|
||||
try {
|
||||
const hash = ContentstackLivePreview.hash;
|
||||
|
||||
const response = await graphqlRequest<T>({
|
||||
document: query,
|
||||
requestHeaders: {
|
||||
access_token: env.CMS_ACCESS_TOKEN,
|
||||
"Content-Type": "application/json",
|
||||
preview_token: env.CMS_PREVIEW_TOKEN,
|
||||
live_preview: hash,
|
||||
},
|
||||
url: env.CMS_PREVIEW_URL,
|
||||
variables,
|
||||
});
|
||||
|
||||
return { data: response };
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw new Error("Something went wrong");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user