@graphql-markdown/core 1.2.2 → 1.4.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.
@@ -2,7 +2,7 @@
2
2
  "schemaSidebar": [
3
3
  {
4
4
  "type": "autogenerated",
5
- "dirName": "schema"
5
+ "dirName": "."
6
6
  }
7
7
  ]
8
8
  }
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.2.2",
8
+ "version": "1.4.0",
9
9
  "license": "MIT",
10
10
  "main": "src/index.js",
11
11
  "repository": {
@@ -29,11 +29,11 @@
29
29
  "stryker": "stryker run"
30
30
  },
31
31
  "dependencies": {
32
- "@graphql-markdown/utils": "^1.2.1"
32
+ "@graphql-markdown/utils": "^1.4.0"
33
33
  },
34
34
  "peerDependencies": {
35
- "@graphql-markdown/printer-legacy": "^1.2.2",
36
- "@graphql-markdown/diff": "^1.0.8",
35
+ "@graphql-markdown/printer-legacy": "^1.3.0",
36
+ "@graphql-markdown/diff": "^1.0.10",
37
37
  "prettier": "^2.8"
38
38
  },
39
39
  "peerDependenciesMeta": {
package/src/config.js CHANGED
@@ -5,8 +5,6 @@ const {
5
5
  object: { hasProperty },
6
6
  } = require("@graphql-markdown/utils");
7
7
 
8
- const { parseGroupByOption } = require("./group-info");
9
-
10
8
  const PACKAGE_NAME = "@graphql-markdown/docusaurus";
11
9
  const ASSETS_LOCATION = join(__dirname, "../assets/");
12
10
 
@@ -33,6 +31,7 @@ const DEFAULT_OPTIONS = {
33
31
  deprecated: "default",
34
32
  },
35
33
  skipDocDirective: [],
34
+ customDirective: undefined,
36
35
  };
37
36
 
