@graphql-markdown/printer-legacy 1.1.7 → 1.2.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.1.7",
8
+ "version": "1.2.0",
9
9
  "license": "MIT",
10
10
  "main": "src/index.js",
11
11
  "repository": {
@@ -22,7 +22,7 @@
22
22
  "stryker": "stryker run"
23
23
  },
24
24
  "dependencies": {
25
- "@graphql-markdown/utils": "^1.1.4"
25
+ "@graphql-markdown/utils": "^1.2.0"
26
26
  },
27
27
  "directories": {
28
28
  "test": "tests"
package/src/badge.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const {
2
2
  object: { hasProperty },
3
- graphql: { isNonNullType, isListType, getNamedType },
3
+ graphql: { isNonNullType, isListType, isDeprecated, getNamedType },
4
4
  } = require("@graphql-markdown/utils");
5
5
 
6
6
  const { getLinkCategory } = require("./link");
@@ -11,15 +11,15 @@ const getTypeBadges = (type) => {
11
11
 
12
12
  const badges = [];
13
13
 
14
- if (hasProperty(type, "isDeprecated") && type.isDeprecated) {
14
+ if (isDeprecated(type) === true) {
15
15
  badges.push("deprecated");
16
16
  }
17
17
 
18
- if (isNonNullType(rootType)) {
18
+ if (isNonNullType(rootType) === true) {
19
19
  badges.push("non-null");
20
20
  }
21
21
 
22
- if (isListType(rootType)) {
22
+ if (isListType(rootType) === true) {
23
23
  badges.push("list");
24
24
  }
25
25
 
package/src/common.js CHANGED
@@ -1,15 +1,13 @@
1
1
  const {
2
2
  string: { escapeMDX },
3
3
  object: { hasProperty },
4
+ graphql: { isDeprecated },
4
5
  } = require("@graphql-markdown/utils");
5
6
 
6
7
  const { MARKDOWN_EOP, NO_DESCRIPTION_TEXT } = require("./const/strings");
7
8
 
8
9
  const printDeprecation = (type) => {
9
- if (
10
- hasProperty(type, "isDeprecated") === false ||
11
- type.isDeprecated === false
12
- ) {
10
+ if (isDeprecated(type) === false) {
13
11
  return "";
14
12
  }
15
13
 
package/src/const/mdx.js CHANGED
@@ -4,4 +4,26 @@ export const Bullet = () => <><span style={{ fontWeight: 'normal', fontSize: '.5
4
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
5
 
6
6
  export const Badge = (props) => <><span className={'badge badge--' + props.class}>{props.text}</span></>
7
+
8
+ import { useState } from 'react';
9
+
10
+ export const Details = ({ dataOpen, dataClose, children, startOpen = false }) => {
11
+ const [open, setOpen] = useState(startOpen);
12
+ return (
13
+ <details {...(open ? { open: true } : {})} className="details" style={{ border:'none', boxShadow:'none', background:'var(--ifm-background-color)' }}>
14
+ <summary
15
+ onClick={(e) => {
16
+ e.preventDefault();
17
+ setOpen((open) => !open);
18
+ }}
19
+ style={{ listStyle:'none' }}
20
+ >
21
+ {open ? dataOpen : dataClose}
22
+ </summary>
23
+ {open && children}
24
+ </details>
25
+ );
26
+ };
27
+
28
+
7
29
  `;
@@ -0,0 +1,20 @@
1
+ const OPTION_DEPRECATED = {
2
+ DEFAULT: "default",
3
+ GROUP: "group",
4
+ };
5
+
6
+ const DEFAULT_OPTIONS = {
7
+ schema: undefined,
8
+ basePath: "/",
9
+ groups: {},
10
+ parentTypePrefix: true,
11
+ relatedTypeSection: true,
12
+ typeBadges: true,
13
+ skipDocDirective: undefined,
14
+ printDeprecated: OPTION_DEPRECATED.DEFAULT,
15
+ };
16
+
17
+ module.exports = {
18
+ OPTION_DEPRECATED,
19
+ DEFAULT_OPTIONS,
20
+ };
@@ -22,6 +22,8 @@ const MARKDOWN_SOC = `${MARKDOWN_EOL}\`\`\`graphql${MARKDOWN_EOL}`;
22
22
  const MARKDOWN_EOC = `${MARKDOWN_EOL}\`\`\`${MARKDOWN_EOL}`;
23
23
  const FRONT_MATTER = "---";
24
24
 
25
+ const DEPRECATED = "deprecated";
26
+
25
27
  module.exports = {
26
28
  ROOT_TYPE_LOCALE,
27
29
  HEADER_SECTION_LEVEL,
@@ -33,4 +35,5 @@ module.exports = {
33
35
  MARKDOWN_SOC,
34
36
  MARKDOWN_EOC,
35
37
  FRONT_MATTER,
38
+ DEPRECATED,
36
39
  };
@@ -1,8 +1,9 @@
1
1
  const {
2
+ object: { hasProperty },
2
3
  graphql: { getTypeName },
3
4
  } = require("@graphql-markdown/utils");
4
5
 
5
- const { printSection } = require("../section");
6
+ const { printMetadataSection } = require("../section");
6
7
  const { printCodeArguments } = require("../code");
7
8
 
8
9
  const printCodeDirectiveLocation = (type) => {
@@ -25,10 +26,11 @@ const printCodeDirectiveLocation = (type) => {
25
26
  };
26
27
 
27
28
  const printDirectiveMetadata = (type, options) => {
28
- return printSection(type.args, "Arguments", {
29
- ...options,
30
- parentType: type.name,
31
- });
29
+ if (hasProperty(type, "args") === false) {
30
+ return "";
31
+ }
32
+
33
+ return printMetadataSection(type, type.args, "Arguments", options);
32
34
  };
33
35
 
34
36
  const printCodeDirective = (type) => {
@@ -3,14 +3,10 @@ const {
3
3
  } = require("@graphql-markdown/utils");
4
4
 
5
5
  const { MARKDOWN_EOL } = require("../const/strings");
6
-
7
- const { printSection } = require("../section");
6
+ const { printMetadataSection } = require("../section");
8
7
 
9
8
  const printEnumMetadata = (type, options) => {
10
- return printSection(type.getValues(), "Values", {
11
- ...options,
12
- parentType: type.name,
13
- });
9
+ return printMetadataSection(type, type.getValues(), "Values", options);
14
10
  };
15
11
 
16
12
  const printCodeEnum = (type) => {
@@ -3,19 +3,28 @@ const {
3
3
  graphql: { getTypeName, getFields },
4
4
  } = require("@graphql-markdown/utils");
5
5
 
6
- const { printSection } = require("../section");
6
+ const { printSection, printMetadataSection } = require("../section");
7
7
  const { printCodeField } = require("../code");
8
8
  const { MARKDOWN_EOL } = require("../const/strings");
9
9
 
10
- const printObjectMetadata = (type, options) => {
11
- let metadata = printSection(getFields(type), "Fields", {
12
- ...options,
13
- parentType: type.name,
14
- });
15
- if (hasMethod(type, "getInterfaces")) {
16
- metadata += printSection(type.getInterfaces(), "Interfaces", options);
10
+ const printInterfaceMetadata = (type, options) => {
11
+ if (hasMethod(type, "getInterfaces") === false) {
12
+ return "";
17
13
  }
18
- return metadata;
14
+
15
+ return printSection(type.getInterfaces(), "Interfaces", options);
16
+ };
17
+
18
+ const printObjectMetadata = (type, options) => {
19
+ const interfaceMeta = printInterfaceMetadata(type, options);
20
+ const metadata = printMetadataSection(
21
+ type,
22
+ getFields(type),
23
+ "Fields",
24
+ options,
25
+ );
26
+
27
+ return `${metadata}${interfaceMeta}`;
19
28
  };
20
29
 
21
30
  const printCodeType = (type, entity) => {
@@ -2,23 +2,22 @@ const {
2
2
  graphql: { getTypeName },
3
3
  } = require("@graphql-markdown/utils");
4
4
 
5
- const { printSection } = require("../section");
5
+ const { printSection, printMetadataSection } = require("../section");
6
6
  const { printCodeField } = require("../code");
7
7
 
8
- const printOperationMetadata = (type, options) => {
9
- let metadata = printSection(type.args, "Arguments", {
10
- ...options,
11
- parentType: type.name,
12
- parentTypePrefix: true,
13
- });
14
-
8
+ const printOperationType = (type, options) => {
15
9
  const queryType = getTypeName(type.type).replace(/[![\]]*/g, "");
16
- metadata += printSection([options.schema.getType(queryType)], "Type", {
10
+ return printSection([options.schema.getType(queryType)], "Type", {
17
11
  ...options,
18
12
  parentTypePrefix: false,
19
13
  });
14
+ };
15
+
16
+ const printOperationMetadata = (type, options) => {
17
+ const response = printOperationType(type, options);
18
+ const metadata = printMetadataSection(type, type.args, "Arguments", options);
20
19
 
21
- return metadata;
20
+ return `${metadata}${response}`;
22
21
  };
23
22
 
24
23
  const printCodeOperation = printCodeField;
package/src/link.js CHANGED
@@ -17,11 +17,13 @@ const {
17
17
  isListType,
18
18
  isNonNullType,
19
19
  isLeafType,
20
+ isDeprecated,
20
21
  },
21
22
  } = require("@graphql-markdown/utils");
22
23
 
23
24
  const { getGroup } = require("./group");
24
- const { ROOT_TYPE_LOCALE } = require("./const/strings");
25
+ const { ROOT_TYPE_LOCALE, DEPRECATED } = require("./const/strings");
26
+ const { OPTION_DEPRECATED } = require("./const/options");
25
27
 
26
28
  class Link {
27
29
  static getLinkCategory = (graphLQLNamedType) => {
@@ -74,7 +76,7 @@ class Link {
74
76
  return fallback;
75
77
  }
76
78
 
77
- // special case for support relation map
79
+ // special case for relation map
78
80
  if (category === ROOT_TYPE_LOCALE.OPERATION) {
79
81
  if (typeof operation === "undefined") {
80
82
  return fallback;
@@ -83,9 +85,18 @@ class Link {
83
85
  }
84
86
 
85
87
  const text = graphLQLNamedType.name || graphLQLNamedType;
86
- const group = getGroup(type, options.groups);
88
+ const deprecated =
89
+ hasProperty(options, "printDeprecated") &&
90
+ options.printDeprecated === OPTION_DEPRECATED.GROUP &&
91
+ isDeprecated(type)
92
+ ? DEPRECATED
93
+ : "";
94
+ const group = hasProperty(options, "groups")
95
+ ? getGroup(type, options.groups)
96
+ : "";
87
97
  const url = pathUrl.join(
88
98
  options.basePath,
99
+ deprecated,
89
100
  group,
90
101
  category.plural,
91
102
  toSlug(text),
package/src/printer.js CHANGED
@@ -46,15 +46,7 @@ const {
46
46
  } = require("./const/strings");
47
47
  const mdx = require("./const/mdx");
48
48
 
49
- const DEFAULT_OPTIONS = {
50
- schema: undefined,
51
- basePath: "/",
52
- groups: {},
53
- parentTypePrefix: true,
54
- relatedTypeSection: true,
55
- typeBadges: true,
56
- skipDocDirective: undefined,
57
- };
49
+ const { DEFAULT_OPTIONS, OPTION_DEPRECATED } = require("./const/options");
58
50
 
59
51
  class Printer {
60
52
  static options;
@@ -82,6 +74,8 @@ class Printer {
82
74
  relatedTypeSection: printTypeOptions?.relatedTypeSection ?? true,
83
75
  typeBadges: printTypeOptions?.typeBadges ?? true,
84
76
  skipDocDirective: skipDocDirective ?? undefined,
77
+ printDeprecated:
78
+ printTypeOptions?.deprecated ?? OPTION_DEPRECATED.DEFAULT,
85
79
  };
86
80
  };
87
81
 
package/src/section.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const {
2
- graphql: { isParametrizedField, hasDirective },
2
+ graphql: { isParametrizedField, hasDirective, isDeprecated },
3
3
  } = require("@graphql-markdown/utils");
4
4
  const { hasProperty } = require("@graphql-markdown/utils/src/object");
5
5
 
@@ -13,6 +13,7 @@ const {
13
13
  const { printDescription } = require("./common");
14
14
  const { printBadges } = require("./badge");
15
15
  const { printLink, printParentLink } = require("./link");
16
+ const { OPTION_DEPRECATED } = require("./const/options");
16
17
 
17
18
  const sectionLevels = [
18
19
  HEADER_SECTION_LEVEL,
@@ -20,6 +21,46 @@ const sectionLevels = [
20
21
  HEADER_SECTION_ITEM_LEVEL,
21
22
  ];
22
23
 
24
+ const SHOW_DEPRECATED = `<><span className="deprecated">Show deprecated</span></>`;
25
+ const HIDE_DEPRECATED = `<><span className="deprecated">Hide deprecated</span></>`;
26
+
27
+ const printMetadataSection = (type, values, section, options) => {
28
+ switch (options.printDeprecated) {
29
+ case OPTION_DEPRECATED.GROUP: {
30
+ const { fields, deprecated } = values.reduce(
31
+ (res, arg) => {
32
+ isDeprecated(arg) ? res.deprecated.push(arg) : res.fields.push(arg);
33
+ return res;
34
+ },
35
+ { fields: [], deprecated: [] },
36
+ );
37
+
38
+ const meta = printSection(fields, section, {
39
+ ...options,
40
+ parentType: type.name,
41
+ });
42
+ const deprecatedMeta = printSection(deprecated, "", {
43
+ ...options,
44
+ parentType: type.name,
45
+ level: "",
46
+ collapsible: {
47
+ dataOpen: HIDE_DEPRECATED,
48
+ dataClose: SHOW_DEPRECATED,
49
+ },
50
+ });
51
+
52
+ return `${meta}${deprecatedMeta}`;
53
+ }
54
+
55
+ case OPTION_DEPRECATED.DEFAULT:
56
+ default:
57
+ return printSection(values, section, {
58
+ ...options,
59
+ parentType: type.name,
60
+ });
61
+ }
62
+ };
63
+
23
64
  const printSection = (values, section, options) => {
24
65
  const level =
25
66
  hasProperty(options, "level") &&
@@ -33,15 +74,32 @@ const printSection = (values, section, options) => {
33
74
  }
34
75
 
35
76
  const levelPosition = sectionLevels.indexOf(level);
36
- let subLevel = undefined;
37
- if (levelPosition > -1) {
38
- subLevel = sectionLevels[levelPosition + 1];
39
- }
40
77
 
41
- return `${level} ${section}${MARKDOWN_EOP}${printSectionItems(values, {
78
+ const [openSection, closeSection] = (() => {
79
+ if (
80
+ hasProperty(options, "collapsible") &&
81
+ hasProperty(options.collapsible, "dataOpen") &&
82
+ hasProperty(options.collapsible, "dataClose")
83
+ ) {
84
+ return [
85
+ `${MARKDOWN_EOP}<Details dataOpen={${options.collapsible.dataOpen}} dataClose={${options.collapsible.dataClose}}>${MARKDOWN_EOP}`,
86
+ `${MARKDOWN_EOP}</Details>${MARKDOWN_EOP}`,
87
+ ];
88
+ }
89
+ return [MARKDOWN_EOP, MARKDOWN_EOP];
90
+ })();
91
+
92
+ const items = printSectionItems(values, {
42
93
  ...options,
43
- level: subLevel,
44
- })}${MARKDOWN_EOP}`;
94
+ collapsible: undefined, // do not propagate collapsible
95
+ level: levelPosition > -1 ? sectionLevels[levelPosition + 1] : undefined,
96
+ });
97
+
98
+ if (items === "") {
99
+ return ""; // do not print section is no items printed
100
+ }
101
+
102
+ return `${level} ${section}${openSection}${items}${closeSection}`;
45
103
  };
46
104
 
47
105
  const printSectionItems = (values, options) => {
@@ -79,13 +137,17 @@ const printSectionItem = (type, options) => {
79
137
  if (
80
138
  typeof type === "undefined" ||
81
139
  type === null ||
82
- hasDirective(type, options.skipDocDirective)
140
+ (hasProperty(options, "skipDocDirective") &&
141
+ hasDirective(type, options.skipDocDirective) === true)
83
142
  ) {
84
143
  return "";
85
144
  }
86
145
 
87
146
  const typeNameLink = printLink(type, { ...options, withAttributes: false });
88
- const description = printDescription(type, "");
147
+ const description = printDescription(type, "").replaceAll(
148
+ "\n",
149
+ `${MARKDOWN_EOL}> `,
150
+ );
89
151
  const badges = printBadges(type, options);
90
152
  const parentTypeLink = printParentLink(type, options);
91
153
 
@@ -108,4 +170,7 @@ module.exports = {
108
170
  printSection,
109
171
  printSectionItem,
110
172
  printSectionItems,
173
+ printMetadataSection,
174
+ SHOW_DEPRECATED,
175
+ HIDE_DEPRECATED,
111
176
  };