Skip to content

Commit 5768397

Browse files
author
Phuc La
committed
Test api connect
1 parent 2e613e9 commit 5768397

File tree

4 files changed

+4180
-5
lines changed

4 files changed

+4180
-5
lines changed

libs/graphcms.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
async function fetchAPI(query, { variables, preview } = {}) {
2+
console.log("process.env.GRAPHCMS_PROJECT_API");
3+
console.log(query, variables);
4+
const res = await fetch(process.env.GRAPHCMS_PROJECT_API, {
5+
method: "POST",
6+
headers: {
7+
"Content-Type": "application/json",
8+
Authorization: `Bearer ${process.env.GRAPHCMS_DEV_AUTH_TOKEN}`,
9+
},
10+
body: JSON.stringify({
11+
query: query,
12+
}),
13+
});
14+
const json = await res.json();
15+
16+
if (json.errors) {
17+
console.log(process.env.NEXT_EXAMPLE_CMS_GCMS_PROJECT_ID);
18+
console.error(json.errors);
19+
throw new Error("Failed to fetch API");
20+
}
21+
22+
// return json.data;
23+
return "";
24+
}
25+
26+
export async function getPreviewPostBySlug(slug) {
27+
const data = await fetchAPI(
28+
`
29+
query PostBySlug($slug: String!, $stage: Stage!) {
30+
post(where: {slug: $slug}, stage: $stage) {
31+
slug
32+
}
33+
}`,
34+
{
35+
preview: true,
36+
variables: {
37+
stage: "DRAFT",
38+
slug,
39+
},
40+
}
41+
);
42+
return data.post;
43+
}
44+
45+
export async function getAllPostsWithSlug() {
46+
const data = await fetchAPI(`
47+
{
48+
posts {
49+
slug
50+
}
51+
}
52+
`);
53+
return data.posts;
54+
}
55+
56+
export async function getAllPostsForHome(preview) {
57+
const data = await fetchAPI(
58+
`
59+
query MyQuery {
60+
users_ID {
61+
userName
62+
id
63+
}
64+
}
65+
`
66+
);
67+
console.log("data", data);
68+
return data;
69+
}
70+
71+
export async function getPostAndMorePosts(slug, preview) {
72+
const data = await fetchAPI(
73+
`
74+
query PostBySlug($slug: String!, $stage: Stage!) {
75+
post(stage: $stage, where: {slug: $slug}) {
76+
title
77+
slug
78+
content {
79+
html
80+
}
81+
date
82+
ogImage: coverImage {
83+
url(transformation: {image: {resize: {fit: crop, width: 2000, height: 1000}}})
84+
}
85+
coverImage {
86+
url(transformation: {image: {resize: {fit: crop, width: 2000, height: 1000}}})
87+
}
88+
author {
89+
name
90+
picture {
91+
url(transformation: {image: {resize: {fit: crop, width: 100, height: 100}}})
92+
}
93+
}
94+
}
95+
morePosts: posts(orderBy: date_DESC, first: 2, where: {slug_not_in: [$slug]}) {
96+
title
97+
slug
98+
excerpt
99+
date
100+
coverImage {
101+
url(transformation: {image: {resize: {fit: crop, width: 2000, height: 1000}}})
102+
}
103+
author {
104+
name
105+
picture {
106+
url(transformation: {image: {resize: {fit: crop, width: 100, height: 100}}})
107+
}
108+
}
109+
}
110+
}
111+
`,
112+
{
113+
preview,
114+
variables: {
115+
stage: preview ? "DRAFT" : "PUBLISHED",
116+
slug,
117+
},
118+
}
119+
);
120+
return data;
121+
}

pages/api/preview.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { getPreviewPostBySlug } from "../../libs/graphcms";
2+
3+
export default async function handler(req, res) {
4+
// Check the secret and next parameters
5+
// This secret should only be known to this API route and the CMS
6+
if (
7+
req.query.secret !== process.env.GRAPHCMS_PREVIEW_SECRET ||
8+
!req.query.slug
9+
) {
10+
return res.status(401).json({ message: "Invalid token" });
11+
}
12+
13+
// Fetch the headless CMS to check if the provided `slug` exists
14+
const post = await getPreviewPostBySlug(req.query.slug);
15+
16+
// If the slug doesn't exist prevent preview mode from being enabled
17+
if (!post) {
18+
return res.status(401).json({ message: "Invalid slug" });
19+
}
20+
21+
// Enable Preview Mode by setting the cookies
22+
res.setPreviewData({});
23+
24+
// Redirect to the path from the fetched post
25+
// We don't redirect to req.query.slug as that might lead to open redirect vulnerabilities
26+
res.writeHead(307, { Location: `/posts/${post.slug}` });
27+
res.end();
28+
}

pages/index.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import Head from 'next/head'
2-
import styles from '../styles/Home.module.css'
1+
import Head from "next/head";
2+
import styles from "../styles/Home.module.css";
3+
import { getAllPostsForHome } from "../libs/graphcms";
34

45
export default function Home() {
56
return (
@@ -15,7 +16,7 @@ export default function Home() {
1516
</h1>
1617

1718
<p className={styles.description}>
18-
Get started by editing{' '}
19+
Get started by editing{" "}
1920
<code className={styles.code}>pages/index.js</code>
2021
</p>
2122

@@ -56,10 +57,18 @@ export default function Home() {
5657
target="_blank"
5758
rel="noopener noreferrer"
5859
>
59-
Powered by{' '}
60+
Powered by{" "}
6061
<img src="/vercel.svg" alt="Vercel Logo" className={styles.logo} />
6162
</a>
6263
</footer>
6364
</div>
64-
)
65+
);
66+
}
67+
68+
export async function getStaticProps({ preview = false }) {
69+
const posts = (await getAllPostsForHome(preview)) || [];
70+
console.log("fetch", posts, preview);
71+
return {
72+
props: { posts, preview },
73+
};
6574
}

0 commit comments

Comments
 (0)