@graphql-markdown/core 1.4.2 → 1.6.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/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "bugs": {
6
6
  "url": "https://github.com/graphql-markdown/graphql-markdown/issues"
7
7
  },
8
- "version": "1.4.2",
8
+ "version": "1.6.0",
9
9
  "license": "MIT",
10
10
  "main": "src/index.js",
11
11
  "repository": {
@@ -29,11 +29,12 @@
29
29
  "stryker": "stryker run"
30
30
  },
31
31
  "dependencies": {
32
- "@graphql-markdown/utils": "^1.4.1"
32
+ "@graphql-markdown/utils": "^1.5.0"
33
33
  },
34
34
  "peerDependencies": {
35
- "@graphql-markdown/printer-legacy": "^1.3.2",
36
- "@graphql-markdown/diff": "^1.0.11",
35
+ "@graphql-markdown/diff": "^1.0.13",
36
+ "@graphql-markdown/printer-legacy": "^1.4.0",
37
+ "graphql-config": "^5.0.2",
37
38
  "prettier": "^2.8"
38
39
  },
39
40
  "peerDependenciesMeta": {
@@ -43,12 +44,16 @@
43
44
  "@graphql-markdown/diff": {
44
45
  "optional": true
45
46
  },
47
+ "graphql-config": {
48
+ "optional": true
49
+ },
46
50
  "prettier": {
47
51
  "optional": true
48
52
  }
49
53
  },
50
54
  "devDependencies": {
51
- "@graphql-markdown/printer-legacy": "^1.1.3"
55
+ "@graphql-markdown/printer-legacy": "^1.1.3",
56
+ "graphql-config": "^5.0.2"
52
57
  },
