-
Notifications
You must be signed in to change notification settings - Fork 24
/
{kontentItemArticle.elements__slug__value}.js
58 lines (52 loc) · 1.49 KB
/
{kontentItemArticle.elements__slug__value}.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import React from "react"
import Layout from "../../components/layout"
import { plugins } from "../../../gatsby-config"
export default function Component({ serverData }) {
return (
<Layout>
<h1>{serverData.title}</h1>
<div>Published: {serverData.date}</div>
<p>{serverData.content}</p>
</Layout>
)
}
const kontentSourcePluginConfig = plugins.find(
plugin => plugin.resolve === "@kontent-ai/gatsby-source"
)
const projectId = kontentSourcePluginConfig.options.projectId
const language = kontentSourcePluginConfig.options.languageCodenames[0]
export async function getServerData(context) {
try {
const response = await fetch(`https://graphql.kontent.ai/${projectId}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query: `
query ssrArticleQuery($slug: String!, $language: String!) {
article_All(where: {slug: {eq: $slug}}, limit: 1, languageFilter: {languageCodename: $language}) {
items {
title
date
content
}
}
}`,
variables: {
slug: context.params.elements__slug__value,
language: language,
},
}),
}).then(res => res.json())
return {
props: response.data.article_All.items[0],
}
} catch (error) {
return {
status: 500,
headers: {},
props: {},
}
}
}