@graphql-markdown/printer-legacy 1.1.7 → 1.2.1

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.1",
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/code.js CHANGED
@@ -1,14 +1,28 @@
1
1
  const {
2
2
  object: { hasProperty },
3
- graphql: { getDefaultValue, getTypeName },
3
+ graphql: { getDefaultValue, getTypeName, isDeprecated, hasDirective },
4
4
  } = require("@graphql-markdown/utils");
5
5
 
6
- const { MARKDOWN_EOL } = require("./const/strings");
6
+ const { MARKDOWN_EOL, DEPRECATED } = require("./const/strings");
7
+ const { OPTION_DEPRECATED } = require("./const/options");
8
+
9
+ const printCodeField = (type, options = {}) => {
10
+ const skipDirective =
11
+ hasProperty(options, "skipDocDirective") &&
12
+ hasDirective(type, options.skipDocDirective) === true;
13
+ const skipDeprecated =
14
+ hasProperty(options, "printDeprecated") &&
15
+ options.printDeprecated === OPTION_DEPRECATED.SKIP &&
16
+ isDeprecated(type) === true;
17
+ if (skipDirective === true || skipDeprecated === true) {
18
+ return "";
19
+ }
7
20
 
8
- const printCodeField = (type) => {
9
21
  let code = `${getTypeName(type)}`;
10
22
  code += printCodeArguments(type);
11
- code += `: ${getTypeName(type.type)}${MARKDOWN_EOL}`;
23
+ code += `: ${getTypeName(type.type)}`;
24
+ code += isDeprecated(type) ? ` @${DEPRECATED}` : "";
25
+ code += MARKDOWN_EOL;
12
26
 
13
27
  return code;
14
28
  };
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,21 @@
1
+ const OPTION_DEPRECATED = {
2
+ DEFAULT: "default",
3
+ GROUP: "group",
4
+ SKIP: "skip",
5
+ };
6
+
7
+ const DEFAULT_OPTIONS = {
8
+ schema: undefined,
9
+ basePath: "/",
10
+ groups: {},
11
+ parentTypePrefix: true,
12
+ relatedTypeSection: true,
13
+ typeBadges: true,
14
+ skipDocDirective: undefined,
15
+ printDeprecated: OPTION_DEPRECATED.DEFAULT,
16
+ };
17
+
18
+ module.exports = {
19
+ OPTION_DEPRECATED,
20
+ DEFAULT_OPTIONS,
21
+ };
@@ -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) => {
@@ -1,19 +1,17 @@
1
1
  const {
2
- graphql: { isEnumType, getTypeName },
2
+ object: { hasProperty },
3
+ graphql: { isEnumType, getTypeName, isDeprecated, hasDirective },
3
4
  } = require("@graphql-markdown/utils");
4
5
 
5
- const { MARKDOWN_EOL } = require("../const/strings");
6
-
7
- const { printSection } = require("../section");
6
+ const { MARKDOWN_EOL, DEPRECATED } = require("../const/strings");
7
+ const { OPTION_DEPRECATED } = require("../const/options");
8
+ const { printMetadataSection } = require("../section");
8
9
 
9
10
  const printEnumMetadata = (type, options) => {
10
- return printSection(type.getValues(), "Values", {
11
- ...options,
12
- parentType: type.name,
13
- });
11
+ return printMetadataSection(type, type.getValues(), "Values", options);
14
12
  };
15
13
 
16
- const printCodeEnum = (type) => {
14
+ const printCodeEnum = (type, options) => {
17
15
  if (!isEnumType(type)) {
18
16
  return "";
19
17
  }
@@ -21,7 +19,22 @@ const printCodeEnum = (type) => {
21
19
  let code = `enum ${getTypeName(type)} {${MARKDOWN_EOL}`;
22
20
  code += type
23
21
  .getValues()
24
- .map((v) => ` ${getTypeName(v)}`)
22
+ .map((value) => {
23
+ const skipDirective =
24
+ hasProperty(options, "skipDocDirective") &&
25
+ hasDirective(value, options.skipDocDirective) === true;
26
+ const skipDeprecated =
27
+ hasProperty(options, "printDeprecated") &&
28
+ options.printDeprecated === OPTION_DEPRECATED.SKIP &&
29
+ isDeprecated(value) === true;
30
+ if (skipDirective === true || skipDeprecated === true) {
31
+ return "";
32
+ }
33
+ const v = getTypeName(value);
34
+ const d = isDeprecated(value) === true ? ` @${DEPRECATED}` : "";
35
+ return ` ${v}${d}`;
36
+ })
37
+ .filter((value) => value.length > 0)
25
38
  .join(MARKDOWN_EOL);
26
39
  code += `${MARKDOWN_EOL}}`;
27
40
 
@@ -2,7 +2,7 @@ const { printObjectMetadata, printCodeType } = require("./object");
2
2
 
3
3
  const printInputMetadata = printObjectMetadata;
4
4
 
5
- const printCodeInput = (type) => printCodeType(type, "input");
5
+ const printCodeInput = (type, options) => printCodeType(type, "input", options);
6
6
 
7
7
  module.exports = {
8
8
  printInputMetadata,
@@ -3,22 +3,31 @@ 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
- const printCodeType = (type, entity) => {
30
+ const printCodeType = (type, entity, options) => {
22
31
  const name = getTypeName(type);
23
32
  const extendsInterface =
24
33
  hasMethod(type, "getInterfaces") && type.getInterfaces().length > 0
@@ -28,13 +37,17 @@ const printCodeType = (type, entity) => {
28
37
  .join(", ")}`
29
38
  : "";
30
39
  const typeFields = getFields(type)
31
- .map((v) => ` ${printCodeField(v)}`)
40
+ .map((field) => {
41
+ const f = printCodeField(field, options);
42
+ return f.length > 0 ? ` ${f}` : "";
43
+ })
44
+ .filter((field) => field.length > 0)
32
45
  .join("");
33
46
 
34
47
  return `${entity} ${name}${extendsInterface} {${MARKDOWN_EOL}${typeFields}}`;
35
48
  };
36
49
 
37
- const printCodeObject = (type) => printCodeType(type, "type");
50
+ const printCodeObject = (type, options) => printCodeType(type, "type", options);
38
51
 
39
52
  module.exports = {
40
53
  printObjectMetadata,
@@ -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
 
@@ -109,33 +103,33 @@ class Printer {
109
103
 
110
104
  static printDescription = printDescription;
111
105
 
112
- static printCode = (type) => {
106
+ static printCode = (type, options) => {
113
107
  let code = "";
114
108
 
115
109
  switch (true) {
116
110
  case isOperation(type):
117
- code += printCodeOperation(type);
111
+ code += printCodeOperation(type, options);
118
112
  break;
119
113
  case isEnumType(type):
120
- code += printCodeEnum(type);
114
+ code += printCodeEnum(type, options);
121
115
  break;
122
- case isUnionType(type):
123
- code += printCodeUnion(type);
116
+ case isUnionType(type, options):
117
+ code += printCodeUnion(type, options);
124
118
  break;
125
- case isInterfaceType(type):
126
- code += printCodeInterface(type);
119
+ case isInterfaceType(type, options):
120
+ code += printCodeInterface(type, options);
127
121
  break;
128
122
  case isObjectType(type):
129
- code += printCodeObject(type);
123
+ code += printCodeObject(type, options);
130
124
  break;
131
125
  case isInputType(type):
132
- code += printCodeInput(type);
126
+ code += printCodeInput(type, options);
133
127
  break;
134
128
  case isScalarType(type):
135
- code += printCodeScalar(type);
129
+ code += printCodeScalar(type, options);
136
130
  break;
137
131
  case isDirectiveType(type):
138
- code += printCodeDirective(type);
132
+ code += printCodeDirective(type, options);
139
133
  break;
140
134
  default:
141
135
  code += `"${getTypeName(type)}" not supported`;
@@ -198,7 +192,7 @@ class Printer {
198
192
  header: options,
199
193
  });
200
194
  const description = Printer.printDescription(type);
201
- const code = Printer.printCode(type);
195
+ const code = Printer.printCode(type, printTypeOptions);
202
196
  const metadata = Printer.printTypeMetadata(type, printTypeOptions);
203
197
  const relations = Printer.printRelations(type, printTypeOptions);
204
198
 
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) => {
@@ -76,16 +134,28 @@ const printSectionItem = (type, options) => {
76
134
  ? options.level
77
135
  : HEADER_SECTION_SUB_LEVEL;
78
136
 
137
+ const skipDirective =
138
+ hasProperty(options, "skipDocDirective") &&
139
+ hasDirective(type, options.skipDocDirective) === true;
140
+ const skipDeprecated =
141
+ hasProperty(options, "printDeprecated") &&
142
+ options.printDeprecated === OPTION_DEPRECATED.SKIP &&
143
+ isDeprecated(type) === true;
144
+
79
145
  if (
80
146
  typeof type === "undefined" ||
81
147
  type === null ||
82
- hasDirective(type, options.skipDocDirective)
148
+ skipDirective === true ||
149
+ skipDeprecated === true
83
150
  ) {
84
151
  return "";
85
152
  }
86
153
 
87
154
  const typeNameLink = printLink(type, { ...options, withAttributes: false });
88
- const description = printDescription(type, "");
155
+ const description = printDescription(type, "").replaceAll(
156
+ "\n",
157
+ `${MARKDOWN_EOL}> `,
158
+ );
89
159
  const badges = printBadges(type, options);
90
160
  const parentTypeLink = printParentLink(type, options);
91
161
 
@@ -108,4 +178,7 @@ module.exports = {
108
178
  printSection,
109
179
  printSectionItem,
110
180
  printSectionItems,
181
+ printMetadataSection,
182
+ SHOW_DEPRECATED,
183
+ HIDE_DEPRECATED,
111
184
  };