53
58
  "directories": {
54
59
  "test": "tests"
package/src/config.js CHANGED
@@ -5,6 +5,8 @@ const {
5
5
  object: { hasProperty },
6
6
  } = require("@graphql-markdown/utils");
7
7
 
8
+ const { loadConfiguration } = require("./graphql-config");
9
+
8
10
  const PACKAGE_NAME = "@graphql-markdown/docusaurus";
9
11
  const ASSETS_LOCATION = join(__dirname, "../assets/");
10
12
 
@@ -35,17 +37,14 @@ const DEFAULT_OPTIONS = {
35
37
  skipDocDirective: [],
36
38
  };
37
39
 
38
- function buildConfig(configFileOpts, cliOpts) {
39
- let config = DEFAULT_OPTIONS;
40
-
41
- if (typeof configFileOpts !== "undefined" && configFileOpts != null) {
42
- config = { ...DEFAULT_OPTIONS, ...configFileOpts };
43
- }
44
-
45
- if (typeof cliOpts === "undefined" || cliOpts == null) {
40
+ function buildConfig(configFileOpts, cliOpts, id = undefined) {
41
+ if (typeof cliOpts === "undefined" || cliOpts === null) {
46
42
  cliOpts = {};
47
43
  }
48
44
 
45
+ const graphqlConfig = loadConfiguration(id);
46
+ const config = { ...DEFAULT_OPTIONS, ...graphqlConfig, ...configFileOpts };
47
+
49
48
  const baseURL = cliOpts.base ?? config.baseURL;
50
49
  const skipDocDirective = getSkipDocDirectives(cliOpts, config);
51
50
 
@@ -85,11 +84,12 @@ function getCustomDirectives(customDirectiveOptions, skipDocDirective = []) {
85
84
  if (skipDocDirective.includes(name)) {
86
85
  delete customDirectiveOptions[name];
87
86
  } else if (
88
- hasProperty(option, "descriptor") === false ||
89
- typeof option.descriptor !== "function"
87
+ (hasProperty(option, "descriptor") === false ||
88
+ typeof option.descriptor !== "function") &&
89
+ (hasProperty(option, "tag") === false || typeof option.tag !== "function")
90
90
  ) {
91
91
  throw new Error(
92
- `Wrong format for plugin custom directive "${name}", it should be {descriptor: (directiveType, constDirectiveType) => String}`,
92
+ `Wrong format for plugin custom directive "${name}".\nPlease refer to https://graphql-markdown.github.io/docs/advanced/custom-directive`,
93
93
  );
94
94
  }
95
95
  }
package/src/diff.js ADDED
@@ -0,0 +1,30 @@
1
+ const logger = require("@graphql-markdown/utils").logger.getInstance();
2
+
3
+ const hasChanges = async (
4
+ schema,
5
+ tmpDir,
6
+ diffMethod,
7
+ diffModule = "@graphql-markdown/diff",
8
+ ) => {
9
+ if (
10
+ typeof diffMethod === "undefined" ||
11
+ diffMethod == null ||
12
+ typeof diffModule === "undefined" ||
13
+ diffModule == null
14
+ ) {
15
+ return true;
16
+ }
17
+
18
+ try {
19
+ const { checkSchemaChanges } = require(diffModule);
20
+ return await checkSchemaChanges(schema, tmpDir, diffMethod);
21
+ } catch (error) {
22
+ logger.warn(
23
+ `Cannot find module '${diffModule}' from @graphql-markdown/core!`,
24
+ );
25
+ }
26
+
27
+ return true;
28
+ };
29
+
30
+ module.exports = { hasChanges };
package/src/generator.js CHANGED
@@ -3,61 +3,15 @@ const {
3
3
  group: { getGroups },
4
4
  directive: { getCustomDirectives },
5
5
  } = require("@graphql-markdown/utils");
6
+
7
+ const { logger: Logger } = require("@graphql-markdown/utils");
6
8
  const Renderer = require("./renderer");
9
+ const { hasChanges } = require("./diff");
10
+ const { getPrinter } = require("./printer");
7
11
 
8
12
  const NS_PER_SEC = 1e9;
9
13
  const SEC_DECIMALS = 3;
10
14
 
11
- const hasChanges = async (
12
- schema,
13
- tmpDir,
14
- diffMethod,
15
- diffModule = "@graphql-markdown/diff",
16
- ) => {
17
- if (
18
- typeof diffMethod === "undefined" ||
19
- diffMethod == null ||
20
- typeof diffModule === "undefined" ||
21
- diffModule == null
22
- ) {
23
- return true;
24
- }
25
-
26
- try {
27
- const { checkSchemaChanges } = require(diffModule);
28
- return await checkSchemaChanges(schema, tmpDir, diffMethod);
29
- } catch (error) {
30
- console.warn(
31
- `Cannot find module '${diffModule}' from @graphql-markdown/core!`,
32
- );
33
- }
34
-
35
- return true;
36
- };
37
-
38
- const getPrinter = (printerModule, config, options) => {
39
- let Printer = undefined;
40
-
41
- if (typeof printerModule !== "string") {
42
- throw new Error(
43
- 'Invalid printer module name in "printTypeOptions" settings.',
44
- );
45
- }
46
-
47
- try {
48
- Printer = require(printerModule);
49
- } catch (error) {
50
- throw new Error(
51
- `Cannot find module '${printerModule}' for @graphql-markdown/core in "printTypeOptions" settings.`,
52
- );
53
- }
54
-
55
- const { schema, baseURL, linkRoot } = config;
56
- Printer.init(schema, baseURL, linkRoot, { ...options });
57
-
58
- return Printer;
59
- };
60
-
61
15
  const generateDocFromSchema = async ({
62
16
  baseURL,
63
17
  schemaLocation,
@@ -74,15 +28,18 @@ const generateDocFromSchema = async ({
74
28
  printer: printerModule,
75
29
  skipDocDirective,
76
30
  customDirective,
31
+ loggerModule,
77
32
  }) => {
78
33
  const start = process.hrtime.bigint();
79
34
 
35
+ const logger = Logger.setInstance(loggerModule);
36
+
80
37
  const loaders = getDocumentLoaders(loadersList);
81
38
  const schema = await loadSchema(schemaLocation, loaders);
82
39
 
83
40
  const changed = await hasChanges(schema, tmpDir, diffMethod);
84
41
  if (!changed) {
85
- console.info(`No changes detected in schema "${schemaLocation}".`);
42
+ logger.info(`No changes detected in schema "${schemaLocation}".`);
86
43
  }
87
44
 
88
45
  const rootTypes = getSchemaMap(schema);
@@ -126,17 +83,17 @@ const generateDocFromSchema = async ({
126
83
  Number(process.hrtime.bigint() - start) / NS_PER_SEC
127
84
  ).toFixed(SEC_DECIMALS);
128
85
 
129
- console.info(
86
+ logger.success(
130
87
  `Documentation successfully generated in "${outputDir}" with base URL "${baseURL}".`,
131
88
  );
132
- console.log(
89
+ logger.info(
133
90
  `${
134
91
  pages.flat().length
135
92
  } pages generated in ${duration}s from schema "${schemaLocation}".`,
136
93
  );
137
- console.info(
94
+ logger.info(
138
95
  `Remember to update your Docusaurus site's sidebars with "${sidebarPath}".`,
139
96
  );
140
97
  };
141
98
 
142
- module.exports = { getPrinter, hasChanges, generateDocFromSchema };
99
+ module.exports = { generateDocFromSchema };
@@ -0,0 +1,61 @@
1
+ const logger = require("@graphql-markdown/utils").logger.getInstance();
2
+
3
+ const EXTENSION_NAME = "graphql-markdown";
4
+
5
+ const loadConfiguration = (
6
+ id = undefined,
7
+ options = undefined,
8
+ { throwOnMissing, throwOnEmpty } = {
9
+ throwOnMissing: false,
10
+ throwOnEmpty: false,
11
+ },
12
+ ) => {
13
+ let GraphQLConfig;
14
+ try {
15
+ // eslint-disable-next-line node/no-missing-require
16
+ GraphQLConfig = require("graphql-config");
17
+ } catch (error) {
18
+ logger.log("Cannot find module 'graphql-config'!");
19
+ return undefined;
20
+ }
21
+
22
+ const config = GraphQLConfig.loadConfigSync({
23
+ ...options,
24
+ extensions: [() => ({ name: EXTENSION_NAME })],
25
+ throwOnMissing,
26
+ throwOnEmpty,
27
+ });
28
+
29
+ try {
30
+ const projectConfig = config.getProject(id).extension(EXTENSION_NAME);
31
+
32
+ if (typeof projectConfig?.schema !== "string") {
33
+ const schema = projectConfig?.schema[0];
34
+ if (typeof schema === "string") {
35
+ projectConfig.schema = schema;
36
+ } else {
37
+ projectConfig.schema = Object.keys(schema)[0];
38
+ projectConfig.loaders = setLoaderOptions(
39
+ projectConfig.loaders,
40
+ Object.values(schema)[0],
41
+ );
42
+ }
43
+ }
44
+ return projectConfig;
45
+ } catch (error) {
46
+ return undefined;
47
+ }
48
+ };
49
+
50
+ const setLoaderOptions = (loaders, options) => {
51
+ for (const loader in loaders) {
52
+ if (typeof loaders[loader] === "string") {
53
+ loaders[loader] = { module: loaders[loader], options };
54
+ } else {
55
+ loaders[loader].options = { ...options, ...loaders[loader].options };
56
+ }
57
+ }
58
+ return loaders;
59
+ };
60
+
61
+ module.exports = { loadConfiguration, EXTENSION_NAME };
package/src/printer.js ADDED
@@ -0,0 +1,24 @@
1
+ const getPrinter = (printerModule, config, options) => {
2
+ let Printer = undefined;
3
+
4
+ if (typeof printerModule !== "string") {
5
+ throw new Error(
6
+ 'Invalid printer module name in "printTypeOptions" settings.',
7
+ );
8
+ }
9
+
10
+ try {
11
+ Printer = require(printerModule);
12
+ } catch (error) {
13
+ throw new Error(
14
+ `Cannot find module '${printerModule}' for @graphql-markdown/core in "printTypeOptions" settings.`,
15
+ );
16
+ }
17
+
18
+ const { schema, baseURL, linkRoot } = config;
19
+ Printer.init(schema, baseURL, linkRoot, { ...options });
20
+
21
+ return Printer;
22
+ };
23
+
24
+ module.exports = { getPrinter };
package/src/renderer.js CHANGED
@@ -9,6 +9,8 @@ const {
9
9
  graphql: { isDeprecated },
10
10
  } = require("@graphql-markdown/utils");
11
11
 
12
+ const logger = require("@graphql-markdown/utils").logger.getInstance();
13
+
12
14
  const { ASSETS_LOCATION } = require("./config");
13
15
  const { schemaSidebar } = require(`${ASSETS_LOCATION}/sidebar.json`);
14
16
 
@@ -122,7 +124,7 @@ module.exports = class Renderer {
122
124
  return undefined;
123
125
  }
124
126
  } catch (error) {
125
- console.log(`An error occurred while processing "${type}"`);
127
+ logger.warn(`An error occurred while processing "${type}"`);
126
128
  return undefined;
127
129
  }
128
130