|
| 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 | +}; |
0 commit comments