@graphql-markdown/printer-legacy 1.7.0 → 1.8.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.
@@ -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,2 @@
1
+ import type { Maybe, TypeDirectiveExample } from "@graphql-markdown/types";
2
+ export declare const printExample: (type: unknown, directiveExample: TypeDirectiveExample) => Maybe<string>;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.printExample = void 0;
4
+ const graphql_1 = require("@graphql-markdown/graphql");
5
+ const parseTypeFields = (type, directiveExample) => {
6
+ const fields = (0, graphql_1.getFields)(type);
7
+ if (fields.length === 0) {
8
+ return undefined;
9
+ }
10
+ let example = {};
11
+ fields.forEach((field) => {
12
+ const fieldType = (0, graphql_1.hasDirective)(field, [directiveExample.directive])
13
+ ? field
14
+ : field.type;
15
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
16
+ const value = parseExampleDirective(fieldType, directiveExample);
17
+ example = {
18
+ ...example,
19
+ [field.name]: (0, graphql_1.isListType)((0, graphql_1.getNullableType)(fieldType))
20
+ ? [value]
21
+ : value,
22
+ };
23
+ });
24
+ return example;
25
+ };
26
+ const parseExampleValue = (value,
27
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
28
+ type) => {
29
+ if (!value || typeof value !== "string") {
30
+ return undefined;
31
+ }
32
+ try {
33
+ return JSON.parse(value);
34
+ }
35
+ catch (err) {
36
+ if ((0, graphql_1.isOperation)(type)) {
37
+ try {
38
+ return (0, graphql_1.parse)(value, { noLocation: true });
39
+ }
40
+ catch (err) {
41
+ /* empty */
42
+ }
43
+ }
44
+ }
45
+ return value;
46
+ };
47
+ const parseExampleDirective = (type, directiveExample) => {
48
+ const parser = typeof directiveExample.parser === "function"
49
+ ? directiveExample.parser
50
+ : parseExampleValue;
51
+ const namedType = (0, graphql_1.isType)(type) ? (0, graphql_1.getNamedType)(type) : type;
52
+ if (!namedType) {
53
+ return undefined;
54
+ }
55
+ try {
56
+ const arg = (0, graphql_1.getTypeDirectiveArgValue)(directiveExample.directive, namedType, directiveExample.field);
57
+ return parser(arg, namedType);
58
+ }
59
+ catch (err) {
60
+ if (err instanceof graphql_1.IntrospectionError) {
61
+ return (0, graphql_1.isType)(namedType)
62
+ ? parseTypeFields(namedType, directiveExample)
63
+ : undefined;
64
+ }
65
+ throw err;
66
+ }
67
+ };
68
+ const printExample = (type, directiveExample) => {
69
+ if (!(0, graphql_1.isType)(type) && !(0, graphql_1.isOperation)(type)) {
70
+ return undefined;
71
+ }
72
+ const example = parseExampleDirective(type, directiveExample);
73
+ if (!example) {
74
+ return undefined;
75
+ }
76
+ if ((0, graphql_1.isOperation)(type) && (0, graphql_1.isNode)(example)) {
77
+ return (0, graphql_1.print)(example);
78
+ }
79
+ return JSON.stringify(example, undefined, 2);
80
+ };
81
+ 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,32 @@ 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
+ let directiveName = "example", argName = "value";
95
+ if (typeof options.exampleSection === "object") {
96
+ if ("directive" in options.exampleSection &&
97
+ options.exampleSection.directive) {
98
+ directiveName = options.exampleSection.directive;
99
+ }
100
+ if ("field" in options.exampleSection && options.exampleSection.field) {
101
+ argName = options.exampleSection.field;
102
+ }
103
+ }
104
+ const directive = options.schema?.getDirective(directiveName);
105
+ if (!directive) {
106
+ return "";
107
+ }
108
+ const example = (0, example_1.printExample)(type, { directive, field: argName });
109
+ if (!example) {
110
+ return "";
111
+ }
112
+ return `${options_1.SectionLevels.LEVEL_3} Example${strings_1.MARKDOWN_EOP}${strings_1.MARKDOWN_SOC}${example}${strings_1.MARKDOWN_EOC}${strings_1.MARKDOWN_EOP}`;
113
+ };
89
114
  static printTypeMetadata = (type, options) => {
90
115
  switch (true) {
91
116
  case (0, graphql_1.isScalarType)(type):
@@ -143,6 +168,7 @@ class Printer {
143
168
  const tags = Printer.printCustomTags(type, printTypeOptions);
144
169
  const metadata = Printer.printTypeMetadata(type, printTypeOptions);
145
170
  const relations = Printer.printRelations(type, printTypeOptions);
171
+ const example = Printer.printExample(type, printTypeOptions);
146
172
  return [
147
173
  header,
148
174
  metatags,
@@ -152,6 +178,7 @@ class Printer {
152
178
  code,
153
179
  customDirectives,
154
180
  metadata,
181
+ example,
155
182
  relations,
156
183
  ].join(strings_1.MARKDOWN_EOP);
157
184
  };
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.7.0",
8
+ "version": "1.8.0",
9
9
  "license": "MIT",
10
10
  "repository": {
11
11
  "type": "git",
@@ -56,11 +56,11 @@
56
56
  },
57
57
  "dependencies": {
58
58
  "@docusaurus/utils": ">=2.0.0",
59
- "@graphql-markdown/graphql": "^1.1.0",
59
+ "@graphql-markdown/graphql": "^1.1.1",
60
60
  "@graphql-markdown/utils": "^1.6.3"
61
61
  },
62
62
  "devDependencies": {
63
- "@graphql-markdown/types": "^1.2.0"
63
+ "@graphql-markdown/types": "^1.3.0"
64
64
  },
65
65
  "directories": {
66
66
  "test": "tests"