1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import React from 'react';
|
9 | import { GraphQLArgument, DirectiveNode, isType } from 'graphql';
|
10 | import { useExplorerContext } from '@graphiql/react';
|
11 |
|
12 | import Argument from './Argument';
|
13 | import Directive from './Directive';
|
14 | import MarkdownContent from './MarkdownContent';
|
15 | import TypeLink from './TypeLink';
|
16 |
|
17 | export default function FieldDoc() {
|
18 | const { explorerNavStack } = useExplorerContext({ nonNull: true });
|
19 | const [showDeprecated, handleShowDeprecated] = React.useState(false);
|
20 |
|
21 | const navItem = explorerNavStack[explorerNavStack.length - 1];
|
22 | const field = navItem.def;
|
23 | if (!field || isType(field)) {
|
24 | return null;
|
25 | }
|
26 |
|
27 | let argsDef;
|
28 | let deprecatedArgsDef;
|
29 | if (field && 'args' in field && field.args.length > 0) {
|
30 | argsDef = (
|
31 | <div id="doc-args" className="doc-category">
|
32 | <div className="doc-category-title">arguments</div>
|
33 | {field.args
|
34 | .filter(arg => !arg.deprecationReason)
|
35 | .map((arg: GraphQLArgument) => (
|
36 | <div key={arg.name} className="doc-category-item">
|
37 | <div>
|
38 | <Argument arg={arg} />
|
39 | </div>
|
40 | <MarkdownContent
|
41 | className="doc-value-description"
|
42 | markdown={arg.description}
|
43 | />
|
44 | {arg && 'deprecationReason' in arg && (
|
45 | <MarkdownContent
|
46 | className="doc-deprecation"
|
47 | markdown={arg?.deprecationReason}
|
48 | />
|
49 | )}
|
50 | </div>
|
51 | ))}
|
52 | </div>
|
53 | );
|
54 | const deprecatedArgs = field.args.filter(arg =>
|
55 | Boolean(arg.deprecationReason),
|
56 | );
|
57 | if (deprecatedArgs.length > 0) {
|
58 | deprecatedArgsDef = (
|
59 | <div id="doc-deprecated-args" className="doc-category">
|
60 | <div className="doc-category-title">deprecated arguments</div>
|
61 | {!showDeprecated ? (
|
62 | <button
|
63 | className="show-btn"
|
64 | onClick={() => handleShowDeprecated(!showDeprecated)}
|
65 | >
|
66 | Show deprecated arguments...
|
67 | </button>
|
68 | ) : (
|
69 | deprecatedArgs.map((arg, i) => (
|
70 | <div key={i}>
|
71 | <div>
|
72 | <Argument arg={arg} />
|
73 | </div>
|
74 | <MarkdownContent
|
75 | className="doc-value-description"
|
76 | markdown={arg.description}
|
77 | />
|
78 | {arg && 'deprecationReason' in arg && (
|
79 | <MarkdownContent
|
80 | className="doc-deprecation"
|
81 | markdown={arg?.deprecationReason}
|
82 | />
|
83 | )}
|
84 | </div>
|
85 | ))
|
86 | )}
|
87 | </div>
|
88 | );
|
89 | }
|
90 | }
|
91 |
|
92 | let directivesDef;
|
93 | if (field?.astNode?.directives && field.astNode.directives.length > 0) {
|
94 | directivesDef = (
|
95 | <div id="doc-directives" className="doc-category">
|
96 | <div className="doc-category-title">directives</div>
|
97 | {field.astNode.directives.map((directive: DirectiveNode) => (
|
98 | <div key={directive.name.value} className="doc-category-item">
|
99 | <div>
|
100 | <Directive directive={directive} />
|
101 | </div>
|
102 | </div>
|
103 | ))}
|
104 | </div>
|
105 | );
|
106 | }
|
107 |
|
108 | return (
|
109 | <div>
|
110 | <MarkdownContent
|
111 | className="doc-type-description"
|
112 | markdown={field.description || 'No Description'}
|
113 | />
|
114 | {field && 'deprecationReason' in field && (
|
115 | <MarkdownContent
|
116 | className="doc-deprecation"
|
117 | markdown={field.deprecationReason}
|
118 | />
|
119 | )}
|
120 | <div className="doc-category">
|
121 | <div className="doc-category-title">type</div>
|
122 | <TypeLink type={field.type} />
|
123 | </div>
|
124 | {argsDef}
|
125 | {directivesDef}
|
126 | {deprecatedArgsDef}
|
127 | </div>
|
128 | );
|
129 | }
|