-
Notifications
You must be signed in to change notification settings - Fork 5
/
gatsby-node.ts
498 lines (445 loc) · 15.1 KB
/
gatsby-node.ts
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* eslint-disable no-await-in-loop */
import pg from 'pg';
import { GatsbyNode } from 'gatsby';
import { createRemoteFileNode } from 'gatsby-source-filesystem';
import { SUPABASE_CONNECTION_STRING } from './config';
import { normalizeConnector } from './src/utils';
import { getAuthorPathBySlug, getComparisonSlug, Vendor } from './shared';
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.com/docs/reference/config-files/gatsby-node/
*/
// TODO: replace this to ES6 import when gatsby release conversion from commonJS to ES6
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');
// Define the template for blog and blog post
const blogPostTemplate = path.resolve('./src/templates/blog-post/index.tsx');
const blogTemplate = path.resolve('./src/templates/blog/index.tsx');
const caseStudyTemplate = path.resolve('./src/layouts/CaseStudy/index.tsx');
const connectorTemplate = path.resolve('./src/templates/connector/index.tsx');
const connectionTemplate = path.resolve('./src/templates/connection.tsx');
const authorTemplate = path.resolve('./src/templates/author/index.tsx');
const comparisonTemplate = path.resolve('./src/templates/etl-tools/index.tsx');
export const createPages: GatsbyNode['createPages'] = async ({
graphql,
actions,
reporter,
}) => {
const { createPage, createRedirect } = actions;
const validateDataExistence = (data: any[] | undefined, name: string) => {
if (!Array.isArray(data) || data.length === 0) {
throw new Error(
`${name} array is either not an array or is empty.`
);
}
};
createRedirect({
fromPath: '/blogs',
toPath: '/blog',
});
// Get all strapi blog posts sorted by date
const result = await graphql<{
allStrapiBlogPost: {
nodes: {
updatedAt: any;
Slug: string;
id: string;
tags: {
Name: string;
Slug: string;
Type: string;
IsTab: boolean;
}[];
}[];
};
}>(`
{
allStrapiBlogPost(
sort: { publishedAt: DESC }
filter: { publishedAt: { ne: null } }
) {
nodes {
updatedAt
Slug
id
tags {
Name
Slug
Type
IsTab
}
}
}
}
`);
const caseStudyPages = await graphql<{
allStrapiCaseStudy: {
nodes: {
Slug: string;
id: string;
}[];
};
}>(`
{
allStrapiCaseStudy {
nodes {
id
Slug
}
}
}
`);
const allCaseStudies = caseStudyPages.data?.allStrapiCaseStudy.nodes;
validateDataExistence(allCaseStudies, 'Case Studies');
allCaseStudies?.forEach((node) => {
createPage({
path: `customers/${node.Slug}`,
component: caseStudyTemplate,
context: {
id: node.id,
},
});
});
const comparisonVendors = await graphql<{
allStrapiComparison: {
nodes: Partial<Vendor>[];
};
}>(`
{
allStrapiComparison(sort: { slugKey: ASC }) {
nodes {
id
slugKey
}
}
}
`);
const vendors = comparisonVendors.data?.allStrapiComparison.nodes;
if (vendors) {
vendors.forEach((xVendor, i) => {
vendors.slice(i + 1).forEach((yVendor) => {
if (xVendor.slugKey && yVendor.slugKey) {
createPage({
path: `/${getComparisonSlug(
xVendor.slugKey,
yVendor.slugKey
)}`,
component: comparisonTemplate,
context: {
xVendorId: xVendor.id,
yVendorId: yVendor.id,
estuaryVendorId:
'd829928c-c473-5421-ac0a-f03c45b14993',
},
});
}
});
});
}
if (result.errors || comparisonVendors.errors || caseStudyPages.errors) {
reporter.panicOnBuild(
'There was an error loading your blog posts',
result.errors
);
return;
}
const allPosts = result.data?.allStrapiBlogPost.nodes ?? [];
validateDataExistence(allPosts, 'Posts');
validateDataExistence(vendors, 'Comparison Pages');
const categories: {
[key: string]: {
Type: string;
Slug: string;
Name: string;
IsTab: string;
};
} = Object.assign(
{},
...allPosts.flatMap((post) =>
post.tags
.filter((tag) => tag.Type === 'category')
.map((tag) => ({ [tag.Slug]: tag }))
)
);
const postsByCategory = [
...Object.keys(categories).map((category) =>
allPosts.filter((post) =>
post.tags.some((tag) => tag.Slug === category)
)
),
// Let's not forget posts that have no category!
allPosts.filter((post) =>
post.tags.every((tag) => tag.Type !== 'category')
),
];
const tabCategories = Object.values(categories)
.filter((cat) => cat.IsTab)
.sort((a, b) =>
a.Slug === 'featured' ? -999999999 : a.Name.localeCompare(b.Name)
);
const blogPageSlug = (pathStr, page) =>
page > 0 ? `${pathStr}/${page + 1}` : pathStr;
const createBlogPostPages = (
postIds: string[],
pathStr: string,
title: string,
slug: string,
pageSize = 30
) => {
const totalPages = Math.ceil(postIds.length / pageSize);
for (let page = 0; page < totalPages; page += 1) {
const pagePostIds = postIds.slice(
page * pageSize,
(page + 1) * pageSize
);
const calculatedPath = blogPageSlug(pathStr, page);
const prevPage = page > 0 ? blogPageSlug(pathStr, page - 1) : null;
const nextPage =
page + 1 < totalPages ? blogPageSlug(pathStr, page + 1) : null;
createPage({
path: calculatedPath,
component: blogTemplate,
context: {
blogPostIds: pagePostIds,
categoryTitle: title,
categorySlug: slug,
pagination: {
page,
totalPages,
nextPage,
prevPage,
},
tabCategories,
},
});
}
};
for (const category of tabCategories) {
createBlogPostPages(
allPosts
.filter((post) =>
post.tags.some((tag) => tag.Slug === category.Slug)
)
.map((post) => post.id),
`/blog/${category.Slug}`,
category.Name,
category.Slug
);
}
createBlogPostPages(
allPosts.map((post) => post.id),
'/blog',
'All',
''
);
// Create blog posts pages
// But only if there's at least one markdown file found at "content/blog" (defined in gatsby-config.js)
// `context` is available in the template as a prop and as a variable in GraphQL
for (const posts of postsByCategory) {
if (posts.length > 0) {
posts.forEach((post, index) => {
if (!post.Slug) {
throw new Error(
`Unable to figure out a slug for the post with id: ${post.id}`
);
} else {
const previousPostId =
index === 0 ? null : posts[index - 1].id;
const nextPostId =
index === posts.length - 1 ? null : posts[index + 1].id;
createPage({
path: post.Slug,
component: blogPostTemplate,
context: {
id: post.id,
previousPostId,
nextPostId,
lastMod: post.updatedAt,
},
});
}
});
}
}
const connectors = await graphql<{
postgres: {
allConnectors: {
nodes: any[];
};
};
}>(`
{
postgres {
allConnectors(orderBy: [RECOMMENDED_DESC, CREATED_AT_DESC]) {
nodes {
id
externalUrl
imageName
shortDescription
longDescription
title
logoUrl
recommended
connectorTagsByConnectorIdList {
protocol
}
}
}
}
}
`);
const mapped_connectors =
connectors.data?.postgres.allConnectors.nodes
.filter((conn) => conn?.connectorTagsByConnectorIdList?.length > 0)
.map(normalizeConnector) ?? [];
validateDataExistence(mapped_connectors, 'Connectors');
for (const normalized_connector of mapped_connectors) {
if (!normalized_connector.slug) {
throw new Error(
`Unable to figure out a slug for the connector with image: ${normalized_connector.imageName}`
);
} else {
createPage({
path: normalized_connector.slug,
component: connectorTemplate,
context: {
id: normalized_connector.id,
type: normalized_connector.type,
},
});
if (normalized_connector.type === 'capture') {
for (const destination_connector of mapped_connectors.filter(
(con) => con.type === 'materialization'
)) {
createPage({
path: `/integrations/${normalized_connector.slugified_name}-to-${destination_connector.slugified_name}`,
component: connectionTemplate,
context: {
source_id: normalized_connector.id,
destination_id: destination_connector.id,
},
});
}
}
}
}
const authors = await graphql<{
allStrapiAuthor: {
nodes: any[];
};
}>(`
{
allStrapiAuthor {
nodes {
id
slug: Slug
}
}
}
`);
if (authors.data?.allStrapiAuthor.nodes) {
for (const author of authors.data.allStrapiAuthor.nodes) {
createPage({
path: getAuthorPathBySlug(author.slug),
component: authorTemplate,
context: {
id: author.id,
},
});
}
}
};
// Hacky hack :(
// tl;dr `gatsby-source-pg` doesn't go through the normal gatsby `createNode` workflow.
// As a result, we can't look up the nodes for connectors in order to download their logo.
// You used to be able to call `createRemoteFileNode` inside `createResolvers`, but
// that breaks Gatsby invariants, see:
// https://www.gatsbyjs.com/docs/reference/release-notes/migrating-from-v3-to-v4/#do-not-create-nodes-in-custom-resolvers
// So instead what we have to do is just know that we're going to want connector logos
// and query them all through a "side-channel" here, so that we can actually pass them through Gatsby's Sharp
// transformer. Then we can just attach a much simpler resolver to `PostGraphile_Connector` that just
// looks up that previously created and processed logo.
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type ConnectorLogo implements Node @dontInfer {
connectorId: String!
logoUrl: String!
logo: File!
}
`;
createTypes(typeDefs);
};
const createLogoNodeId = (connectorId: string) =>
`ConnectorLogo-${connectorId}`;
export const sourceNodes: GatsbyNode['sourceNodes'] = async ({
actions: { createNode },
createNodeId,
getCache,
createContentDigest,
}) => {
const pool = new pg.Pool({
connectionString: SUPABASE_CONNECTION_STRING,
connectionTimeoutMillis: 5 * 1000,
});
const connectors = await pool.query(
'select connectors.id as id, connectors.logo_url as logo_url from public.connectors;'
);
for (const conn of connectors.rows) {
const usUrl = conn.logo_url?.['en-US'];
if (usUrl) {
const fileNode = await createRemoteFileNode({
url: usUrl,
createNode,
createNodeId,
getCache,
});
await createNode({
connectorId: conn.id,
logoUrl: usUrl,
logo: fileNode,
id: createNodeId(createLogoNodeId(conn.id)),
internal: {
type: 'ConnectorLogo',
contentDigest: createContentDigest(fileNode),
},
});
}
}
await pool.end();
};
export const createResolvers: GatsbyNode['createResolvers'] = async ({
createResolvers: createResolversParam,
}) => {
createResolversParam({
PostGraphile_Connector: {
logo: {
type: 'File',
async resolve(node, _, ctx) {
const { id } = node;
const logoNode = await ctx.nodeModel.findOne({
type: 'ConnectorLogo',
query: { filter: { connectorId: { eq: id } } },
});
if (logoNode?.logo) {
return logoNode.logo;
}
return null;
},
},
},
});
};
export const onCreateWebpackConfig = ({ stage, actions, getConfig }) => {
if (['build-javascript', 'develop'].includes(stage)) {
const config = getConfig();
const miniCssExtractPlugin = config.plugins.find(
(plugin) => plugin.constructor.name === 'MiniCssExtractPlugin'
);
if (miniCssExtractPlugin) {
miniCssExtractPlugin.options.ignoreOrder = true;
}
actions.replaceWebpackConfig(config);
}
};