@graphql-markdown/printer-legacy 1.6.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.
package/dist/badge.js CHANGED
@@ -27,7 +27,7 @@ const getTypeBadges = (type, groups) => {
27
27
  if ((0, graphql_1.isListType)(rootType)) {
28
28
  badges.push({ text: "list", classname: exports.DEFAULT_CSS_CLASSNAME });
29
29
  }
30
- const category = (0, link_1.getLinkCategory)((0, graphql_1.getNamedType)(rootType));
30
+ const category = (0, link_1.getCategoryLocale)((0, graphql_1.getNamedType)(rootType));
31
31
  if (category) {
32
32
  badges.push({ text: category, classname: exports.DEFAULT_CSS_CLASSNAME });
33
33
  }
@@ -11,10 +11,12 @@ 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,
17
18
  typeBadges: true,
19
+ useApiGroup: true,
18
20
  };
19
21
  exports.DEFAULT_OPTIONS = {
20
22
  ...exports.PRINT_TYPE_DEFAULT_OPTIONS,
@@ -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/group.js CHANGED
@@ -4,7 +4,11 @@ exports.getGroup = void 0;
4
4
  const graphql_1 = require("@graphql-markdown/graphql");
5
5
  const utils_1 = require("@graphql-markdown/utils");
6
6
  const getGroup = (type, groups, typeCategory) => {
7
- if (typeof type !== "object" || type === null || !groups || !typeCategory) {
7
+ if (typeof type !== "object" ||
8
+ type === null ||
9
+ typeof groups !== "object" ||
10
+ groups === null ||
11
+ !typeCategory) {
8
12
  return "";
9
13
  }
10
14
  const graphQLNamedType = (0, graphql_1.getNamedType)(type);
package/dist/link.d.ts CHANGED
@@ -1,7 +1,10 @@
1
- import type { MDXString, Maybe, PrintLinkOptions, TypeLink, TypeLocale } from "@graphql-markdown/types";
2
- export declare const getLinkCategory: (type: unknown) => Maybe<TypeLocale>;
1
+ import type { MDXString, Maybe, PrintLinkOptions, TypeDeprecatedOption, TypeLink, TypeLocale } from "@graphql-markdown/types";
2
+ export declare const getCategoryLocale: (type: unknown) => Maybe<TypeLocale>;
3
+ export declare const getLinkCategoryFolder: (type: unknown, operationLocale?: Maybe<TypeLocale>) => Maybe<string>;
3
4
  export declare const hasOptionWithAttributes: (options: PrintLinkOptions) => boolean;
4
5
  export declare const hasOptionParentType: (options: PrintLinkOptions) => boolean;
6
+ export declare const getLinkApiGroupFolder: (type: unknown) => string;
7
+ export declare const getLinkDeprecatedFolder: (type: unknown, option: Maybe<TypeDeprecatedOption>) => string;
5
8
  export declare const toLink: (type: unknown, name: string, operation: Maybe<TypeLocale>, options: PrintLinkOptions) => TypeLink;
6
9
  export declare const getRelationLink: (category: Maybe<TypeLocale>, type: unknown, options: PrintLinkOptions) => Maybe<TypeLink>;
7
10
  export declare const printLinkAttributes: (type: unknown, text?: Maybe<string>) => string;
package/dist/link.js CHANGED
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.printParentLink = exports.printLink = exports.printLinkAttributes = exports.getRelationLink = exports.toLink = exports.hasOptionParentType = exports.hasOptionWithAttributes = exports.getLinkCategory = void 0;
3
+ exports.printParentLink = exports.printLink = exports.printLinkAttributes = exports.getRelationLink = exports.toLink = exports.getLinkDeprecatedFolder = exports.getLinkApiGroupFolder = exports.hasOptionParentType = exports.hasOptionWithAttributes = exports.getLinkCategoryFolder = exports.getCategoryLocale = void 0;
4
4
  const graphql_1 = require("@graphql-markdown/graphql");
5
5
  const utils_1 = require("@graphql-markdown/utils");
6
6
  const group_1 = require("./group");
7
7
  const strings_1 = require("./const/strings");
8
8
  const common_1 = require("./common");
9
- const getLinkCategory = (type) => {
9
+ const getCategoryLocale = (type) => {
10
10
  switch (true) {
11
11
  case (0, graphql_1.isDirectiveType)(type):
12
12
  return strings_1.ROOT_TYPE_LOCALE.DIRECTIVE;
@@ -27,7 +27,26 @@ const getLinkCategory = (type) => {
27
27
  }
28
28
  return undefined;
29
29
  };
30
- exports.getLinkCategory = getLinkCategory;
30
+ exports.getCategoryLocale = getCategoryLocale;
31
+ const getLinkCategoryFolder = (type, operationLocale) => {
32
+ const categoryLocale = (0, exports.getCategoryLocale)(type);
33
+ if (!categoryLocale) {
34
+ return undefined;
35
+ }
36
+ // special case for relation map
37
+ if (categoryLocale === strings_1.ROOT_TYPE_LOCALE.OPERATION) {
38
+ if (!operationLocale) {
39
+ return undefined;
40
+ }
41
+ return typeof operationLocale === "object"
42
+ ? operationLocale.plural
43
+ : operationLocale;
44
+ }
45
+ return typeof categoryLocale === "object"
46
+ ? categoryLocale.plural
47
+ : categoryLocale;
48
+ };
49
+ exports.getLinkCategoryFolder = getLinkCategoryFolder;
31
50
  const hasOptionWithAttributes = (options) => {
32
51
  return "withAttributes" in options && options.withAttributes === true;
33
52
  };
@@ -39,46 +58,40 @@ const hasOptionParentType = (options) => {
39
58
  typeof options.parentType !== "undefined");
40
59
  };
41
60
  exports.hasOptionParentType = hasOptionParentType;
61
+ const getLinkApiGroupFolder = (type) => {
62
+ return (0, graphql_1.isApiType)(type) ? "api" : "types";
63
+ };
64
+ exports.getLinkApiGroupFolder = getLinkApiGroupFolder;
65
+ const getLinkDeprecatedFolder = (type, option) => {
66
+ return option === "group" && (0, graphql_1.isDeprecated)(type) ? strings_1.DEPRECATED : "";
67
+ };
68
+ exports.getLinkDeprecatedFolder = getLinkDeprecatedFolder;
42
69
  const toLink = (type, name, operation, options) => {
43
70
  const fallback = {
44
71
  text: name,
45
72
  url: "#",
46
73
  };
47
- if (typeof type !== "object") {
48
- return fallback;
49
- }
50
- if (typeof options !== "undefined" && !(0, common_1.hasPrintableDirective)(type, options)) {
74
+ if (typeof type !== "object" ||
75
+ (typeof options !== "undefined" && !(0, common_1.hasPrintableDirective)(type, options))) {
51
76
  return fallback;
52
77
  }
53
78
  const graphQLNamedType = (0, graphql_1.getNamedType)(type);
54
- let category = (0, exports.getLinkCategory)(graphQLNamedType);
79
+ const category = (0, exports.getLinkCategoryFolder)(graphQLNamedType, operation);
55
80
  if (!category || !graphQLNamedType) {
56
81
  return fallback;
57
82
  }
58
- // special case for relation map
59
- if (category === strings_1.ROOT_TYPE_LOCALE.OPERATION) {
60
- if (!operation) {
61
- return fallback;
62
- }
63
- category = operation;
64
- }
65
- if (typeof category === "object") {
66
- category = category.plural;
67
- }
68
83
  const text = graphQLNamedType.name || graphQLNamedType.toString();
69
- const deprecated = typeof options !== "undefined" &&
70
- "deprecated" in options &&
71
- options.deprecated === "group" &&
72
- (0, graphql_1.isDeprecated)(type)
73
- ? strings_1.DEPRECATED
84
+ const deprecatedFolder = options.deprecated
85
+ ? (0, exports.getLinkDeprecatedFolder)(type, options.deprecated)
74
86
  : "";
75
- const group = "groups" in options && options.groups
87
+ const groupFolder = options.groups
76
88
  ? (0, group_1.getGroup)(type, options.groups, category)
77
89
  : "";
78
- const url = utils_1.pathUrl.join(options.basePath, deprecated, group, category, (0, utils_1.slugify)(text));
90
+ const apiGroupFolder = options.useApiGroup ? (0, exports.getLinkApiGroupFolder)(type) : "";
91
+ const url = utils_1.pathUrl.join(options.basePath, deprecatedFolder, apiGroupFolder, groupFolder, category, (0, utils_1.slugify)(text));
79
92
  return {
80
- text: text,
81
- url: url,
93
+ text,
94
+ url,
82
95
  };
83
96
  };
84
97
  exports.toLink = toLink;
package/dist/printer.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import type { CustomDirectiveMap, GraphQLDirective, GraphQLSchema, IPrinter, MDXString, Maybe, PrintTypeOptions, PrinterConfigPrintTypeOptions, SchemaEntitiesGroupMap, TypeDeprecatedOption } from "@graphql-markdown/types";
2
2
  export declare class Printer implements IPrinter {
3
- static options: Maybe<PrintTypeOptions>;
4
- static printDescription: (type: unknown, options?: PrintTypeOptions | undefined, noText?: string | undefined) => string | MDXString;
5
- static printCustomDirectives: (type: unknown, options: PrintTypeOptions) => string;
6
- static printCustomTags: (type: unknown, options: PrintTypeOptions) => string | MDXString;
3
+ static options: Readonly<Maybe<PrintTypeOptions>>;
4
+ static readonly printDescription: (type: unknown, options?: PrintTypeOptions | undefined, noText?: string | undefined) => string | MDXString;
5
+ static readonly printCustomDirectives: (type: unknown, options: PrintTypeOptions) => string;
6
+ static readonly printCustomTags: (type: unknown, options: PrintTypeOptions) => string | MDXString;
7
7
  static init(schema: Maybe<GraphQLSchema>, baseURL?: Maybe<string>, linkRoot?: Maybe<string>, { customDirectives, groups, onlyDocDirectives, printTypeOptions, skipDocDirectives, metatags, }?: {
8
8
  customDirectives?: CustomDirectiveMap;
9
9
  deprecated?: TypeDeprecatedOption;
@@ -13,10 +13,11 @@ export declare class Printer implements IPrinter {
13
13
  skipDocDirectives?: GraphQLDirective[];
14
14
  metatags?: Record<string, string>[];
15
15
  }): void;
16
- static printHeader: (id: string, title: string, options: PrintTypeOptions) => string;
17
- static printCode: (type: unknown, options: PrintTypeOptions) => string;
18
- static printTypeMetadata: (type: unknown, options: PrintTypeOptions) => MDXString | string;
19
- static printRelations: (type: unknown, options: PrintTypeOptions) => MDXString | string;
20
- static printMetaTags: (_type: unknown, { metatags }: PrintTypeOptions) => MDXString | string;
21
- static printType: (name: Maybe<string>, type: unknown, options?: Maybe<Partial<PrintTypeOptions>>) => Maybe<MDXString>;
16
+ static readonly printHeader: (id: string, title: string, options: PrintTypeOptions) => string;
17
+ static readonly printCode: (type: unknown, options: PrintTypeOptions) => string;
18
+ static readonly printExample: (type: unknown, options: PrintTypeOptions) => string;
19
+ static readonly printTypeMetadata: (type: unknown, options: PrintTypeOptions) => MDXString | string;
20
+ static readonly printRelations: (type: unknown, options: PrintTypeOptions) => MDXString | string;
21
+ static readonly printMetaTags: (_type: unknown, { metatags }: PrintTypeOptions) => MDXString | string;
22
+ static readonly printType: (name: Maybe<string>, type: unknown, options?: Maybe<Partial<PrintTypeOptions>>) => Maybe<MDXString>;
22
23
  }
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,
@@ -43,6 +42,7 @@ class Printer {
43
42
  skipDocDirectives: skipDocDirectives ?? [],
44
43
  typeBadges: printTypeOptions?.typeBadges ?? options_1.PRINT_TYPE_DEFAULT_OPTIONS.typeBadges,
45
44
  metatags: metatags ?? [],
45
+ useApiGroup: printTypeOptions?.useApiGroup ?? options_1.PRINT_TYPE_DEFAULT_OPTIONS.useApiGroup,
46
46
  };
47
47
  }
48
48
  static printHeader = (id, title, options) => {
@@ -85,6 +85,32 @@ class Printer {
85
85
  }
86
86
  return strings_1.MARKDOWN_SOC + code.trim() + strings_1.MARKDOWN_EOC;
87
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
+ };
88
114
  static printTypeMetadata = (type, options) => {
89
115
  switch (true) {
90
116
  case (0, graphql_1.isScalarType)(type):
@@ -142,6 +168,7 @@ class Printer {
142
168
  const tags = Printer.printCustomTags(type, printTypeOptions);
143
169
  const metadata = Printer.printTypeMetadata(type, printTypeOptions);
144
170
  const relations = Printer.printRelations(type, printTypeOptions);
171
+ const example = Printer.printExample(type, printTypeOptions);
145
172
  return [
146
173
  header,
147
174
  metatags,
@@ -151,6 +178,7 @@ class Printer {
151
178
  code,
152
179
  customDirectives,
153
180
  metadata,
181
+ example,
154
182
  relations,
155
183
  ].join(strings_1.MARKDOWN_EOP);
156
184
  };
package/dist/relation.js CHANGED
@@ -55,7 +55,7 @@ const printRelationOf = (type, section, getRelation, options) => {
55
55
  exports.printRelationOf = printRelationOf;
56
56
  const printRelations = (type, options) => {
57
57
  const relations = {
58
- returnedBy: { section: "Returned by", getRelation: graphql_1.getRelationOfReturn },
58
+ returnedBy: { section: "Returned By", getRelation: graphql_1.getRelationOfReturn },
59
59
  memberOf: { section: "Member Of", getRelation: graphql_1.getRelationOfField },
60
60
  implementedBy: {
61
61
  section: "Implemented By",
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.6.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.0.2",
60
- "@graphql-markdown/utils": "^1.6.2"
59
+ "@graphql-markdown/graphql": "^1.1.1",
60
+ "@graphql-markdown/utils": "^1.6.3"
61
61
  },
62
62
  "devDependencies": {
63
- "@graphql-markdown/types": "^1.0.1"
63
+ "@graphql-markdown/types": "^1.3.0"
64
64
  },
65
65
  "directories": {
66
66
  "test": "tests"