38
37
  function buildConfig(configFileOpts, cliOpts) {
@@ -47,6 +46,7 @@ function buildConfig(configFileOpts, cliOpts) {
47
46
  }
48
47
 
49
48
  const baseURL = cliOpts.base ?? config.baseURL;
49
+ const skipDocDirective = getSkipDocDirectives(cliOpts, config);
50
50
 
51
51
  return {
52
52
  baseURL,
@@ -63,10 +63,41 @@ function buildConfig(configFileOpts, cliOpts) {
63
63
  docOptions: getDocOptions(cliOpts, config.docOptions),
64
64
  printTypeOptions: gePrintTypeOptions(cliOpts, config.printTypeOptions),
65
65
  printer: config.printer,
66
- skipDocDirective: getSkipDocDirectives(cliOpts, config),
66
+ skipDocDirective,
67
+ customDirective: getCustomDirectives(
68
+ config.customDirective,
69
+ skipDocDirective,
70
+ ),
67
71
  };
68
72
  }
69
73
 
74
+ function getCustomDirectives(customDirectiveOptions, skipDocDirective = []) {
75
+ if (
76
+ typeof customDirectiveOptions === "undefined" ||
77
+ (typeof customDirectiveOptions === "object" &&
78
+ Object.keys(customDirectiveOptions).length == 0)
79
+ ) {
80
+ return undefined;
81
+ }
82
+
83
+ for (const [name, option] of Object.entries(customDirectiveOptions)) {
84
+ if (skipDocDirective.includes(name)) {
85
+ delete customDirectiveOptions[name];
86
+ } else if (
87
+ hasProperty(option, "descriptor") === false ||
88
+ typeof option.descriptor !== "function"
89
+ ) {
90
+ throw new Error(
91
+ `Wrong format for plugin custom directive "${name}", it should be {descriptor: (directiveType, constDirectiveType) => String}`,
92
+ );
93
+ }
94
+ }
95
+
96
+ return Object.keys(customDirectiveOptions).length === 0
97
+ ? undefined
98
+ : customDirectiveOptions;
99
+ }
100
+
70
101
  function getDiffMethod(diff, force) {
71
102
  return force ? "FORCE" : diff;
72
103
  }
@@ -103,9 +134,10 @@ function getSkipDocDirectives(cliOpts = {}, configFileOpts = {}) {
103
134
  );
104
135
 
105
136
  if (
106
- (hasProperty(configFileOpts, "printTypeOptions") &&
137
+ (hasProperty(configFileOpts, "printTypeOptions") === true &&
107
138
  configFileOpts.printTypeOptions.deprecated === "skip") ||
108
- (hasProperty(cliOpts, "deprecated") && cliOpts.deprecated === "skip")
139
+ (hasProperty(cliOpts, "deprecated") === true &&
140
+ cliOpts.deprecated === "skip")
109
141
  ) {
110
142
  skipDirectives.push("deprecated");
111
143
  }
@@ -129,10 +161,31 @@ function getSkipDocDirective(option) {
129
161
  return parsedOption.groups.directive;
130
162
  }
131
163
 
164
+ function parseGroupByOption(groupOptions) {
165
+ const DEFAULT_GROUP = "Miscellaneous";
166
+ const OPTION_REGEX =
167
+ /^@(?<directive>\w+)\((?<field>\w+)(?:\|=(?<fallback>\w+))?\)/;
168
+
169
+ if (typeof groupOptions !== "string") {
170
+ return undefined;
171
+ }
172
+
173
+ const parsedOptions = OPTION_REGEX.exec(groupOptions);
174
+
175
+ if (typeof parsedOptions === "undefined" || parsedOptions == null) {
176
+ throw new Error(`Invalid "${groupOptions}"`);
177
+ }
178
+
179
+ const { directive, field, fallback = DEFAULT_GROUP } = parsedOptions.groups;
180
+ return { directive, field, fallback };
181
+ }
182
+
132
183
  module.exports = {
133
184
  buildConfig,
134
185
  getSkipDocDirectives,
135
186
  getSkipDocDirective,
187
+ parseGroupByOption,
188
+ getCustomDirectives,
136
189
  DEFAULT_OPTIONS,
137
190
  ASSETS_LOCATION,
138
191
  };
package/src/generator.js CHANGED
@@ -1,7 +1,8 @@
1
1
  const {
2
2
  graphql: { getSchemaMap, loadSchema, getDocumentLoaders },
3
+ group: { getGroups },
4
+ directive: { getCustomDirectives },
3
5
  } = require("@graphql-markdown/utils");
4
- const { getGroups } = require("./group-info");
5
6
  const Renderer = require("./renderer");
6
7
 
7
8
  const NS_PER_SEC = 1e9;
@@ -34,15 +35,7 @@ const hasChanges = async (
34
35
  return true;
35
36
  };
36
37
 
37
- const getPrinter = (
38
- schema,
39
- baseURL,
40
- linkRoot,
41
- groups,
42
- printTypeOptions,
43
- printerModule,
44
- skipDocDirective,
45
- ) => {
38
+ const getPrinter = (printerModule, config, options) => {
46
39
  let Printer = undefined;
47
40
 
48
41
  if (typeof printerModule !== "string") {
@@ -59,11 +52,8 @@ const getPrinter = (
59
52
  );
60
53
  }
61
54
 
62
- Printer.init(schema, baseURL, linkRoot, {
63
- groups,
64
- printTypeOptions,
65
- skipDocDirective,
66
- });
55
+ const { schema, baseURL, linkRoot } = config;
56
+ Printer.init(schema, baseURL, linkRoot, { ...options });
67
57
 
68
58
  return Printer;
69
59
  };
@@ -83,6 +73,7 @@ const generateDocFromSchema = async ({
83
73
  printTypeOptions,
84
74
  printer: printerModule,
85
75
  skipDocDirective,
76
+ customDirective,
86
77
  }) => {
87
78
  const start = process.hrtime.bigint();
88
79
 
@@ -95,15 +86,26 @@ const generateDocFromSchema = async ({
95
86
  }
96
87
 
97
88
  const rootTypes = getSchemaMap(schema);
89
+ const customDirectives = getCustomDirectives(rootTypes, customDirective);
98
90
  const groups = new getGroups(rootTypes, groupByDirective);
99
91
  const printer = getPrinter(
100
- schema,
101
- baseURL,
102
- linkRoot,
103
- groups,
104
- printTypeOptions,
92
+ // module mandatory
105
93
  printerModule,
106
- skipDocDirective,
94
+
95
+ // config mandatory
96
+ {
97
+ schema,
98
+ baseURL,
99
+ linkRoot,
100
+ },
101
+
102
+ // options
103
+ {
104
+ groups,
105
+ printTypeOptions,
106
+ skipDocDirective,
107
+ customDirectives,
108
+ },
107
109
  );
108
110
  const renderer = new Renderer(printer, outputDir, baseURL, groups, prettify, {
109
111
  ...docOptions,
package/src/index.js CHANGED
@@ -2,6 +2,5 @@
2
2
 
3
3
  module.exports = {
4
4
  generateDocFromSchema: require("./generator").generateDocFromSchema,
5
- groupInfo: require("./group-info"),
6
5
  config: require("./config"),
7
6
  };
package/src/renderer.js CHANGED
@@ -74,9 +74,15 @@ module.exports = class Renderer {
74
74
  );
75
75
  }
76
76
 
77
- if (hasProperty(this.group, name)) {
78
- dirPath = path.join(dirPath, toSlug(this.group[name]));
79
- await this.generateCategoryMetafile(this.group[name], dirPath);
77
+ if (
78
+ hasProperty(this.group, rootTypeName) &&
79
+ hasProperty(this.group[rootTypeName], name)
80
+ ) {
81
+ dirPath = path.join(dirPath, toSlug(this.group[rootTypeName][name]));
82
+ await this.generateCategoryMetafile(
83
+ this.group[rootTypeName][name],
84
+ dirPath,
85
+ );
80
86
  }
81
87
 
82
88
  dirPath = path.join(dirPath, toSlug(rootTypeName));
package/src/group-info.js DELETED
@@ -1,65 +0,0 @@
1
- const DEFAULT_GROUP = "Miscellaneous";
2
- const OPTION_REGEX =
3
- /^@(?<directive>\w+)\((?<field>\w+)(?:\|=(?<fallback>\w+))?\)/;
4
-
5
- function parseGroupByOption(groupOptions) {
6
- if (typeof groupOptions !== "string") {
7
- return undefined;
8
- }
9
-
10
- const parsedOptions = OPTION_REGEX.exec(groupOptions);
11
-
12
- if (typeof parsedOptions === "undefined" || parsedOptions == null) {
13
- throw new Error(`Invalid "${groupOptions}"`);
14
- }
15
-
16
- const { directive, field, fallback = DEFAULT_GROUP } = parsedOptions.groups;
17
- return { directive, field, fallback };
18
- }
19
-
20
- function getGroups(rootTypes, groupByDirective) {
21
- let groups = {};
22
-
23
- if (typeof groupByDirective === "undefined" || groupByDirective == null) {
24
- return undefined;
25
- }
26
-
27
- Object.keys(rootTypes).forEach((typeName) => {
28
- let rootType = rootTypes[typeName];
29
- if (typeof rootType != "undefined" && rootType != null) {
30
- Object.keys(rootType).forEach((type) => {
31
- groups[type] = getGroupName(rootType[type], groupByDirective);
32
- });
33
- }
34
- });
35
-
36
- return groups;
37
- }
38
-
39
- function getGroupName(type, groupByDirective) {
40
- let group = groupByDirective.fallback; // default value is fallback, and it will be only overridden if a group is found
41
-
42
- if (typeof type.astNode === "undefined" || type.astNode == null) {
43
- return group;
44
- }
45
-
46
- const allDirectives = type.astNode.directives;
47
-
48
- if (!Array.isArray(allDirectives)) {
49
- return group;
50
- }
51
-
52
- for (const directive of allDirectives) {
53
- if (directive.name.value !== groupByDirective.directive) {
54
- continue;
55
- }
56
- const field = directive.arguments.find(
57
- ({ name }) => name.value === groupByDirective.field,
58
- );
59
- return field.value.value;
60
- }
61
-
62
- return group;
63
- }
64
-
65
- module.exports = { getGroupName, getGroups, parseGroupByOption };