Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions examples/cms-graphcms/lib/graphcms.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
async function fetchAPI(query, { variables, preview } = {}) {
const res = await fetch(process.env.GRAPHCMS_PROJECT_API, {
// Validate critical environment variables
const apiUrl = process.env.GRAPHCMS_PROJECT_API;
if (!apiUrl) throw new Error("Missing GRAPHCMS_PROJECT_API environment variable");

const token = preview
? process.env.GRAPHCMS_DEV_AUTH_TOKEN
: process.env.GRAPHCMS_PROD_AUTH_TOKEN;

if (!token) throw new Error(`Missing ${preview ? "GRAPHCMS_DEV_AUTH_TOKEN" : "GRAPHCMS_PROD_AUTH_TOKEN"} environment variable`);

// Perform API request
const res = await fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${
preview
? process.env.GRAPHCMS_DEV_AUTH_TOKEN
: process.env.GRAPHCMS_PROD_AUTH_TOKEN
}`,
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
query,
variables,
}),
});

// Parse response
const json = await res.json();

if (json.errors) {
console.log(process.env.NEXT_EXAMPLE_CMS_GCMS_PROJECT_ID);
console.error(json.errors);
console.error("GraphCMS API errors:", json.errors);
throw new Error("Failed to fetch API");
}

return json.data;
}


export async function getPreviewPostBySlug(slug) {
const data = await fetchAPI(
`
Expand Down
Loading