Skip to content

Commit 7e46cc4

Browse files
author
Alban Bailly
committed
re hitting graphql
1 parent 1ce93f5 commit 7e46cc4

File tree

7 files changed

+296
-53
lines changed

7 files changed

+296
-53
lines changed

gatsby-node.js

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,63 @@
1+
const JsonSchemaRefParser = require("json-schema-ref-parser");
12
const path = require("path");
23
const _ = require("lodash");
4+
// const axios = require("axios");
5+
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+
};
361

462
exports.createPages = async ({ actions, graphql }) => {
563
const { createPage } = actions;
@@ -50,14 +108,12 @@ exports.createPages = async ({ actions, graphql }) => {
50108
});
51109
});
52110

53-
const specs = await graphql(`
111+
const specsPages = await graphql(`
54112
{
55-
allMarkdownRemark {
113+
allPaths {
56114
edges {
57115
node {
58-
frontmatter {
59-
changelog
60-
}
116+
name
61117
}
62118
}
63119
}
@@ -66,9 +122,15 @@ exports.createPages = async ({ actions, graphql }) => {
66122
if (result.errors) {
67123
return Promise.reject(result.errors);
68124
}
69-
70-
const specsapi = require("./src/data/spec.json");
71-
Object.keys(specsapi.paths).forEach(name => {
125+
const res = result.data.allPaths.edges;
126+
let paths = [];
127+
_.each(res, edge => {
128+
if (_.get(edge, "node.name")) {
129+
paths = paths.concat(edge.node.name);
130+
}
131+
});
132+
paths = _.uniq(paths);
133+
paths.forEach(name => {
72134
createPage({
73135
path: `api/v4${name}`,
74136
component: apiTemplate,
@@ -79,5 +141,5 @@ exports.createPages = async ({ actions, graphql }) => {
79141
});
80142
});
81143

82-
return Promise.all([changelogs, specs]);
144+
return Promise.all([changelogs, specsPages]);
83145
};

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"version": "0.1.0",
66
"author": "Kyle Mathews <[email protected]>",
77
"dependencies": {
8+
"axios": "^0.18.0",
9+
"crypto": "^1.0.1",
810
"gatsby": "^2.0.115",
911
"gatsby-image": "^2.0.29",
1012
"gatsby-plugin-manifest": "^2.0.17",
@@ -18,6 +20,7 @@
1820
"gatsby-transformer-remark": "^2.1.1-rc.5",
1921
"gatsby-transformer-sharp": "^2.1.13",
2022
"gatsby-transformer-yaml": "^2.1.8",
23+
"json-schema-ref-parser": "^6.1.0",
2124
"postcss-easy-import": "^3.0.0",
2225
"prop-types": "^15.6.2",
2326
"react": "^16.7.0",
Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,51 @@
11
import React from "react";
2-
// import { graphql, StaticQuery } from "gatsby";
2+
import { graphql, StaticQuery } from "gatsby";
33

4-
const specs = require("../../data/spec.json");
5-
6-
const SideMenu = () => (
7-
<React.Fragment>
4+
const SideMenu = ({ data }) => {
5+
const nodes = data.allPaths.edges;
6+
return (
87
<ul>
9-
{Object.keys(specs.paths).map(name => {
8+
{nodes.map((node, i) => {
9+
const n = node.node;
1010
return (
11-
<li key={name}>
12-
<a href={`/api/v4${name}`}>{name}</a>
11+
<li key={i} className="list-reset">
12+
<a href={`/api/v4${n.name}`}>
13+
{(n.get && n.get.summary) ||
14+
(n.post && n.post.summary) ||
15+
(n.put && n.put.summary)}
16+
</a>
1317
</li>
1418
);
1519
})}
1620
</ul>
17-
</React.Fragment>
18-
);
21+
);
22+
};
1923

20-
export default SideMenu;
24+
export default props => (
25+
<StaticQuery
26+
query={graphql`
27+
query {
28+
allPaths {
29+
edges {
30+
node {
31+
internal {
32+
contentDigest
33+
}
34+
name
35+
get {
36+
summary
37+
}
38+
post {
39+
summary
40+
}
41+
put {
42+
summary
43+
}
44+
}
45+
}
46+
}
47+
}
48+
`}
49+
render={data => <SideMenu data={data} {...props} />}
50+
/>
51+
);

src/components/5_templates/api-old.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import React from "react";
2+
import * as JsonSchemaRefParser from "json-schema-ref-parser";
3+
4+
import Layout from "../../components/4_layouts/layout";
5+
import SEO from "../../components/0_utlilities/seo";
6+
import Sidebar from "../../components/2_molecules/sidemenu";
7+
8+
const specs = require("../../data/spec.json");
9+
const parser = new JsonSchemaRefParser();
10+
11+
class apiPage extends React.Component {
12+
constructor(props) {
13+
super(props);
14+
15+
this.state = {
16+
loading: true,
17+
apispecs: ""
18+
};
19+
}
20+
21+
componentDidMount() {
22+
this.load();
23+
}
24+
25+
async load() {
26+
try {
27+
this._resolvedSpec = await parser.dereference(specs);
28+
} catch (e) {
29+
this.setState({
30+
error: e
31+
});
32+
} finally {
33+
this.setState({
34+
loading: false,
35+
apispecs: this._resolvedSpec
36+
});
37+
}
38+
}
39+
40+
render() {
41+
const { loading, apispecs } = this.state;
42+
const { pageContext } = this.props;
43+
const modes = {
44+
get: "get",
45+
put: "put",
46+
post: "post"
47+
};
48+
49+
return (
50+
<Layout title="API Documentation" subtitle="Linode API Documentation">
51+
<SEO title="API Documentation" description="" />
52+
<div className="flex flex-wrap">
53+
<div className="w-full md:w-1/4 mt-8">
54+
<Sidebar />
55+
</div>
56+
{!loading && (
57+
<div className="w-full md:w-3/4 md:pl-8">
58+
<h1>
59+
{(apispecs.paths[pageContext.name]["get"] &&
60+
apispecs.paths[pageContext.name]["get"].tags) ||
61+
(apispecs.paths[pageContext.name]["put"] &&
62+
apispecs.paths[pageContext.name]["put"].tags) ||
63+
(apispecs.paths[pageContext.name]["post"] &&
64+
apispecs.paths[pageContext.name]["post"].tags)}
65+
</h1>
66+
{Object.keys(apispecs.paths[pageContext.name]).map(e => {
67+
const mode = modes[e];
68+
const n = apispecs.paths[pageContext.name][mode];
69+
// console.log(n);
70+
return (
71+
mode !== undefined && (
72+
<div key={e}>
73+
<h2>{n.summary}</h2>
74+
<p>
75+
<b>{mode}</b>&nbsp; https://api.linode.com/v4
76+
{pageContext.name}
77+
</p>
78+
<p>{n.description}</p>
79+
<div>
80+
<h3>Responses</h3>
81+
{/* {Object.keys(n.responses).map(response => {
82+
const r = n.responses[response];
83+
return (
84+
<div key={response} className="mb-8">
85+
<b>{response}</b>
86+
{Object.keys(
87+
r.content["application/json"].schema.properties
88+
).map(property => {
89+
return <div>{property}</div>;
90+
})}
91+
</div>
92+
);
93+
})} */}
94+
</div>
95+
</div>
96+
)
97+
);
98+
})}
99+
</div>
100+
)}
101+
</div>
102+
</Layout>
103+
);
104+
}
105+
}
106+
107+
export default apiPage;

src/components/5_templates/api.js

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,16 @@ import Layout from "../../components/4_layouts/layout";
44
import SEO from "../../components/0_utlilities/seo";
55
import Sidebar from "../../components/2_molecules/sidemenu";
66

7-
const specs = require("../../data/spec.json");
8-
9-
const apiPage = ({ pageContext }) => {
10-
const node = specs.paths[pageContext.name];
11-
const modes = {
12-
get: "get",
13-
put: "put",
14-
post: "post"
15-
};
7+
const apiPage = () => {
168
return (
179
<Layout title="API Documentation" subtitle="Linode API Documentation">
1810
<SEO title="API Documentation" description="" />
19-
<h1>
20-
{(node["get"] && node["get"].tags) ||
21-
(node["put"] && node["put"].tags) ||
22-
(node["post"] && node["post"].tags)}
23-
</h1>
24-
{Object.keys(node).map(e => {
25-
const mode = modes[e];
26-
const n = node[mode];
27-
return (
28-
mode !== undefined && (
29-
<div key={e}>
30-
<h2>{mode}</h2>
31-
<p>{n.summary}</p>
32-
</div>
33-
)
34-
);
35-
})}
36-
{/* {node.get && node.get.tags && <div>{node.get.tags}</div>}
37-
{node.get && node.get.summary && <div>{node.get.summary}</div>}
38-
{node.put && node.put.summary && <div>{node.put.summary}</div>}
39-
{node.post && node.post.summary && <div>{node.post.summary}</div>} */}
40-
<Sidebar />
11+
<div className="flex flex-wrap">
12+
<div className="w-full md:w-1/4 mt-8">
13+
<Sidebar />
14+
</div>
15+
poos
16+
</div>
4117
</Layout>
4218
);
4319
};

src/pages/api/v4.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import SEO from "../../components/0_utlilities/seo";
77
import Sidebar from "../../components/2_molecules/sidemenu";
88

99
const APIDocs = ({ data }) => {
10+
// const nodes = data.allPaths.edges;
1011
return (
1112
<Layout title="API Documentation" subtitle="Linode API Documentation">
1213
<SEO title="API Documentation" description="" />
@@ -16,4 +17,47 @@ const APIDocs = ({ data }) => {
1617
);
1718
};
1819

20+
export const query = graphql`
21+
query PathQuery {
22+
allPaths {
23+
edges {
24+
node {
25+
internal {
26+
contentDigest
27+
}
28+
name
29+
get {
30+
x_linode_grant
31+
summary
32+
description
33+
operationId
34+
x_linode_cli_action
35+
x_linode_cli_skip
36+
x_linode_redoc_load_ids
37+
x_linode_cli_command
38+
}
39+
post {
40+
x_linode_grant
41+
summary
42+
description
43+
operationId
44+
x_linode_cli_action
45+
x_linode_cli_command
46+
x_linode_charge
47+
x_linode_cli_skip
48+
}
49+
put {
50+
x_linode_grant
51+
summary
52+
description
53+
operationId
54+
x_linode_cli_action
55+
x_linode_cli_skip
56+
}
57+
}
58+
}
59+
}
60+
}
61+
`;
62+
1963
export default APIDocs;

0 commit comments

Comments
 (0)