@graphql-markdown/printer-legacy 1.7.0 → 1.8.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/README.md CHANGED
@@ -1,4 +1,7 @@
1
1
  # @graphql-markdown/printer-legacy
2
2
 
3
- [![Latest Version](https://img.shields.io/npm/v/@graphql-markdown/printer-legacy?style=flat-square)](https://www.npmjs.com/package/@graphql-markdown/printer-legacy)
4
- [![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)
3
+ [![Latest Version](https://img.shields.io/npm/v/@graphql-markdown/printer-legacy?style=flat)](https://www.npmjs.com/package/@graphql-markdown/printer-legacy)
4
+ [![GitHub License](https://img.shields.io/github/license/graphql-markdown/graphql-markdown?style=flat)](https://raw.githubusercontent.com/graphql-markdown/graphql-markdown/main/LICENSE)
5
+ [![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=graphql-markdown_printer-legacy&metric=sqale_index)](https://sonarcloud.io/summary/new_code?id=graphql-markdown_printer-legacy)
6
+ [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=graphql-markdown_printer-legacy&metric=coverage)](https://sonarcloud.io/summary/new_code?id=graphql-markdown_printer-legacy)
7
+ [![Mutation Score](https://img.shields.io/endpoint?label=mutation%20score&style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fgraphql-markdown%2Fgraphql-markdown%2Fmain%3Fmodule%3Dprinter-legacy)](https://dashboard.stryker-mutator.io/reports/github.com/graphql-markdown/graphql-markdown/main?module=printer-legacy)
@@ -11,6 +11,7 @@ var SectionLevels;
11
11
  exports.PRINT_TYPE_DEFAULT_OPTIONS = {
12
12
  codeSection: true,
13
13
  deprecated: "default",
14
+ exampleSection: false,
14
15
  metatags: [],
15
16
  parentTypePrefix: true,
16
17
  relatedTypeSection: true,
@@ -0,0 +1,3 @@
1
+ import type { Maybe, PrintTypeOptions, TypeDirectiveExample } from "@graphql-markdown/types";
2
+ export declare const getDirectiveExampleOption: (options: PrintTypeOptions) => Maybe<TypeDirectiveExample>;
3
+ export declare const printExample: (type: unknown, options: PrintTypeOptions) => Maybe<string>;
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.printExample = exports.getDirectiveExampleOption = void 0;
4
+ const graphql_1 = require("@graphql-markdown/graphql");
5
+ const utils_1 = require("@graphql-markdown/utils");
6
+ const common_1 = require("./common");
7
+ const getDirectiveExampleOption = (options) => {
8
+ let directiveName = "example", argName = "value", parserFunc = undefined;
9
+ if (options.exampleSection && typeof options.exampleSection === "object") {
10
+ if ("directive" in options.exampleSection &&
11
+ options.exampleSection.directive) {
12
+ directiveName = options.exampleSection.directive;
13
+ }
14
+ if ("field" in options.exampleSection && options.exampleSection.field) {
15
+ argName = options.exampleSection.field;
16
+ }
17
+ if ("parser" in options.exampleSection && options.exampleSection.parser) {
18
+ parserFunc = options.exampleSection.parser;
19
+ }
20
+ }
21
+ const directive = options.schema && !(0, utils_1.isEmpty)(options.schema)
22
+ ? options.schema.getDirective(directiveName)
23
+ : undefined;
24
+ if (!directive) {
25
+ return undefined;
26
+ }
27
+ return {
28
+ directive,
29
+ field: argName,
30
+ parser: parserFunc,
31
+ };
32
+ };
33
+ exports.getDirectiveExampleOption = getDirectiveExampleOption;
34
+ const parseTypeFields = (type, options) => {
35
+ const directiveExample = (0, exports.getDirectiveExampleOption)(options);
36
+ if (!directiveExample) {
37
+ return undefined;
38
+ }
39
+ const fields = (0, graphql_1.getFields)(type);
40
+ if (fields.length === 0) {
41
+ return undefined;
42
+ }
43
+ let example = {};
44
+ fields
45
+ .filter((field) => {
46
+ return (0, common_1.hasPrintableDirective)(field, options);
47
+ })
48
+ .forEach((field) => {
49
+ const fieldType = (0, graphql_1.hasDirective)(field, [directiveExample.directive])
50
+ ? field
51
+ : field.type;
52
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
53
+ const value = parseExampleDirective(fieldType, options);
54
+ example = {
55
+ ...example,
56
+ [field.name]: (0, graphql_1.isListType)((0, graphql_1.getNullableType)(fieldType))
57
+ ? [value]
58
+ : value,
59
+ };
60
+ });
61
+ return example;
62
+ };
63
+ const parseExampleValue = (value,
64
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
65
+ type) => {
66
+ if (!value || typeof value !== "string") {
67
+ return undefined;
68
+ }
69
+ try {
70
+ return JSON.parse(value);
71
+ }
72
+ catch (err) {
73
+ if ((0, graphql_1.isOperation)(type)) {
74
+ try {
75
+ return (0, graphql_1.parse)(value, { noLocation: true });
76
+ }
77
+ catch (err) {
78
+ /* empty */
79
+ }
80
+ }
81
+ }
82
+ return value;
83
+ };
84
+ const parseExampleDirective = (type, options) => {
85
+ const directiveExample = (0, exports.getDirectiveExampleOption)(options);
86
+ if (!directiveExample || !(0, common_1.hasPrintableDirective)(type, options)) {
87
+ return undefined;
88
+ }
89
+ const parser = typeof directiveExample.parser === "function"
90
+ ? directiveExample.parser
91
+ : parseExampleValue;
92
+ const namedType = (0, graphql_1.isType)(type) ? (0, graphql_1.getNamedType)(type) : type;
93
+ if (!namedType) {
94
+ return undefined;
95
+ }
96
+ try {
97
+ const arg = (0, graphql_1.getTypeDirectiveArgValue)(directiveExample.directive, namedType, directiveExample.field);
98
+ return parser(arg, namedType);
99
+ }
100
+ catch (err) {
101
+ if (err instanceof graphql_1.IntrospectionError) {
102
+ return (0, graphql_1.isType)(namedType)
103
+ ? parseTypeFields(namedType, options)
104
+ : undefined;
105
+ }
106
+ throw err;
107
+ }
108
+ };
109
+ const printExample = (type, options) => {
110
+ if (!(0, graphql_1.isType)(type) && !(0, graphql_1.isOperation)(type)) {
111
+ return undefined;
112
+ }
113
+ const example = parseExampleDirective(type, options);
114
+ if (!example) {
115
+ return undefined;
116
+ }
117
+ if ((0, graphql_1.isOperation)(type) && (0, graphql_1.isNode)(example)) {
118
+ return (0, graphql_1.print)(example);
119
+ }
120
+ return JSON.stringify(example, undefined, 2);
121
+ };
122
+ exports.printExample = printExample;
@@ -8,7 +8,7 @@ const printSpecification = (type) => {
8
8
  if (typeof type !== "object" ||
9
9
  type === null ||
10
10
  !("specifiedByURL" in type) ||
11
- !type.specifiedByURL) {
11
+ typeof type.specifiedByURL !== "string") {
12
12
  return "";
13
13
  }
14
14
  const url = type.specifiedByURL;
package/dist/printer.d.ts CHANGED
@@ -15,6 +15,7 @@ export declare class Printer implements IPrinter {
15
15
  }): void;
16
16
  static readonly printHeader: (id: string, title: string, options: PrintTypeOptions) => string;
17
17
  static readonly printCode: (type: unknown, options: PrintTypeOptions) => string;
18
+ static readonly printExample: (type: unknown, options: PrintTypeOptions) => string;
18
19
  static readonly printTypeMetadata: (type: unknown, options: PrintTypeOptions) => MDXString | string;
19
20
  static readonly printRelations: (type: unknown, options: PrintTypeOptions) => MDXString | string;
20
21
  static readonly printMetaTags: (_type: unknown, { metatags }: PrintTypeOptions) => MDXString | string;
package/dist/printer.js CHANGED
@@ -11,6 +11,7 @@ const graphql_2 = require("./graphql");
11
11
  const strings_1 = require("./const/strings");
12
12
  const mdx_1 = require("./const/mdx");
13
13
  const options_1 = require("./const/options");
14
+ const example_1 = require("./example");
14
15
  class Printer {
15
16
  static options;
16
17
  static printDescription = common_1.printDescription;
@@ -19,10 +20,6 @@ class Printer {
19
20
  static init(schema, baseURL = "schema", linkRoot = "/", { customDirectives, groups, onlyDocDirectives, printTypeOptions, skipDocDirectives, metatags, } = {
20
21
  customDirectives: undefined,
21
22
  groups: undefined,
22
- onlyDocDirectives: [],
23
- printTypeOptions: options_1.PRINT_TYPE_DEFAULT_OPTIONS,
24
- skipDocDirectives: [],
25
- metatags: [],
26
23
  }) {
27
24
  if (typeof Printer.options !== "undefined") {
28
25
  return;
@@ -32,6 +29,8 @@ class Printer {
32
29
  basePath: utils_1.pathUrl.join(linkRoot ?? "", baseURL ?? ""),
33
30
  codeSection: printTypeOptions?.codeSection ?? options_1.PRINT_TYPE_DEFAULT_OPTIONS.codeSection,
34
31
  customDirectives,
32
+ exampleSection: printTypeOptions?.exampleSection ??
33
+ options_1.PRINT_TYPE_DEFAULT_OPTIONS.exampleSection,
35
34
  groups,
36
35
  parentTypePrefix: printTypeOptions?.parentTypePrefix ??
37
36
  options_1.PRINT_TYPE_DEFAULT_OPTIONS.parentTypePrefix,
@@ -86,6 +85,18 @@ class Printer {
86
85
  }
87
86
  return strings_1.MARKDOWN_SOC + code.trim() + strings_1.MARKDOWN_EOC;
88
87
  };
88
+ static printExample = (type, options) => {
89
+ if (typeof options.exampleSection === "undefined" ||
90
+ options.exampleSection === null ||
91
+ options.exampleSection === false) {
92
+ return "";
93
+ }
94
+ const example = (0, example_1.printExample)(type, options);
95
+ if (!example) {
96
+ return "";
97
+ }
98
+ return `${options_1.SectionLevels.LEVEL_3} Example${strings_1.MARKDOWN_EOP}${strings_1.MARKDOWN_SOC}${example}${strings_1.MARKDOWN_EOC}${strings_1.MARKDOWN_EOP}`;
99
+ };
89
100
  static printTypeMetadata = (type, options) => {
90
101
  switch (true) {
91
102
  case (0, graphql_1.isScalarType)(type):
@@ -143,6 +154,7 @@ class Printer {
143
154
  const tags = Printer.printCustomTags(type, printTypeOptions);
144
155
  const metadata = Printer.printTypeMetadata(type, printTypeOptions);
145
156
  const relations = Printer.printRelations(type, printTypeOptions);
157
+ const example = Printer.printExample(type, printTypeOptions);
146
158
  return [
147
159
  header,
148
160
  metatags,
@@ -152,6 +164,7 @@ class Printer {
152
164
  code,
153
165
  customDirectives,
154
166
  metadata,
167
+ example,
155
168
  relations,
156
169
  ].join(strings_1.MARKDOWN_EOP);
157
170
  };
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@graphql-markdown/printer-legacy",
3
- "homepage": "https://graphql-markdown.github.io",
3
+ "homepage": "https://graphql-markdown.dev",
4
4
  "description": "GraphQL-Markdown legacy Docusaurus printer.",
5
5
  "bugs": {
6
6
  "url": "https://github.com/graphql-markdown/graphql-markdown/issues"
7
7
  },
8
- "version": "1.7.0",
8
+ "version": "1.8.1",
9
9
  "license": "MIT",
10
10
  "repository": {
11
11
  "type": "git",
@@ -55,12 +55,12 @@
55
55
  "clean": "rm -rf ./dist"
56
56
  },
57
57
  "dependencies": {
58
- "@docusaurus/utils": ">=2.0.0",
59
- "@graphql-markdown/graphql": "^1.1.0",
60
- "@graphql-markdown/utils": "^1.6.3"
58
+ "@docusaurus/utils": ">=3.2.0",
59
+ "@graphql-markdown/graphql": "^1.1.2",
60
+ "@graphql-markdown/utils": "^1.6.4"
61
61
  },
62
62
  "devDependencies": {
63
- "@graphql-markdown/types": "^1.2.0"
63
+ "@graphql-markdown/types": "^1.3.1"
64
64
  },
65
65
  "directories": {
66
66
  "test": "tests"