1 | import { GraphQLList, GraphQLNonNull, } from 'graphql';
|
2 | import CodeMirror from 'codemirror';
|
3 | import getTypeInfo from './utils/getTypeInfo';
|
4 | import { getArgumentReference, getDirectiveReference, getEnumValueReference, getFieldReference, getTypeReference, } from './utils/SchemaReference';
|
5 | import './utils/info-addon';
|
6 | CodeMirror.registerHelper('info', 'graphql', (token, options) => {
|
7 | if (!options.schema || !token.state) {
|
8 | return;
|
9 | }
|
10 | const state = token.state;
|
11 | const kind = state.kind;
|
12 | const step = state.step;
|
13 | const typeInfo = getTypeInfo(options.schema, token.state);
|
14 | if ((kind === 'Field' && step === 0 && typeInfo.fieldDef) ||
|
15 | (kind === 'AliasedField' && step === 2 && typeInfo.fieldDef)) {
|
16 | const header = document.createElement('div');
|
17 | header.className = 'CodeMirror-info-header';
|
18 | renderField(header, typeInfo, options);
|
19 | const into = document.createElement('div');
|
20 | into.appendChild(header);
|
21 | renderDescription(into, options, typeInfo.fieldDef);
|
22 | return into;
|
23 | }
|
24 | else if (kind === 'Directive' && step === 1 && typeInfo.directiveDef) {
|
25 | const header = document.createElement('div');
|
26 | header.className = 'CodeMirror-info-header';
|
27 | renderDirective(header, typeInfo, options);
|
28 | const into = document.createElement('div');
|
29 | into.appendChild(header);
|
30 | renderDescription(into, options, typeInfo.directiveDef);
|
31 | return into;
|
32 | }
|
33 | else if (kind === 'Argument' && step === 0 && typeInfo.argDef) {
|
34 | const header = document.createElement('div');
|
35 | header.className = 'CodeMirror-info-header';
|
36 | renderArg(header, typeInfo, options);
|
37 | const into = document.createElement('div');
|
38 | into.appendChild(header);
|
39 | renderDescription(into, options, typeInfo.argDef);
|
40 | return into;
|
41 | }
|
42 | else if (kind === 'EnumValue' &&
|
43 | typeInfo.enumValue &&
|
44 | typeInfo.enumValue.description) {
|
45 | const header = document.createElement('div');
|
46 | header.className = 'CodeMirror-info-header';
|
47 | renderEnumValue(header, typeInfo, options);
|
48 | const into = document.createElement('div');
|
49 | into.appendChild(header);
|
50 | renderDescription(into, options, typeInfo.enumValue);
|
51 | return into;
|
52 | }
|
53 | else if (kind === 'NamedType' &&
|
54 | typeInfo.type &&
|
55 | typeInfo.type.description) {
|
56 | const header = document.createElement('div');
|
57 | header.className = 'CodeMirror-info-header';
|
58 | renderType(header, typeInfo, options, typeInfo.type);
|
59 | const into = document.createElement('div');
|
60 | into.appendChild(header);
|
61 | renderDescription(into, options, typeInfo.type);
|
62 | return into;
|
63 | }
|
64 | });
|
65 | function renderField(into, typeInfo, options) {
|
66 | renderQualifiedField(into, typeInfo, options);
|
67 | renderTypeAnnotation(into, typeInfo, options, typeInfo.type);
|
68 | }
|
69 | function renderQualifiedField(into, typeInfo, options) {
|
70 | var _a;
|
71 | const fieldName = ((_a = typeInfo.fieldDef) === null || _a === void 0 ? void 0 : _a.name) || '';
|
72 | text(into, fieldName, 'field-name', options, getFieldReference(typeInfo));
|
73 | }
|
74 | function renderDirective(into, typeInfo, options) {
|
75 | var _a;
|
76 | const name = '@' + (((_a = typeInfo.directiveDef) === null || _a === void 0 ? void 0 : _a.name) || '');
|
77 | text(into, name, 'directive-name', options, getDirectiveReference(typeInfo));
|
78 | }
|
79 | function renderArg(into, typeInfo, options) {
|
80 | var _a;
|
81 | const name = ((_a = typeInfo.argDef) === null || _a === void 0 ? void 0 : _a.name) || '';
|
82 | text(into, name, 'arg-name', options, getArgumentReference(typeInfo));
|
83 | renderTypeAnnotation(into, typeInfo, options, typeInfo.inputType);
|
84 | }
|
85 | function renderEnumValue(into, typeInfo, options) {
|
86 | var _a;
|
87 | const name = ((_a = typeInfo.enumValue) === null || _a === void 0 ? void 0 : _a.name) || '';
|
88 | renderType(into, typeInfo, options, typeInfo.inputType);
|
89 | text(into, '.');
|
90 | text(into, name, 'enum-value', options, getEnumValueReference(typeInfo));
|
91 | }
|
92 | function renderTypeAnnotation(into, typeInfo, options, t) {
|
93 | const typeSpan = document.createElement('span');
|
94 | typeSpan.className = 'type-name-pill';
|
95 | if (t instanceof GraphQLNonNull) {
|
96 | renderType(typeSpan, typeInfo, options, t.ofType);
|
97 | text(typeSpan, '!');
|
98 | }
|
99 | else if (t instanceof GraphQLList) {
|
100 | text(typeSpan, '[');
|
101 | renderType(typeSpan, typeInfo, options, t.ofType);
|
102 | text(typeSpan, ']');
|
103 | }
|
104 | else {
|
105 | text(typeSpan, (t === null || t === void 0 ? void 0 : t.name) || '', 'type-name', options, getTypeReference(typeInfo, t));
|
106 | }
|
107 | into.appendChild(typeSpan);
|
108 | }
|
109 | function renderType(into, typeInfo, options, t) {
|
110 | if (t instanceof GraphQLNonNull) {
|
111 | renderType(into, typeInfo, options, t.ofType);
|
112 | text(into, '!');
|
113 | }
|
114 | else if (t instanceof GraphQLList) {
|
115 | text(into, '[');
|
116 | renderType(into, typeInfo, options, t.ofType);
|
117 | text(into, ']');
|
118 | }
|
119 | else {
|
120 | text(into, (t === null || t === void 0 ? void 0 : t.name) || '', 'type-name', options, getTypeReference(typeInfo, t));
|
121 | }
|
122 | }
|
123 | function renderDescription(into, options, def) {
|
124 | const description = def.description;
|
125 | if (description) {
|
126 | const descriptionDiv = document.createElement('div');
|
127 | descriptionDiv.className = 'info-description';
|
128 | if (options.renderDescription) {
|
129 | descriptionDiv.innerHTML = options.renderDescription(description);
|
130 | }
|
131 | else {
|
132 | descriptionDiv.appendChild(document.createTextNode(description));
|
133 | }
|
134 | into.appendChild(descriptionDiv);
|
135 | }
|
136 | renderDeprecation(into, options, def);
|
137 | }
|
138 | function renderDeprecation(into, options, def) {
|
139 | const reason = def.deprecationReason;
|
140 | if (reason) {
|
141 | const deprecationDiv = document.createElement('div');
|
142 | deprecationDiv.className = 'info-deprecation';
|
143 | into.appendChild(deprecationDiv);
|
144 | const label = document.createElement('span');
|
145 | label.className = 'info-deprecation-label';
|
146 | label.appendChild(document.createTextNode('Deprecated'));
|
147 | deprecationDiv.appendChild(label);
|
148 | const reasonDiv = document.createElement('div');
|
149 | reasonDiv.className = 'info-deprecation-reason';
|
150 | if (options.renderDescription) {
|
151 | reasonDiv.innerHTML = options.renderDescription(reason);
|
152 | }
|
153 | else {
|
154 | reasonDiv.appendChild(document.createTextNode(reason));
|
155 | }
|
156 | deprecationDiv.appendChild(reasonDiv);
|
157 | }
|
158 | }
|
159 | function text(into, content, className = '', options = { onClick: null }, ref = null) {
|
160 | if (className) {
|
161 | const onClick = options.onClick;
|
162 | let node;
|
163 | if (onClick) {
|
164 | node = document.createElement('a');
|
165 | node.href = 'javascript:void 0';
|
166 | node.addEventListener('click', (e) => {
|
167 | onClick(ref, e);
|
168 | });
|
169 | }
|
170 | else {
|
171 | node = document.createElement('span');
|
172 | }
|
173 | node.className = className;
|
174 | node.appendChild(document.createTextNode(content));
|
175 | into.appendChild(node);
|
176 | }
|
177 | else {
|
178 | into.appendChild(document.createTextNode(content));
|
179 | }
|
180 | }
|
181 |
|
\ | No newline at end of file |