-
Notifications
You must be signed in to change notification settings - Fork 37
/
gatsby-node.js
130 lines (119 loc) · 3.45 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
const { githubParser } = require('./src/lib/github');
const path = require('path');
const NodeGeocoder = require('node-geocoder');
require('dotenv').config();
const options = {
provider: 'opencage',
apiKey: process.env.OPEN_CAGE_API_KEY, // for Mapquest, OpenCage, Google Premier
};
const geocoder = NodeGeocoder(options);
// Using callback
const githubQuery = `
allGithubData {
nodes {
data {
organization {
teams {
edges {
node {
members {
nodes {
avatarUrl
bio
company
email
followers {
totalCount
}
following {
totalCount
}
login
name
twitterUsername
url
websiteUrl
location
}
}
name
description
avatarUrl
}
}
}
}
}
}
}`;
let addedNode = false;
export const onCreateNode = async ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === 'GithubData' && !addedNode) {
let memberLocationMap = {};
node.data.organization.teams.edges.forEach(({ node: team }) => {
team.members.nodes.forEach((member) => {
memberLocationMap[member.login] = member.location;
});
});
const entries = Object.entries(memberLocationMap);
const geocodeResult = await geocoder.batchGeocode(
entries.map((entry) => entry[1]),
);
const ret = entries.map((entry, i) => {
const value = geocodeResult[i].value ? geocodeResult[i].value[0] : {};
return {
name: entry[0],
long: value && value.longitude,
lat: value && value.latitude,
};
});
console.log(ret);
createNodeField({
node,
name: 'memberLocationMap',
value: ret,
});
addedNode = true;
}
};
export const createPages = async ({ graphql, actions, reporter }) => {
// destructure the createPage function from the actions object
const { createPage } = actions;
const result = await graphql(`
query PageCreation {
allMdx {
nodes {
id
frontmatter {
github
}
}
}
${githubQuery}
}
`);
if (result.errors) {
reporter.panicOnBuild('🚨 ERROR: Loading "createPages" query');
}
// create profile pages
// setfields on graphql node type
//createResolvers
const profiles = result.data.allMdx.nodes;
const githubProfiles = githubParser(result.data.allGithubData.nodes[0].data);
githubProfiles.forEach((profile) => {
const mdxNode = profiles.find(
(node) =>
node.frontmatter.github.toLowerCase() ===
profile.username.toLowerCase(),
);
createPage({
// slug that is created before
path: profile.username,
// this component will wrap the MDX content
component: path.resolve(`./src/components/ConditionalPortfolio.tsx`),
// values here are made available to graphql
context: { id: mdxNode ? mdxNode.id : null, githubProfile: profile },
});
});
};