@graphql-markdown/docusaurus 1.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Grégory Heitz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ [![GraphQL-Markdown](https://user-images.githubusercontent.com/324670/188957463-dae99daa-763d-466e-91f4-0629f455df74.svg)](https://graphql-markdown.github.io)
2
+
3
+ <h1 align="center">GraphQL documentation generator for Docusaurus</h1>
4
+
5
+ [![Latest Version](https://img.shields.io/npm/v/@graphql-markdown/docusaurus?style=flat-square)](https://www.npmjs.com/package/@graphql-markdown/docusaurus)
6
+ [![GitHub License](https://img.shields.io/github/license/graphql-markdown/graphql-markdown?style=flat-square)](https://raw.githubusercontent.com/graphql-markdown/graphql-markdown/main/LICENSE)
7
+ [![Buy us a tree](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-lightgreen)](https://plant.treeware.earth/graphql-markdown/graphql-markdown)
8
+ [![Coverage Status](https://img.shields.io/coveralls/github/graphql-markdown/graphql-markdown?style=flat-square)](https://coveralls.io/github/graphql-markdown/graphql-markdown?branch=main)
9
+ [![Mutation Score](https://img.shields.io/endpoint?label=mutation%20score&style=flat-square&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fgraphql-markdown%2Fgraphql-markdown%2Fmain)](https://dashboard.stryker-mutator.io/reports/github.com/graphql-markdown/graphql-markdown/main)
10
+ [![Sonar Tech Debt](https://img.shields.io/sonar/tech_debt/graphql-markdown_graphql-markdown/main?server=https%3A%2F%2Fsonarcloud.io&style=flat-square)](https://sonarcloud.io/project/overview?id=graphql-markdown_graphql-markdown)
11
+ [![Snyk Package Health](https://snyk.io/advisor/npm-package/@graphql-markdown/docusaurus/badge.svg)](https://snyk.io/advisor/npm-package/@graphql-markdown/docusaurus)
12
+ [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
13
+
14
+ This plugin generates **Markdown pages** from a **GraphQL schema** for **Docusaurus** [docs feature](https://docusaurus.io/docs/docs-introduction).
15
+
16
+ ## Installation
17
+
18
+ **`graphql` package is a peer-dependency, and it should be installed separately.**
19
+
20
+ ```shell
21
+ npm install @graphql-markdown/docusaurus graphql
22
+ ```
23
+
24
+ Add `@graphql-markdown/docusaurus` to your site's `docusaurus.config.js` plugins option:
25
+
26
+ ```js
27
+ module.exports = {
28
+ // ...
29
+ plugins: ["@graphql-markdown/docusaurus"],
30
+ };
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ The plugin adds a new command `graphql-to-doc` to the [Docusaurus CLI](https://docusaurus.io/docs/cli).
36
+
37
+ ```shell
38
+ npx docusaurus graphql-to-doc
39
+ ```
40
+
41
+ Command line options are described in the [documentation settings](https://graphql-markdown.github.io/docs/settings) page.
42
+
43
+ ## Configuration
44
+
45
+ See [documentation settings](https://graphql-markdown.github.io/docs/settings) page.
46
+
47
+ ## Troubleshooting
48
+
49
+ See [documentation troubleshooting](https://graphql-markdown.github.io/docs/troubleshooting) page.
50
+
51
+ ## License
52
+
53
+ GraphQL-Markdown packages are 100% free and open-source, under the [MIT license](https://github.com/graphql-markdown/graphql-markdown/blob/main/LICENSE).
54
+
55
+ This package is [Treeware](https://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/graphql-markdown/graphql-markdown) to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
56
+
57
+ ## Contributions
58
+
59
+ [Contributions](https://github.com/graphql-markdown/graphql-markdown/blob/main/CONTRIBUTING.md), issues and feature requests are very welcome. If you are using this package and fixed a bug for yourself, please consider submitting a PR!
60
+
61
+ <a href="https://github.com/graphql-markdown/graphql-markdown/graphs/contributors">
62
+ <img src="https://contrib.rocks/image?repo=graphql-markdown/graphql-markdown" />
63
+ </a>
64
+
65
+ Made with [contributors-img](https://contrib.rocks).
@@ -0,0 +1,19 @@
1
+ ---
2
+ id: schema
3
+ slug: ##baseURL##
4
+ title: Schema Documentation
5
+ sidebar_position: 1
6
+ hide_table_of_contents: true
7
+ pagination_next: null
8
+ pagination_prev: null
9
+ sidebar_class_name: navbar__toggle
10
+ ---
11
+
12
+ This documentation has been automatically generated from the GraphQL schema.
13
+
14
+ Use the docs in the sidebar to find out how to use the schema:
15
+
16
+ - **Allowed operations**: queries and mutations.
17
+ - **Schema-defined types**: scalars, objects, enums, interfaces, unions, and input objects.
18
+
19
+ <small><i>Generated on ##generated-date-time##.</i></small>
@@ -0,0 +1,8 @@
1
+ {
2
+ "schemaSidebar": [
3
+ {
4
+ "type": "autogenerated",
5
+ "dirName": "schema"
6
+ }
7
+ ]
8
+ }
package/package.json ADDED
@@ -0,0 +1,185 @@
1
+ {
2
+ "name": "@graphql-markdown/docusaurus",
3
+ "homepage": "https://graphql-markdown.github.io",
4
+ "description": "Docusaurus plugin for generating Markdown documentation from a GraphQL schema.",
5
+ "bugs": {
6
+ "url": "https://github.com/graphql-markdown/graphql-markdown/issues"
7
+ },
8
+ "version": "1.14.0",
9
+ "license": "MIT",
10
+ "main": "src/index.js",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/graphql-markdown/graphql-markdown.git"
14
+ },
15
+ "keywords": [
16
+ "docusaurus",
17
+ "graphql",
18
+ "graphql-schema",
19
+ "documentation",
20
+ "plugin",
21
+ "markdown",
22
+ "graphql-markdown"
23
+ ],
24
+ "author": {
25
+ "name": "Gregory Heitz"
26
+ },
27
+ "contributors": [
28
+ {
29
+ "name": "jocrau"
30
+ },
31
+ {
32
+ "name": "mimo84"
33
+ },
34
+ {
35
+ "name": "coder2034"
36
+ },
37
+ {
38
+ "name": "lauzierj"
39
+ },
40
+ {
41
+ "name": "jineshshah36"
42
+ },
43
+ {
44
+ "name": "GuiHash"
45
+ },
46
+ {
47
+ "name": "davidyaha"
48
+ },
49
+ {
50
+ "name": "jlndk"
51
+ },
52
+ {
53
+ "name": "elias-pap"
54
+ }
55
+ ],
56
+ "scripts": {
57
+ "prettier": "prettier \"{src,tests,assets}/**/*.{js,json,md}\"",
58
+ "lint": "eslint \"{src,tests,assets}/**/*.js\"",
59
+ "test": "jest",
60
+ "test:unit": "jest --projects tests/unit",
61
+ "test:integration": "jest --projects tests/integration",
62
+ "test:smoke": "earthly +smoke-test",
63
+ "test:coverage": "sh ./scripts/coverage.sh",
64
+ "test:mutation": "stryker run --reporters progress,html",
65
+ "prepare": "husky install",
66
+ "prepack": "pinst --disable",
67
+ "postpack": "pinst --enable",
68
+ "stryker": "stryker"
69
+ },
70
+ "dependencies": {
71
+ "@graphql-inspector/core": "^3.3.0",
72
+ "@graphql-tools/graphql-file-loader": "^7.5.2",
73
+ "@graphql-tools/load": "^7.7.4"
74
+ },
75
+ "peerDependencies": {
76
+ "graphql": "^14.0 || ^15.0 || ^16.0",
77
+ "prettier": "^2.5"
78
+ },
79
+ "peerDependenciesMeta": {
80
+ "prettier": {
81
+ "optional": true
82
+ }
83
+ },
84
+ "devDependencies": {
85
+ "@stryker-mutator/core": "6.2.2",
86
+ "@stryker-mutator/jest-runner": "6.2.2",
87
+ "commitizen": "4.2.5",
88
+ "cz-emoji": "1.3.1",
89
+ "diff": "5.1.0",
90
+ "directory-tree": "3.3.1",
91
+ "eslint": "8.24.0",
92
+ "eslint-config-prettier": "8.5.0",
93
+ "eslint-plugin-jest": "27.1.0",
94
+ "eslint-plugin-node": "11.1.0",
95
+ "eslint-plugin-prettier": "4.2.1",
96
+ "graphql": "16.6.0",
97
+ "husky": "8.0.1",
98
+ "jest": "29.1.2",
99
+ "jest-file-snapshot": "0.5.0",
100
+ "lodash.filter": "4.6.0",
101
+ "mock-fs": "5.1.4",
102
+ "pinst": "3.0.0",
103
+ "prettier": "2.7.1"
104
+ },
105
+ "directories": {
106
+ "test": "tests"
107
+ },
108
+ "engines": {
109
+ "node": ">=16.14"
110
+ },
111
+ "config": {
112
+ "commitizen": {
113
+ "path": "./node_modules/cz-emoji"
114
+ },
115
+ "cz-emoji": {
116
+ "skipQuestions": [
117
+ "scope",
118
+ "issues",
119
+ "breaking"
120
+ ],
121
+ "types": [
122
+ {
123
+ "emoji": "✨",
124
+ "code": ":sparkles:",
125
+ "description": "Add a new feature.",
126
+ "name": "feat"
127
+ },
128
+ {
129
+ "emoji": "🐛",
130
+ "code": ":bug:",
131
+ "description": "Fix a bug.",
132
+ "name": "fix"
133
+ },
134
+ {
135
+ "emoji": "📦️",
136
+ "code": ":package:",
137
+ "description": "Update dependencies.",
138
+ "name": "build"
139
+ },
140
+ {
141
+ "emoji": "🔧",
142
+ "code": ":wrench:",
143
+ "description": "Update configuration files.",
144
+ "name": "conf"
145
+ },
146
+ {
147
+ "emoji": "♻️",
148
+ "code": ":recycle:",
149
+ "description": "Refactor code.",
150
+ "name": "refactor"
151
+ },
152
+ {
153
+ "emoji": "🧪",
154
+ "code": ":test_tube:",
155
+ "description": "Update tests.",
156
+ "name": "test"
157
+ },
158
+ {
159
+ "emoji": "👷",
160
+ "code": ":construction_worker:",
161
+ "description": "Update CI.",
162
+ "name": "ci"
163
+ },
164
+ {
165
+ "emoji": "📝",
166
+ "code": ":memo:",
167
+ "description": "Update documentation.",
168
+ "name": "docs"
169
+ },
170
+ {
171
+ "emoji": "🔖",
172
+ "code": ":bookmark:",
173
+ "description": "Add a tag / release.",
174
+ "name": "tag"
175
+ },
176
+ {
177
+ "emoji": "🧑‍💻",
178
+ "code": ":technologist:",
179
+ "description": "Other changes.",
180
+ "name": "other"
181
+ }
182
+ ]
183
+ }
184
+ }
185
+ }
package/src/config.js ADDED
@@ -0,0 +1,83 @@
1
+ const { join } = require("path");
2
+ const { tmpdir } = require("os");
3
+
4
+ const { parseGroupByOption } = require("./lib/group-info");
5
+ const { COMPARE_METHOD } = require("./lib/diff");
6
+
7
+ const PACKAGE_NAME = "@graphql-markdown/docusaurus";
8
+ const ASSETS_LOCATION = join(__dirname, "../assets/");
9
+
10
+ const DEFAULT_OPTIONS = {
11
+ schema: "./schema.graphql",
12
+ rootPath: "./docs",
13
+ baseURL: "schema",
14
+ linkRoot: "/",
15
+ homepage: join(ASSETS_LOCATION, "generated.md"),
16
+ diffMethod: COMPARE_METHOD.DIFF,
17
+ tmpDir: join(tmpdir(), PACKAGE_NAME),
18
+ loaders: {},
19
+ pretty: false,
20
+ docOptions: {
21
+ pagination: true,
22
+ toc: true,
23
+ index: false,
24
+ },
25
+ printTypeOptions: {
26
+ parentTypePrefix: true,
27
+ relatedTypeSection: true,
28
+ typeBadges: true,
29
+ },
30
+ };
31
+
32
+ function buildConfig(configFileOpts, cliOpts) {
33
+ let config = DEFAULT_OPTIONS;
34
+
35
+ if (typeof configFileOpts != "undefined" && configFileOpts != null) {
36
+ config = { ...DEFAULT_OPTIONS, ...configFileOpts };
37
+ }
38
+
39
+ if (typeof cliOpts == "undefined" || cliOpts == null) {
40
+ cliOpts = {};
41
+ }
42
+
43
+ const baseURL = cliOpts.base ?? config.baseURL;
44
+
45
+ return {
46
+ baseURL,
47
+ schemaLocation: cliOpts.schema ?? config.schema,
48
+ outputDir: join(cliOpts.root ?? config.rootPath, baseURL),
49
+ linkRoot: cliOpts.link ?? config.linkRoot,
50
+ homepageLocation: cliOpts.homepage ?? config.homepage,
51
+ diffMethod: getDiffMethod(cliOpts.diff ?? config.diffMethod, cliOpts.force),
52
+ tmpDir: cliOpts.tmp ?? config.tmpDir,
53
+ loaders: config.loaders,
54
+ groupByDirective:
55
+ parseGroupByOption(cliOpts.groupByDirective) || config.groupByDirective,
56
+ prettify: cliOpts.pretty ?? config.pretty,
57
+ docOptions: getDocOptions(cliOpts, config.docOptions),
58
+ printTypeOptions: gePrintTypeOptions(cliOpts, config.printTypeOptions),
59
+ };
60
+ }
61
+
62
+ function getDiffMethod(diff, force) {
63
+ return force ? COMPARE_METHOD.FORCE : diff;
64
+ }
65
+
66
+ function getDocOptions(cliOpts, configOptions) {
67
+ return {
68
+ pagination: !cliOpts.noPagination && configOptions.pagination,
69
+ toc: !cliOpts.noToc && configOptions.toc,
70
+ index: cliOpts.index || configOptions.index,
71
+ };
72
+ }
73
+
74
+ function gePrintTypeOptions(cliOpts, configOptions) {
75
+ return {
76
+ parentTypePrefix: !cliOpts.noParentType && configOptions.parentTypePrefix,
77
+ relatedTypeSection:
78
+ !cliOpts.noRelatedType && configOptions.relatedTypeSection,
79
+ typeBadges: !cliOpts.noTypeBadges && configOptions.typeBadges,
80
+ };
81
+ }
82
+
83
+ module.exports = { buildConfig, DEFAULT_OPTIONS, ASSETS_LOCATION };
@@ -0,0 +1,7 @@
1
+ module.exports = `
2
+ export const Bullet = () => <><span style={{ fontWeight: 'normal', fontSize: '.5em', color: 'var(--ifm-color-secondary-darkest)' }}>&nbsp;●&nbsp;</span></>
3
+
4
+ export const SpecifiedBy = (props) => <>Specification<a className="link" style={{ fontSize:'1.5em', paddingLeft:'4px' }} target="_blank" href={props.url} title={'Specified by ' + props.url}>⎘</a></>
5
+
6
+ export const Badge = (props) => <><span class={'badge badge--' + props.class}>{props.text}</span></>
7
+ `;
@@ -0,0 +1,30 @@
1
+ const ROOT_TYPE_LOCALE = {
2
+ QUERY: { singular: "query", plural: "queries" },
3
+ MUTATION: { singular: "mutation", plural: "mutations" },
4
+ SUBSCRIPTION: { singular: "subscription", plural: "subscriptions" },
5
+ TYPE: { singular: "object", plural: "objects" },
6
+ INTERFACE: { singular: "interface", plural: "interfaces" },
7
+ DIRECTIVE: { singular: "directive", plural: "directives" },
8
+ SCALAR: { singular: "scalar", plural: "scalars" },
9
+ ENUM: { singular: "enum", plural: "enums" },
10
+ OPERATION: { singular: "operation", plural: "operations" },
11
+ UNION: { singular: "union", plural: "unions" },
12
+ INPUT: { singular: "input", plural: "inputs" },
13
+ };
14
+
15
+ const HEADER_SECTION_LEVEL = "###";
16
+ const HEADER_SECTION_SUB_LEVEL = "####";
17
+ const HEADER_SECTION_ITEM_LEVEL = "#####";
18
+ const NO_DESCRIPTION_TEXT = "No description";
19
+ const MARKDOWN_EOL = "\n";
20
+ const MARKDOWN_EOP = "\n\n";
21
+
22
+ module.exports = {
23
+ ROOT_TYPE_LOCALE,
24
+ HEADER_SECTION_LEVEL,
25
+ HEADER_SECTION_SUB_LEVEL,
26
+ HEADER_SECTION_ITEM_LEVEL,
27
+ NO_DESCRIPTION_TEXT,
28
+ MARKDOWN_EOL,
29
+ MARKDOWN_EOP,
30
+ };
package/src/index.js ADDED
@@ -0,0 +1,51 @@
1
+ /* istanbul ignore file */
2
+ const { buildConfig } = require("./config.js");
3
+ const generateDocFromSchema = require("./lib/generator");
4
+
5
+ const NAME = "docusaurus-graphql-doc-generator";
6
+ const COMMAND = "graphql-to-doc";
7
+ const DESCRIPTION = "Generate GraphQL Schema Documentation";
8
+ const DEFAULT_ID = "default";
9
+
10
+ module.exports = function pluginGraphQLDocGenerator(_, configOptions) {
11
+ const isDefaultId = configOptions.id === DEFAULT_ID;
12
+
13
+ const command = isDefaultId ? COMMAND : `${COMMAND}:${configOptions.id}`;
14
+ const description = isDefaultId
15
+ ? DESCRIPTION
16
+ : `${DESCRIPTION} for configuration with id ${configOptions.id}`;
17
+
18
+ return {
19
+ name: NAME,
20
+ extendCli(cli) {
21
+ cli
22
+ .command(command)
23
+ .description(description)
24
+ .option("-s, --schema <schema>", "Schema location")
25
+ .option("-r, --root <rootPath>", "Root folder for doc generation")
26
+ .option("-b, --base <baseURL>", "Base URL to be used by Docusaurus")
27
+ .option("-l, --link <linkRoot>", "Root for links in documentation")
28
+ .option(
29
+ "-h, --homepage <homepage>",
30
+ "File location for doc landing page",
31
+ )
32
+ .option("--noPagination", "Disable page navigation buttons")
33
+ .option("--noParentType", "Disable parent type name as field prefix")
34
+ .option("--noRelatedType", "Disable related types sections")
35
+ .option("--noToc", "Disable page table of content")
36
+ .option("--noTypeBadges", "Disable badges for types")
37
+ .option("--index", "Enable generated index for categories")
38
+ .option("-f, --force", "Force document generation")
39
+ .option("-d, --diff <diffMethod>", "Set diff method")
40
+ .option("-t, --tmp <tmpDir>", "Set temp dir for schema diff")
41
+ .option(
42
+ "-gbd, --groupByDirective <@directive(field|=fallback)>",
43
+ "Group documentation by directive",
44
+ )
45
+ .option("--pretty", "Prettify generated files")
46
+ .action(async (cliOptions) => {
47
+ await generateDocFromSchema(buildConfig(configOptions, cliOptions));
48
+ });
49
+ },
50
+ };
51
+ };
@@ -0,0 +1,79 @@
1
+ const path = require("path");
2
+ const crypto = require("crypto");
3
+
4
+ const { fileExists, readFile, saveFile } = require("../utils/helpers/fs");
5
+
6
+ const { loadSchema, getDocumentLoaders, printSchema } = require("./graphql");
7
+ const { diff } = require("@graphql-inspector/core");
8
+
9
+ const SCHEMA_HASH_FILE = ".schema";
10
+ const SCHEMA_REF = "schema.graphql";
11
+ const COMPARE_METHOD = {
12
+ DIFF: "SCHEMA-DIFF",
13
+ HASH: "SCHEMA-HASH",
14
+ FORCE: "FORCE",
15
+ };
16
+
17
+ const defaultLoaders = {
18
+ GraphQLFileLoader: "@graphql-tools/graphql-file-loader",
19
+ };
20
+
21
+ function getSchemaHash(schema) {
22
+ let printedSchema = printSchema(schema, { commentDescriptions: true });
23
+ let sum = crypto.createHash("sha256").update(printedSchema);
24
+ return sum.digest("hex");
25
+ }
26
+
27
+ async function getDiff(schemaNew, schemaOld) {
28
+ const schemaRef = await loadSchema(schemaOld, {
29
+ loaders: getDocumentLoaders(defaultLoaders).loaders,
30
+ });
31
+ return diff(schemaRef, schemaNew);
32
+ }
33
+
34
+ async function checkSchemaChanges(
35
+ schema,
36
+ outputDir,
37
+ method = COMPARE_METHOD.DIFF,
38
+ ) {
39
+ const hashFile = path.join(outputDir, SCHEMA_HASH_FILE);
40
+ const hashSchema = getSchemaHash(schema);
41
+ const schemaRef = path.join(outputDir, SCHEMA_REF);
42
+
43
+ if (method === COMPARE_METHOD.DIFF) {
44
+ if (await fileExists(schemaRef)) {
45
+ const schemaDiff = await getDiff(schema, schemaRef);
46
+ return schemaDiff.length > 0;
47
+ }
48
+ }
49
+
50
+ if (method === COMPARE_METHOD.HASH) {
51
+ if (await fileExists(hashFile)) {
52
+ const hash = await readFile(hashFile);
53
+ return hashSchema != hash;
54
+ }
55
+ }
56
+
57
+ return true;
58
+ }
59
+
60
+ async function saveSchemaFile(schema, outputDir) {
61
+ const schemaFile = path.join(outputDir, SCHEMA_REF);
62
+ const schemaPrint = printSchema(schema);
63
+ await saveFile(schemaFile, schemaPrint);
64
+ }
65
+
66
+ async function saveSchemaHash(schema, outputDir) {
67
+ const hashFile = path.join(outputDir, SCHEMA_HASH_FILE);
68
+ const hashSchema = getSchemaHash(schema);
69
+ await saveFile(hashFile, hashSchema);
70
+ }
71
+
72
+ module.exports = {
73
+ checkSchemaChanges,
74
+ saveSchemaHash,
75
+ saveSchemaFile,
76
+ COMPARE_METHOD,
77
+ SCHEMA_HASH_FILE,
78
+ SCHEMA_REF,
79
+ };
@@ -0,0 +1,78 @@
1
+ const { getSchemaMap, loadSchema, getDocumentLoaders } = require("./graphql");
2
+ const { getGroups } = require("./group-info");
3
+ const Renderer = require("./renderer");
4
+ const Printer = require("./printer");
5
+ const {
6
+ checkSchemaChanges,
7
+ saveSchemaHash,
8
+ saveSchemaFile,
9
+ } = require("./diff");
10
+
11
+ const time = process.hrtime();
12
+
13
+ module.exports = async function generateDocFromSchema({
14
+ baseURL,
15
+ schemaLocation,
16
+ outputDir,
17
+ linkRoot,
18
+ homepageLocation,
19
+ diffMethod,
20
+ tmpDir,
21
+ loaders,
22
+ groupByDirective,
23
+ prettify,
24
+ docOptions,
25
+ printTypeOptions,
26
+ }) {
27
+ const { loaders: documentLoaders, loaderOptions } =
28
+ getDocumentLoaders(loaders);
29
+ const schema = await loadSchema(schemaLocation, {
30
+ loaders: documentLoaders,
31
+ ...loaderOptions,
32
+ });
33
+
34
+ const hasChanged = await checkSchemaChanges(schema, tmpDir, diffMethod);
35
+
36
+ if (hasChanged) {
37
+ const rootTypes = getSchemaMap(schema);
38
+ const groups = new getGroups(rootTypes, groupByDirective);
39
+ const renderer = new Renderer(
40
+ new Printer(schema, baseURL, linkRoot, {
41
+ groups,
42
+ printTypeOptions,
43
+ }),
44
+ outputDir,
45
+ baseURL,
46
+ groups,
47
+ prettify,
48
+ docOptions,
49
+ );
50
+ const pages = await Promise.all(
51
+ Object.keys(rootTypes).map((typeName) =>
52
+ renderer.renderRootTypes(typeName, rootTypes[typeName]),
53
+ ),
54
+ );
55
+ await renderer.renderHomepage(homepageLocation);
56
+ const sidebarPath = await renderer.renderSidebar();
57
+
58
+ const [sec, msec] = process.hrtime(time);
59
+ const duration = (sec + msec / 1e9).toFixed(3);
60
+ console.info(
61
+ `Documentation successfully generated in "${outputDir}" with base URL "${baseURL}".`,
62
+ );
63
+ console.log(
64
+ `${
65
+ pages.flat().length
66
+ } pages generated in ${duration}s from schema "${schemaLocation}".`,
67
+ );
68
+ console.info(
69
+ `Remember to update your Docusaurus site's sidebars with "${sidebarPath}".`,
70
+ );
71
+
72
+ // create references for checkSchemaChanges
73
+ await saveSchemaHash(schema, tmpDir);
74
+ await saveSchemaFile(schema, tmpDir);
75
+ } else {
76
+ console.info(`No changes detected in schema "${schemaLocation}".`);
77
+ }
78
+ };