Skip to content

Commit c7ce2ce

Browse files
Alban BaillyGitHub Enterprise
authored andcommitted
Merge pull request linode#1 from jkobos/openapi-graphql
Openapi graphql
2 parents 568a5be + 018bd67 commit c7ce2ce

File tree

16 files changed

+96404
-48
lines changed

16 files changed

+96404
-48
lines changed

gatsby-config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
const fromJson = filePath => {
5+
return new Promise((resolve, reject) => {
6+
fs.readFile(filePath, "utf8", (err, data) => {
7+
if (err) {
8+
reject(err);
9+
return;
10+
}
11+
12+
resolve(data);
13+
});
14+
});
15+
};
16+
117
module.exports = {
218
siteMetadata: {
319
title: `Linode Developer Tools`,

gatsby-node.js

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,72 @@
1+
const JsonSchemaRefParser = require("json-schema-ref-parser");
12
const path = require("path");
23
const _ = require("lodash");
4+
// const axios = require("axios");
35

4-
exports.createPages = ({ actions, graphql }) => {
6+
const specs = require("./src/data/spec.json");
7+
const crypto = require("crypto");
8+
const parser = new JsonSchemaRefParser();
9+
10+
exports.sourceNodes = async ({ actions }) => {
11+
const { createNode } = actions;
12+
const res = await parser.dereference(specs);
13+
14+
// map into these results and create nodes
15+
Object.keys(res.paths).map((path, i) => {
16+
// Create your node object
17+
const pathNode = {
18+
// Required fields
19+
id: `${i}`,
20+
parent: `__SOURCE__`,
21+
internal: {
22+
type: `Paths` // name of the graphQL query --> allRandomUser {}
23+
// contentDigest will be added just after
24+
// but it is required
25+
},
26+
children: [],
27+
28+
// Other fields that you want to query with graphQl
29+
name: path,
30+
get: res.paths[path].get,
31+
post: res.paths[path].post,
32+
put: res.paths[path].put,
33+
responses: res.paths[path].responses
34+
// name: {
35+
// title: user.name.title,
36+
// first: user.name.first,
37+
// last: user.name.last
38+
// },
39+
// picture: {
40+
// large: user.picture.large,
41+
// medium: user.picture.medium,
42+
// thumbnail: user.picture.thumbnail
43+
// }
44+
// etc...
45+
};
46+
47+
// Get content digest of node. (Required field)
48+
const contentDigest = crypto
49+
.createHash(`md5`)
50+
.update(JSON.stringify(pathNode))
51+
.digest(`hex`);
52+
// add it to userNode
53+
pathNode.internal.contentDigest = contentDigest;
54+
55+
// Create node with the gatsby createNode() API
56+
createNode(pathNode);
57+
});
58+
59+
return;
60+
};
61+
62+
exports.createPages = async ({ actions, graphql }) => {
563
const { createPage } = actions;
664
const changelogTemplate = path.resolve(
765
"src/components/5_templates/changelogs.js"
866
);
67+
const apiTemplate = path.resolve("src/components/5_templates/api.js");
968

10-
return graphql(`
69+
const changelogs = await graphql(`
1170
{
1271
allMarkdownRemark(
1372
sort: { order: DESC, fields: [frontmatter___date] }
@@ -39,7 +98,6 @@ exports.createPages = ({ actions, graphql }) => {
3998

4099
// Make tag pages
41100
tags.forEach(changelog => {
42-
console.log(changelog);
43101
createPage({
44102
path: `/changelog/${_.kebabCase(changelog)}/`,
45103
component: changelogTemplate,
@@ -49,4 +107,40 @@ exports.createPages = ({ actions, graphql }) => {
49107
});
50108
});
51109
});
110+
111+
const specsPages = await graphql(`
112+
{
113+
allPaths {
114+
edges {
115+
node {
116+
id
117+
name
118+
}
119+
}
120+
}
121+
}
122+
`).then(result => {
123+
if (result.errors) {
124+
return Promise.reject(result.errors);
125+
}
126+
const res = result.data.allPaths.edges;
127+
let paths = [];
128+
_.each(res, edge => {
129+
if (_.get(edge, "node.name")) {
130+
paths = paths.concat(edge.node.name);
131+
}
132+
});
133+
paths = _.uniq(paths);
134+
paths.forEach(name => {
135+
createPage({
136+
path: `api/v4/${_.kebabCase(name)}`,
137+
component: apiTemplate,
138+
context: {
139+
name
140+
}
141+
});
142+
});
143+
});
144+
145+
return Promise.all([changelogs, specsPages]);
52146
};

0 commit comments

Comments
 (0)