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