This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
gatsby-node.js
84 lines (79 loc) · 1.87 KB
/
gatsby-node.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const path = require(`path`);
const {
formatRelativeTime,
} = require("./src/utils/format-relative-time.cjs");
exports.createPages = async ({ actions, graphql }) => {
const { data } = await graphql(
`
query {
directus {
articles(filter: { status: { _eq: "published" } }, limit: -1) {
id
title
excerpt
body
publish_date
author {
first_name
last_name
avatar {
id
}
}
cover_image {
id
}
}
}
}
`
);
for (const article of data.directus.articles) {
const formattedArticle = {
...article,
publish_date: formatRelativeTime(new Date(article.publish_date)),
};
const moreArticlesResponse = await graphql(
`
query {
directus {
articles(filter: { _and: [{ id: { _neq: ${article.id} } }, { status: { _eq: "published" } }] }, limit: 2) {
id
title
publish_date
author {
first_name
last_name
avatar {
id
}
}
cover_image {
id
}
}
}
}
`
);
const formattedMoreArticles =
moreArticlesResponse.data.directus.articles.map(
(formattedMoreArticle) => {
return {
...formattedMoreArticle,
publish_date: formatRelativeTime(
new Date(formattedMoreArticle.publish_date)
),
};
}
);
actions.createPage({
path: `/articles/${article.id}`,
component: path.resolve(`src/templates/article.jsx`),
context: {
article: formattedArticle,
moreArticles: formattedMoreArticles,
},
});
}
};