-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
132 lines (120 loc) · 3.79 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
require('dotenv').config({
path: `.env.${process.env.NODE_ENV}`,
});
const path = require(`path`);
const { createRemoteFileNode } = require(`gatsby-source-filesystem`);
exports.onCreatePage = async ({ page, actions }) => {
const { createPage } = actions;
// Only update the `/harta` page.
if (page.path.match(/^\/harta/)) {
// page.matchPath is a special key that's used for matching pages
// with corresponding routes only on the client.
page.matchPath = '/harta/*';
// Update the page.
createPage(page);
}
};
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
const blogPostTemplate = path.resolve(`src/templates/blog-post.tsx`);
const pageTemplate = path.resolve(`src/templates/page.tsx`);
// Query for markdown nodes to use in creating pages.
// You can query for whatever data you want to create pages for e.g.
// products, portfolio items, landing pages, etc.
// Variables can be added as the second function parameter
return graphql(
`
query loadContentQuery($limit: Int!) {
hasura {
blogs(last: $limit) {
slug
title
}
pages(last: $limit) {
slug
}
}
}
`,
{ limit: 100 }
).then(result => {
if (result.errors) {
throw result.errors;
}
// Create blog post pages.
result.data.hasura.blogs.forEach((blog, index) => {
const previous = index === result.data.hasura.blogs.length - 1 ? null : result.data.hasura.blogs[index + 1];
const next = index === 0 ? null : result.data.hasura.blogs[index - 1];
createPage({
// Path for this page — required
path: `/noutati/${blog.slug}`,
component: blogPostTemplate,
context: {
slug: blog.slug,
previous,
next,
},
});
});
// Create pages.
result.data.hasura.pages.forEach(page => {
createPage({
// Path for this page — required
path: `${page.slug}`,
component: pageTemplate,
context: {
slug: page.slug,
},
});
});
});
};
// download images from hasura
exports.createResolvers = ({ actions, cache, createNodeId, createResolvers, store, reporter }) => {
const { createNode } = actions;
const imageUrlFieldName = 'url'; //graphCMS stores images there
const schemaName = process.env.GATSBY_HASURA_GRAPHQL_TYPE_NAME;
const state = store.getState();
const schema = state.schemaCustomization.thirdPartySchemas.filter(s => s._typeMap[schemaName])[0];
if (!schema) {
throw new Error(`SCHEMA '${schemaName} NOT FOUND'`);
} else {
console.log(`Found schema '${schemaName}', traversing for image fields with name '${imageUrlFieldName}'`);
}
const typeMap = schema._typeMap;
const resolvers = {};
for (const typeName in typeMap) {
const typeEntry = typeMap[typeName];
const typeFields = (typeEntry && typeEntry.getFields && typeEntry.getFields()) || {};
const typeResolver = {};
for (const fieldName in typeFields) {
const field = typeFields[fieldName];
if (fieldName === imageUrlFieldName && typeFields.mimeType) {
const x = Object.keys(typeFields);
typeResolver[`${fieldName}Sharp`] = {
type: 'File',
resolve(source) {
const url = source[imageUrlFieldName];
if (url) {
return createRemoteFileNode({
url,
store,
cache,
createNode,
createNodeId,
reporter,
});
}
return null;
},
};
}
}
if (Object.keys(typeResolver).length) {
resolvers[typeName] = typeResolver;
}
}
if (Object.keys(resolvers).length) {
createResolvers(resolvers);
}
};