Skip to content
This repository was archived by the owner on Aug 14, 2024. It is now read-only.

Commit acb71d8

Browse files
authored
doc: PoC of loading event schema docs from Relay (#96)
1 parent d8d62f4 commit acb71d8

File tree

14 files changed

+371
-13
lines changed

14 files changed

+371
-13
lines changed

.github/workflows/test.yml

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ jobs:
1313

1414
steps:
1515
- uses: actions/checkout@v2
16+
with:
17+
submodules: true
1618
- uses: volta-cli/action@v1
1719
- run: yarn install
1820
- run: yarn test

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "src/data-schemas"]
2+
path = src/data-schemas
3+
url = https://github.com/getsentry/sentry-data-schemas

gatsby-config.js

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ const getPlugins = () => {
9090
path: `${__dirname}/src/pages`,
9191
},
9292
},
93+
{
94+
resolve: require.resolve("./plugins/gatsby-plugin-jsonschema"),
95+
},
9396
// this (optional) plugin enables Progressive Web App + Offline functionality
9497
// To learn more, visit: https://gatsby.app/offline
9598
// 'gatsby-plugin-offline',

gatsby-node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { createFilePath } = require("gatsby-source-filesystem");
22

33
exports.onCreateNode = ({ node, actions, getNode }) => {
44
const { createNodeField } = actions;
5-
if (node.internal.type === "Mdx") {
5+
if (node.internal.type === "Mdx" && (!node.parent || getNode(node.parent).internal.type !== "JsonSchemaMarkdown")) {
66
const value = createFilePath({ node, getNode });
77
createNodeField({
88
name: "slug",

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"@sentry/apm": "^5.20.0",
88
"@sentry/gatsby": "^5.20.0",
99
"@sentry/react": "^5.20.0",
10+
"@untitaker/quicktype-core-with-markdown": "^6.0.69",
1011
"add": "^2.0.6",
1112
"algoliasearch": "^4.2.0",
1213
"bootstrap": "4.3.1",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const { readFileSync } = require('fs');
2+
const { quicktype, InputData, JSONSchemaInput, JSONSchemaStore } = require("@untitaker/quicktype-core-with-markdown");
3+
4+
exports.sourceNodes = async ({ actions, createNodeId, createContentDigest }) => {
5+
const { createNode } = actions;
6+
7+
let content;
8+
try {
9+
content = readFileSync('./src/data-schemas/relay/event.schema.json', {encoding: "utf8"});
10+
} catch (e) {
11+
console.warn(`Failed to read Relay event schema: ${e}`);
12+
return;
13+
}
14+
15+
createNode({
16+
content,
17+
name: 'Event',
18+
id: 'relay-event', // human-readable ID for referencing in MDX component
19+
parent: null,
20+
children: [],
21+
internal: {
22+
type: `JsonSchema`,
23+
mediaType: 'application/schema+json',
24+
content,
25+
contentDigest: createContentDigest(content),
26+
},
27+
});
28+
};
29+
30+
31+
32+
function quicktypeJSONSchema(targetLanguage, typeName, jsonSchemaString) {
33+
const schemaInput = new JSONSchemaInput(new JSONSchemaStore());
34+
return schemaInput.addSource({ name: typeName, schema: jsonSchemaString })
35+
.then(_ => {
36+
const inputData = new InputData();
37+
inputData.addInput(schemaInput);
38+
39+
return quicktype({
40+
inputData,
41+
lang: targetLanguage,
42+
});
43+
});
44+
}
45+
46+
exports.onCreateNode = async ({ actions, createNodeId, node, createContentDigest }) => {
47+
const { createNode, createParentChildLink } = actions;
48+
49+
if (node.internal.mediaType !== `application/schema+json` || node.internal.type !== `JsonSchema`) {
50+
return;
51+
}
52+
53+
const { lines } = await quicktypeJSONSchema("markdown", node.name, node.content);
54+
55+
const child = {
56+
lines,
57+
content: lines.join("\n"),
58+
id: createNodeId(`${node.id}-markdown`),
59+
parent: node.id,
60+
internal: {
61+
content: lines.join("\n"),
62+
mediaType: 'text/markdown',
63+
contentDigest: createContentDigest(lines),
64+
type: `JsonSchemaMarkdown`
65+
}
66+
};
67+
68+
createNode(child);
69+
createParentChildLink({ parent: node, child });
70+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//noop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "gatsby-plugin-jsonschema",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"peerDependencies": {
7+
"gatsby": "^2.0.0"
8+
},
9+
"dependencies": {
10+
"unist-util-visit": "^1.4.1"
11+
}
12+
}

src/components/jsonschema.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React from "react"
2+
import { MDXProvider } from "@mdx-js/react";
3+
import { MDXRenderer } from "gatsby-plugin-mdx";
4+
import { graphql, useStaticQuery } from "gatsby"
5+
6+
const JsonSchema = ({id}) => {
7+
// XXX(markus): No clue if this can be replaced by non-static query
8+
//
9+
// This contrived query takes extra care in not referencing allJsonSchema or
10+
// allJsonSchemaMarkdown, as those nodes are not usable if they have not been
11+
// constructed once. That is the case if the data-schema submodule has not
12+
// been checked out in which case we can still show a dummy text instead of
13+
// failing the build.
14+
const query = useStaticQuery(graphql`
15+
{
16+
allMdx(filter: {parent: {internal: {type: {eq: "JsonSchemaMarkdown"}}}}) {
17+
nodes {
18+
body
19+
parent {
20+
parent {
21+
id
22+
}
23+
}
24+
}
25+
}
26+
}
27+
`);
28+
29+
const mdxNode = query.allMdx.nodes.find(node => node.parent.parent.id === id);
30+
31+
if (!mdxNode) {
32+
return "Failed to load JSON Schema. Either the ID you have passed into the component does not exist or you are missing some git submmodules.";
33+
}
34+
35+
return (
36+
<MDXProvider>
37+
<MDXRenderer>{mdxNode.body}</MDXRenderer>
38+
</MDXProvider>
39+
);
40+
}
41+
42+
export default JsonSchema;

src/components/layout.js

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import SmartLink from "./smartLink";
1212
import CodeBlock from "./codeBlock";
1313
import CodeTabs, { CodeContext, useCodeContextState } from "./codeTabs";
1414
import Break from "./break";
15+
import JsonSchema from "./jsonschema";
1516

1617
import "prismjs/themes/prism-tomorrow.css";
1718
import "../css/screen.scss";
@@ -23,6 +24,7 @@ const mdxComponents = {
2324
CodeBlock,
2425
CodeTabs,
2526
Break,
27+
JsonSchema,
2628
};
2729

2830
const TableOfContents = ({ toc: { items } }) => {

src/data-schemas

Submodule data-schemas added at b6d94bb

src/docs/sdk/event-payloads/index.mdx

+4
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,7 @@ The core data interfaces are:
270270

271271
- <Link to="/sdk/event-payloads/debugmeta">Debug Meta Interface</Link>
272272
- <Link to="/sdk/event-payloads/sdk">SDK Interface</Link>
273+
274+
## Type Definitions
275+
276+
- <Link to="/sdk/event-payloads/types">Event Type Definitions</Link>

src/docs/sdk/event-payloads/types.mdx

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Event Type Definitions
3+
---
4+
5+
This page documents the event schema for errors, just like the other documents
6+
at <Link to="/sdk/event-payloads/">Event Payloads</Link> do. However, the
7+
sections here are automatically generated from code, and because of that they
8+
are more complete and less out of date. We intend to make this page the single
9+
source of truth and replace all the other "interface" pages, but there is still
10+
some work to be done to make this more human-readable.
11+
12+
Go to [our schemas repo](https://github.com/getsentry/sentry-data-schemas) to
13+
learn more.
14+
15+
<JsonSchema id="relay-event" />

0 commit comments

Comments
 (0)