@graphql-codegen/c-sharp 4.2.15 → 4.3.0-alpha-29eb1293b.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/cjs/config.js +2 -0
- package/cjs/index.js +16 -0
- package/cjs/json-attributes.js +27 -0
- package/cjs/package.json +1 -0
- package/cjs/visitor.js +325 -0
- package/esm/config.js +1 -0
- package/esm/index.js +12 -0
- package/esm/json-attributes.js +22 -0
- package/{index.mjs → esm/visitor.js} +5 -327
- package/package.json +25 -17
- package/{config.d.ts → typings/config.d.ts} +1 -1
- package/{index.d.ts → typings/index.d.ts} +1 -1
- package/{json-attributes.d.ts → typings/json-attributes.d.ts} +0 -0
- package/{visitor.d.ts → typings/visitor.d.ts} +3 -3
- package/index.js +0 -647
package/index.js
DELETED
|
@@ -1,647 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
const pluginHelpers = require('@graphql-codegen/plugin-helpers');
|
|
6
|
-
const visitorPluginCommon = require('@graphql-codegen/visitor-plugin-common');
|
|
7
|
-
const graphql = require('graphql');
|
|
8
|
-
const changeCaseAll = require('change-case-all');
|
|
9
|
-
|
|
10
|
-
const C_SHARP_SCALARS = {
|
|
11
|
-
ID: 'string',
|
|
12
|
-
String: 'string',
|
|
13
|
-
Boolean: 'bool',
|
|
14
|
-
Int: 'int',
|
|
15
|
-
Float: 'double',
|
|
16
|
-
Date: 'DateTime',
|
|
17
|
-
};
|
|
18
|
-
const csharpValueTypes = [
|
|
19
|
-
'bool',
|
|
20
|
-
'byte',
|
|
21
|
-
'sbyte',
|
|
22
|
-
'char',
|
|
23
|
-
'decimal',
|
|
24
|
-
'double',
|
|
25
|
-
'float',
|
|
26
|
-
'int',
|
|
27
|
-
'uint',
|
|
28
|
-
'long',
|
|
29
|
-
'ulong',
|
|
30
|
-
'short',
|
|
31
|
-
'ushort',
|
|
32
|
-
'DateTime',
|
|
33
|
-
];
|
|
34
|
-
|
|
35
|
-
// This file is bundled and inlined.
|
|
36
|
-
function transformComment(comment, indentLevel = 0) {
|
|
37
|
-
if (!comment) {
|
|
38
|
-
return '';
|
|
39
|
-
}
|
|
40
|
-
if (isStringValueNode(comment)) {
|
|
41
|
-
comment = comment.value;
|
|
42
|
-
}
|
|
43
|
-
comment = comment.trimStart().split('*/').join('*\\/');
|
|
44
|
-
let lines = comment.split('\n');
|
|
45
|
-
lines = ['/// <summary>', ...lines.map(line => `/// ${line}`), '/// </summary>'];
|
|
46
|
-
return lines
|
|
47
|
-
.map(line => visitorPluginCommon.indent(line, indentLevel))
|
|
48
|
-
.concat('')
|
|
49
|
-
.join('\n');
|
|
50
|
-
}
|
|
51
|
-
function isStringValueNode(node) {
|
|
52
|
-
return node && typeof node === 'object' && node.kind === graphql.Kind.STRING;
|
|
53
|
-
}
|
|
54
|
-
function isValueType(type) {
|
|
55
|
-
// Limitation: only checks the list of known built in value types
|
|
56
|
-
// Eg .NET types and struct types won't be detected correctly
|
|
57
|
-
return csharpValueTypes.includes(type);
|
|
58
|
-
}
|
|
59
|
-
function getListTypeField(typeNode) {
|
|
60
|
-
if (typeNode.kind === graphql.Kind.LIST_TYPE) {
|
|
61
|
-
return {
|
|
62
|
-
required: false,
|
|
63
|
-
type: getListTypeField(typeNode.type),
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
if (typeNode.kind === graphql.Kind.NON_NULL_TYPE && typeNode.type.kind === graphql.Kind.LIST_TYPE) {
|
|
67
|
-
return Object.assign(getListTypeField(typeNode.type), {
|
|
68
|
-
required: true,
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
if (typeNode.kind === graphql.Kind.NON_NULL_TYPE) {
|
|
72
|
-
return getListTypeField(typeNode.type);
|
|
73
|
-
}
|
|
74
|
-
return undefined;
|
|
75
|
-
}
|
|
76
|
-
function getListInnerTypeNode(typeNode) {
|
|
77
|
-
if (typeNode.kind === graphql.Kind.LIST_TYPE) {
|
|
78
|
-
return getListInnerTypeNode(typeNode.type);
|
|
79
|
-
}
|
|
80
|
-
if (typeNode.kind === graphql.Kind.NON_NULL_TYPE && typeNode.type.kind === graphql.Kind.LIST_TYPE) {
|
|
81
|
-
return getListInnerTypeNode(typeNode.type);
|
|
82
|
-
}
|
|
83
|
-
return typeNode;
|
|
84
|
-
}
|
|
85
|
-
function wrapFieldType(fieldType, listTypeField, listType = 'IEnumerable') {
|
|
86
|
-
if (listTypeField) {
|
|
87
|
-
const innerType = wrapFieldType(fieldType, listTypeField.type, listType);
|
|
88
|
-
return `${listType}<${innerType}>`;
|
|
89
|
-
}
|
|
90
|
-
return fieldType.innerTypeName;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// This file is bundled and inlined.
|
|
94
|
-
class CSharpDeclarationBlock {
|
|
95
|
-
constructor() {
|
|
96
|
-
this._name = null;
|
|
97
|
-
this._extendStr = [];
|
|
98
|
-
this._implementsStr = [];
|
|
99
|
-
this._kind = null;
|
|
100
|
-
this._access = 'public';
|
|
101
|
-
this._final = false;
|
|
102
|
-
this._static = false;
|
|
103
|
-
this._block = null;
|
|
104
|
-
this._comment = null;
|
|
105
|
-
this._nestedClasses = [];
|
|
106
|
-
}
|
|
107
|
-
nestedClass(nstCls) {
|
|
108
|
-
this._nestedClasses.push(nstCls);
|
|
109
|
-
return this;
|
|
110
|
-
}
|
|
111
|
-
access(access) {
|
|
112
|
-
this._access = access;
|
|
113
|
-
return this;
|
|
114
|
-
}
|
|
115
|
-
asKind(kind) {
|
|
116
|
-
this._kind = kind;
|
|
117
|
-
return this;
|
|
118
|
-
}
|
|
119
|
-
final() {
|
|
120
|
-
this._final = true;
|
|
121
|
-
return this;
|
|
122
|
-
}
|
|
123
|
-
static() {
|
|
124
|
-
this._static = true;
|
|
125
|
-
return this;
|
|
126
|
-
}
|
|
127
|
-
withComment(comment) {
|
|
128
|
-
if (comment) {
|
|
129
|
-
this._comment = transformComment(comment, 1);
|
|
130
|
-
}
|
|
131
|
-
return this;
|
|
132
|
-
}
|
|
133
|
-
withBlock(block) {
|
|
134
|
-
this._block = block;
|
|
135
|
-
return this;
|
|
136
|
-
}
|
|
137
|
-
extends(extendStr) {
|
|
138
|
-
this._extendStr = extendStr;
|
|
139
|
-
return this;
|
|
140
|
-
}
|
|
141
|
-
implements(implementsStr) {
|
|
142
|
-
this._implementsStr = implementsStr;
|
|
143
|
-
return this;
|
|
144
|
-
}
|
|
145
|
-
withName(name) {
|
|
146
|
-
this._name = typeof name === 'object' ? name.value : name;
|
|
147
|
-
return this;
|
|
148
|
-
}
|
|
149
|
-
get string() {
|
|
150
|
-
let result = '';
|
|
151
|
-
if (this._kind) {
|
|
152
|
-
let name = '';
|
|
153
|
-
if (this._name) {
|
|
154
|
-
name = this._name;
|
|
155
|
-
}
|
|
156
|
-
if (this._kind === 'namespace') {
|
|
157
|
-
result += `${this._kind} ${name} `;
|
|
158
|
-
}
|
|
159
|
-
else {
|
|
160
|
-
let extendStr = '';
|
|
161
|
-
let implementsStr = '';
|
|
162
|
-
const final = this._final ? ' final' : '';
|
|
163
|
-
const isStatic = this._static ? ' static' : '';
|
|
164
|
-
if (this._extendStr.length > 0) {
|
|
165
|
-
extendStr = ` : ${this._extendStr.join(', ')}`;
|
|
166
|
-
}
|
|
167
|
-
if (this._implementsStr.length > 0) {
|
|
168
|
-
implementsStr = ` : ${this._implementsStr.join(', ')}`;
|
|
169
|
-
}
|
|
170
|
-
result += `${this._access}${isStatic}${final} ${this._kind} ${name}${extendStr}${implementsStr} `;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
const nestedClasses = this._nestedClasses.length
|
|
174
|
-
? this._nestedClasses.map(c => visitorPluginCommon.indentMultiline(c.string)).join('\n\n')
|
|
175
|
-
: null;
|
|
176
|
-
const before = '{';
|
|
177
|
-
const after = '}';
|
|
178
|
-
const block = [before, nestedClasses, this._block, after].filter(f => f).join('\n');
|
|
179
|
-
result += block;
|
|
180
|
-
return (this._comment ? this._comment : '') + result + '\n';
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
class CSharpFieldType {
|
|
185
|
-
constructor(fieldType) {
|
|
186
|
-
Object.assign(this, fieldType);
|
|
187
|
-
}
|
|
188
|
-
get innerTypeName() {
|
|
189
|
-
const nullable = this.baseType.valueType && !this.baseType.required ? '?' : '';
|
|
190
|
-
return `${this.baseType.type}${nullable}`;
|
|
191
|
-
}
|
|
192
|
-
get isOuterTypeRequired() {
|
|
193
|
-
return this.listType ? this.listType.required : this.baseType.required;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* C# keywords
|
|
199
|
-
* https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/
|
|
200
|
-
*/
|
|
201
|
-
const csharpKeywords = new Set([
|
|
202
|
-
'abstract',
|
|
203
|
-
'as',
|
|
204
|
-
'base',
|
|
205
|
-
'bool',
|
|
206
|
-
'break',
|
|
207
|
-
'byte',
|
|
208
|
-
'case',
|
|
209
|
-
'catch',
|
|
210
|
-
'char',
|
|
211
|
-
'checked',
|
|
212
|
-
'class',
|
|
213
|
-
'const',
|
|
214
|
-
'continue',
|
|
215
|
-
'decimal',
|
|
216
|
-
'default',
|
|
217
|
-
'delegate',
|
|
218
|
-
'do',
|
|
219
|
-
'double',
|
|
220
|
-
'else',
|
|
221
|
-
'enum',
|
|
222
|
-
'event',
|
|
223
|
-
'explicit',
|
|
224
|
-
'extern',
|
|
225
|
-
'false',
|
|
226
|
-
'finally',
|
|
227
|
-
'fixed',
|
|
228
|
-
'float',
|
|
229
|
-
'for',
|
|
230
|
-
'foreach',
|
|
231
|
-
'goto',
|
|
232
|
-
'if',
|
|
233
|
-
'implicit',
|
|
234
|
-
'in',
|
|
235
|
-
'int',
|
|
236
|
-
'interface',
|
|
237
|
-
'internal',
|
|
238
|
-
'is',
|
|
239
|
-
'lock',
|
|
240
|
-
'long',
|
|
241
|
-
'namespace',
|
|
242
|
-
'new',
|
|
243
|
-
'null',
|
|
244
|
-
'object',
|
|
245
|
-
'operator',
|
|
246
|
-
'out',
|
|
247
|
-
'override',
|
|
248
|
-
'params',
|
|
249
|
-
'private',
|
|
250
|
-
'protected',
|
|
251
|
-
'public',
|
|
252
|
-
'readonly',
|
|
253
|
-
'record',
|
|
254
|
-
'ref',
|
|
255
|
-
'return',
|
|
256
|
-
'sbyte',
|
|
257
|
-
'sealed',
|
|
258
|
-
'short',
|
|
259
|
-
'sizeof',
|
|
260
|
-
'stackalloc',
|
|
261
|
-
'static',
|
|
262
|
-
'string',
|
|
263
|
-
'struct',
|
|
264
|
-
'switch',
|
|
265
|
-
'this',
|
|
266
|
-
'throw',
|
|
267
|
-
'true',
|
|
268
|
-
'try',
|
|
269
|
-
'typeof',
|
|
270
|
-
'uint',
|
|
271
|
-
'ulong',
|
|
272
|
-
'unchecked',
|
|
273
|
-
'unsafe',
|
|
274
|
-
'ushort',
|
|
275
|
-
'using',
|
|
276
|
-
'virtual',
|
|
277
|
-
'void',
|
|
278
|
-
'volatile',
|
|
279
|
-
'while',
|
|
280
|
-
]);
|
|
281
|
-
/**
|
|
282
|
-
* Checks name against list of keywords. If it is, will prefix value with @
|
|
283
|
-
*
|
|
284
|
-
* Note:
|
|
285
|
-
* This class should first invoke the convertName from base-visitor to convert the string or node
|
|
286
|
-
* value according the naming configuration, eg upper or lower case. Then resulting string checked
|
|
287
|
-
* against the list or keywords.
|
|
288
|
-
* However the generated C# code is not yet able to handle fields that are in a different case so
|
|
289
|
-
* the invocation of convertName is omitted purposely.
|
|
290
|
-
*/
|
|
291
|
-
function convertSafeName(node) {
|
|
292
|
-
const name = typeof node === 'string' ? node : node.value;
|
|
293
|
-
return csharpKeywords.has(name) ? `@${name}` : name;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
function unsupportedSource(attributesSource) {
|
|
297
|
-
throw new Error(`Unsupported JSON attributes source: ${attributesSource}`);
|
|
298
|
-
}
|
|
299
|
-
class JsonAttributesSourceConfiguration {
|
|
300
|
-
constructor(namespace, propertyAttribute, requiredAttribute) {
|
|
301
|
-
this.namespace = namespace;
|
|
302
|
-
this.propertyAttribute = propertyAttribute;
|
|
303
|
-
this.requiredAttribute = requiredAttribute;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
const newtonsoftConfiguration = new JsonAttributesSourceConfiguration('Newtonsoft.Json', 'JsonProperty', 'JsonRequired');
|
|
307
|
-
// System.Text.Json does not have support of `JsonRequired` alternative (as for .NET 5)
|
|
308
|
-
const systemTextJsonConfiguration = new JsonAttributesSourceConfiguration('System.Text.Json.Serialization', 'JsonPropertyName', null);
|
|
309
|
-
function getJsonAttributeSourceConfiguration(attributesSource) {
|
|
310
|
-
switch (attributesSource) {
|
|
311
|
-
case 'Newtonsoft.Json':
|
|
312
|
-
return newtonsoftConfiguration;
|
|
313
|
-
case 'System.Text.Json':
|
|
314
|
-
return systemTextJsonConfiguration;
|
|
315
|
-
}
|
|
316
|
-
unsupportedSource(attributesSource);
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
class CSharpResolversVisitor extends visitorPluginCommon.BaseVisitor {
|
|
320
|
-
constructor(rawConfig, _schema) {
|
|
321
|
-
var _a;
|
|
322
|
-
super(rawConfig, {
|
|
323
|
-
enumValues: rawConfig.enumValues || {},
|
|
324
|
-
listType: rawConfig.listType || 'List',
|
|
325
|
-
namespaceName: rawConfig.namespaceName || 'GraphQLCodeGen',
|
|
326
|
-
className: rawConfig.className || 'Types',
|
|
327
|
-
emitRecords: rawConfig.emitRecords || false,
|
|
328
|
-
emitJsonAttributes: (_a = rawConfig.emitJsonAttributes) !== null && _a !== void 0 ? _a : true,
|
|
329
|
-
jsonAttributesSource: rawConfig.jsonAttributesSource || 'Newtonsoft.Json',
|
|
330
|
-
scalars: visitorPluginCommon.buildScalarsFromConfig(_schema, rawConfig, C_SHARP_SCALARS),
|
|
331
|
-
});
|
|
332
|
-
this._schema = _schema;
|
|
333
|
-
if (this._parsedConfig.emitJsonAttributes) {
|
|
334
|
-
this.jsonAttributesConfiguration = getJsonAttributeSourceConfiguration(this._parsedConfig.jsonAttributesSource);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
getImports() {
|
|
338
|
-
const allImports = ['System', 'System.Collections.Generic', 'System.ComponentModel.DataAnnotations'];
|
|
339
|
-
if (this._parsedConfig.emitJsonAttributes) {
|
|
340
|
-
const jsonAttributesNamespace = this.jsonAttributesConfiguration.namespace;
|
|
341
|
-
allImports.push(jsonAttributesNamespace);
|
|
342
|
-
}
|
|
343
|
-
return allImports.map(i => `using ${i};`).join('\n') + '\n';
|
|
344
|
-
}
|
|
345
|
-
wrapWithNamespace(content) {
|
|
346
|
-
return new CSharpDeclarationBlock()
|
|
347
|
-
.asKind('namespace')
|
|
348
|
-
.withName(this.config.namespaceName)
|
|
349
|
-
.withBlock(visitorPluginCommon.indentMultiline(content)).string;
|
|
350
|
-
}
|
|
351
|
-
wrapWithClass(content) {
|
|
352
|
-
return new CSharpDeclarationBlock()
|
|
353
|
-
.access('public')
|
|
354
|
-
.asKind('class')
|
|
355
|
-
.withName(convertSafeName(this.config.className))
|
|
356
|
-
.withBlock(visitorPluginCommon.indentMultiline(content)).string;
|
|
357
|
-
}
|
|
358
|
-
getEnumValue(enumName, enumOption) {
|
|
359
|
-
if (this.config.enumValues[enumName] &&
|
|
360
|
-
typeof this.config.enumValues[enumName] === 'object' &&
|
|
361
|
-
this.config.enumValues[enumName][enumOption]) {
|
|
362
|
-
return this.config.enumValues[enumName][enumOption];
|
|
363
|
-
}
|
|
364
|
-
return enumOption;
|
|
365
|
-
}
|
|
366
|
-
EnumValueDefinition(node) {
|
|
367
|
-
return (enumName) => {
|
|
368
|
-
const enumHeader = this.getFieldHeader(node);
|
|
369
|
-
const enumOption = convertSafeName(node.name);
|
|
370
|
-
return enumHeader + visitorPluginCommon.indent(this.getEnumValue(enumName, enumOption));
|
|
371
|
-
};
|
|
372
|
-
}
|
|
373
|
-
EnumTypeDefinition(node) {
|
|
374
|
-
const enumName = this.convertName(node.name);
|
|
375
|
-
const enumValues = node.values.map(enumValue => enumValue(node.name.value)).join(',\n');
|
|
376
|
-
const enumBlock = [enumValues].join('\n');
|
|
377
|
-
return new CSharpDeclarationBlock()
|
|
378
|
-
.access('public')
|
|
379
|
-
.asKind('enum')
|
|
380
|
-
.withComment(node.description)
|
|
381
|
-
.withName(enumName)
|
|
382
|
-
.withBlock(enumBlock).string;
|
|
383
|
-
}
|
|
384
|
-
getFieldHeader(node, fieldType) {
|
|
385
|
-
var _a;
|
|
386
|
-
const attributes = [];
|
|
387
|
-
const commentText = transformComment((_a = node.description) === null || _a === void 0 ? void 0 : _a.value);
|
|
388
|
-
const deprecationDirective = node.directives.find(v => { var _a; return ((_a = v.name) === null || _a === void 0 ? void 0 : _a.value) === 'deprecated'; });
|
|
389
|
-
if (deprecationDirective) {
|
|
390
|
-
const deprecationReason = this.getDeprecationReason(deprecationDirective);
|
|
391
|
-
attributes.push(`[Obsolete("${deprecationReason}")]`);
|
|
392
|
-
}
|
|
393
|
-
if (this._parsedConfig.emitJsonAttributes && node.kind === graphql.Kind.FIELD_DEFINITION) {
|
|
394
|
-
const jsonPropertyAttribute = this.jsonAttributesConfiguration.propertyAttribute;
|
|
395
|
-
if (jsonPropertyAttribute != null) {
|
|
396
|
-
attributes.push(`[${jsonPropertyAttribute}("${node.name.value}")]`);
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
if (node.kind === graphql.Kind.INPUT_VALUE_DEFINITION && fieldType.isOuterTypeRequired) {
|
|
400
|
-
// Should be always inserted for required fields to use in `GetInputObject()` when JSON attributes are not used
|
|
401
|
-
// or there are no JSON attributes in selected attribute source that provides `JsonRequired` alternative
|
|
402
|
-
attributes.push('[Required]');
|
|
403
|
-
if (this._parsedConfig.emitJsonAttributes) {
|
|
404
|
-
const jsonRequiredAttribute = this.jsonAttributesConfiguration.requiredAttribute;
|
|
405
|
-
if (jsonRequiredAttribute != null) {
|
|
406
|
-
attributes.push(`[${jsonRequiredAttribute}]`);
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
if (commentText || attributes.length > 0) {
|
|
411
|
-
const summary = commentText ? visitorPluginCommon.indentMultiline(commentText.trimRight()) + '\n' : '';
|
|
412
|
-
const attributeLines = attributes.length > 0
|
|
413
|
-
? attributes
|
|
414
|
-
.map(attr => visitorPluginCommon.indent(attr))
|
|
415
|
-
.concat('')
|
|
416
|
-
.join('\n')
|
|
417
|
-
: '';
|
|
418
|
-
return summary + attributeLines;
|
|
419
|
-
}
|
|
420
|
-
return '';
|
|
421
|
-
}
|
|
422
|
-
getDeprecationReason(directive) {
|
|
423
|
-
if (directive.name.value !== 'deprecated') {
|
|
424
|
-
return '';
|
|
425
|
-
}
|
|
426
|
-
const hasArguments = directive.arguments.length > 0;
|
|
427
|
-
let reason = 'Field no longer supported';
|
|
428
|
-
if (hasArguments && directive.arguments[0].value.kind === graphql.Kind.STRING) {
|
|
429
|
-
reason = directive.arguments[0].value.value;
|
|
430
|
-
}
|
|
431
|
-
return reason;
|
|
432
|
-
}
|
|
433
|
-
resolveInputFieldType(typeNode, hasDefaultValue = false) {
|
|
434
|
-
const innerType = visitorPluginCommon.getBaseTypeNode(typeNode);
|
|
435
|
-
const schemaType = this._schema.getType(innerType.name.value);
|
|
436
|
-
const listType = getListTypeField(typeNode);
|
|
437
|
-
const required = getListInnerTypeNode(typeNode).kind === graphql.Kind.NON_NULL_TYPE;
|
|
438
|
-
let result = null;
|
|
439
|
-
if (graphql.isScalarType(schemaType)) {
|
|
440
|
-
if (this.scalars[schemaType.name]) {
|
|
441
|
-
const baseType = this.scalars[schemaType.name];
|
|
442
|
-
result = new CSharpFieldType({
|
|
443
|
-
baseType: {
|
|
444
|
-
type: baseType,
|
|
445
|
-
required,
|
|
446
|
-
valueType: isValueType(baseType),
|
|
447
|
-
},
|
|
448
|
-
listType,
|
|
449
|
-
});
|
|
450
|
-
}
|
|
451
|
-
else {
|
|
452
|
-
result = new CSharpFieldType({
|
|
453
|
-
baseType: {
|
|
454
|
-
type: 'object',
|
|
455
|
-
required,
|
|
456
|
-
valueType: false,
|
|
457
|
-
},
|
|
458
|
-
listType,
|
|
459
|
-
});
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
else if (graphql.isInputObjectType(schemaType)) {
|
|
463
|
-
result = new CSharpFieldType({
|
|
464
|
-
baseType: {
|
|
465
|
-
type: `${this.convertName(schemaType.name)}`,
|
|
466
|
-
required,
|
|
467
|
-
valueType: false,
|
|
468
|
-
},
|
|
469
|
-
listType,
|
|
470
|
-
});
|
|
471
|
-
}
|
|
472
|
-
else if (graphql.isEnumType(schemaType)) {
|
|
473
|
-
result = new CSharpFieldType({
|
|
474
|
-
baseType: {
|
|
475
|
-
type: this.convertName(schemaType.name),
|
|
476
|
-
required,
|
|
477
|
-
valueType: true,
|
|
478
|
-
},
|
|
479
|
-
listType,
|
|
480
|
-
});
|
|
481
|
-
}
|
|
482
|
-
else {
|
|
483
|
-
result = new CSharpFieldType({
|
|
484
|
-
baseType: {
|
|
485
|
-
type: `${schemaType.name}`,
|
|
486
|
-
required,
|
|
487
|
-
valueType: false,
|
|
488
|
-
},
|
|
489
|
-
listType,
|
|
490
|
-
});
|
|
491
|
-
}
|
|
492
|
-
if (hasDefaultValue) {
|
|
493
|
-
// Required field is optional when default value specified, see #4273
|
|
494
|
-
(result.listType || result.baseType).required = false;
|
|
495
|
-
}
|
|
496
|
-
return result;
|
|
497
|
-
}
|
|
498
|
-
buildRecord(name, description, inputValueArray, interfaces) {
|
|
499
|
-
const classSummary = transformComment(description === null || description === void 0 ? void 0 : description.value);
|
|
500
|
-
const interfaceImpl = interfaces && interfaces.length > 0 ? ` : ${interfaces.map(ntn => ntn.name.value).join(', ')}` : '';
|
|
501
|
-
const recordMembers = inputValueArray
|
|
502
|
-
.map(arg => {
|
|
503
|
-
const fieldType = this.resolveInputFieldType(arg.type);
|
|
504
|
-
const fieldHeader = this.getFieldHeader(arg, fieldType);
|
|
505
|
-
const fieldName = convertSafeName(changeCaseAll.pascalCase(this.convertName(arg.name)));
|
|
506
|
-
const csharpFieldType = wrapFieldType(fieldType, fieldType.listType, this.config.listType);
|
|
507
|
-
return fieldHeader + visitorPluginCommon.indent(`public ${csharpFieldType} ${fieldName} { get; init; } = ${fieldName};`);
|
|
508
|
-
})
|
|
509
|
-
.join('\n\n');
|
|
510
|
-
const recordInitializer = inputValueArray
|
|
511
|
-
.map(arg => {
|
|
512
|
-
const fieldType = this.resolveInputFieldType(arg.type);
|
|
513
|
-
const fieldName = convertSafeName(changeCaseAll.pascalCase(this.convertName(arg.name)));
|
|
514
|
-
const csharpFieldType = wrapFieldType(fieldType, fieldType.listType, this.config.listType);
|
|
515
|
-
return `${csharpFieldType} ${fieldName}`;
|
|
516
|
-
})
|
|
517
|
-
.join(', ');
|
|
518
|
-
return `
|
|
519
|
-
#region ${name}
|
|
520
|
-
${classSummary}public record ${convertSafeName(name)}(${recordInitializer})${interfaceImpl} {
|
|
521
|
-
#region members
|
|
522
|
-
${recordMembers}
|
|
523
|
-
#endregion
|
|
524
|
-
}
|
|
525
|
-
#endregion`;
|
|
526
|
-
}
|
|
527
|
-
buildClass(name, description, inputValueArray, interfaces) {
|
|
528
|
-
const classSummary = transformComment(description === null || description === void 0 ? void 0 : description.value);
|
|
529
|
-
const interfaceImpl = interfaces && interfaces.length > 0 ? ` : ${interfaces.map(ntn => ntn.name.value).join(', ')}` : '';
|
|
530
|
-
const classMembers = inputValueArray
|
|
531
|
-
.map(arg => {
|
|
532
|
-
const fieldType = this.resolveInputFieldType(arg.type);
|
|
533
|
-
const fieldHeader = this.getFieldHeader(arg, fieldType);
|
|
534
|
-
const fieldName = convertSafeName(arg.name);
|
|
535
|
-
const csharpFieldType = wrapFieldType(fieldType, fieldType.listType, this.config.listType);
|
|
536
|
-
return fieldHeader + visitorPluginCommon.indent(`public ${csharpFieldType} ${fieldName} { get; set; }`);
|
|
537
|
-
})
|
|
538
|
-
.join('\n\n');
|
|
539
|
-
return `
|
|
540
|
-
#region ${name}
|
|
541
|
-
${classSummary}public class ${convertSafeName(name)}${interfaceImpl} {
|
|
542
|
-
#region members
|
|
543
|
-
${classMembers}
|
|
544
|
-
#endregion
|
|
545
|
-
}
|
|
546
|
-
#endregion`;
|
|
547
|
-
}
|
|
548
|
-
buildInterface(name, description, inputValueArray) {
|
|
549
|
-
const classSummary = transformComment(description === null || description === void 0 ? void 0 : description.value);
|
|
550
|
-
const classMembers = inputValueArray
|
|
551
|
-
.map(arg => {
|
|
552
|
-
const fieldType = this.resolveInputFieldType(arg.type);
|
|
553
|
-
const fieldHeader = this.getFieldHeader(arg, fieldType);
|
|
554
|
-
let fieldName;
|
|
555
|
-
let getterSetter;
|
|
556
|
-
if (this.config.emitRecords) {
|
|
557
|
-
// record
|
|
558
|
-
fieldName = convertSafeName(changeCaseAll.pascalCase(this.convertName(arg.name)));
|
|
559
|
-
getterSetter = '{ get; }';
|
|
560
|
-
}
|
|
561
|
-
else {
|
|
562
|
-
// class
|
|
563
|
-
fieldName = convertSafeName(arg.name);
|
|
564
|
-
getterSetter = '{ get; set; }';
|
|
565
|
-
}
|
|
566
|
-
const csharpFieldType = wrapFieldType(fieldType, fieldType.listType, this.config.listType);
|
|
567
|
-
return fieldHeader + visitorPluginCommon.indent(`${csharpFieldType} ${fieldName} ${getterSetter}`);
|
|
568
|
-
})
|
|
569
|
-
.join('\n\n');
|
|
570
|
-
return `
|
|
571
|
-
${classSummary}public interface ${convertSafeName(name)} {
|
|
572
|
-
${classMembers}
|
|
573
|
-
}`;
|
|
574
|
-
}
|
|
575
|
-
buildInputTransformer(name, description, inputValueArray) {
|
|
576
|
-
const classSummary = transformComment(description === null || description === void 0 ? void 0 : description.value);
|
|
577
|
-
const classMembers = inputValueArray
|
|
578
|
-
.map(arg => {
|
|
579
|
-
const fieldType = this.resolveInputFieldType(arg.type, !!arg.defaultValue);
|
|
580
|
-
const fieldHeader = this.getFieldHeader(arg, fieldType);
|
|
581
|
-
const fieldName = convertSafeName(arg.name);
|
|
582
|
-
const csharpFieldType = wrapFieldType(fieldType, fieldType.listType, this.config.listType);
|
|
583
|
-
return fieldHeader + visitorPluginCommon.indent(`public ${csharpFieldType} ${fieldName} { get; set; }`);
|
|
584
|
-
})
|
|
585
|
-
.join('\n\n');
|
|
586
|
-
return `
|
|
587
|
-
#region ${name}
|
|
588
|
-
${classSummary}public class ${convertSafeName(name)} {
|
|
589
|
-
#region members
|
|
590
|
-
${classMembers}
|
|
591
|
-
#endregion
|
|
592
|
-
|
|
593
|
-
#region methods
|
|
594
|
-
public dynamic GetInputObject()
|
|
595
|
-
{
|
|
596
|
-
IDictionary<string, object> d = new System.Dynamic.ExpandoObject();
|
|
597
|
-
|
|
598
|
-
var properties = GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
|
|
599
|
-
foreach (var propertyInfo in properties)
|
|
600
|
-
{
|
|
601
|
-
var value = propertyInfo.GetValue(this);
|
|
602
|
-
var defaultValue = propertyInfo.PropertyType.IsValueType ? Activator.CreateInstance(propertyInfo.PropertyType) : null;
|
|
603
|
-
${this._parsedConfig.emitJsonAttributes && this.jsonAttributesConfiguration.requiredAttribute != null
|
|
604
|
-
? `
|
|
605
|
-
var requiredProp = propertyInfo.GetCustomAttributes(typeof(${this.jsonAttributesConfiguration.requiredAttribute}Attribute), false).Length > 0;
|
|
606
|
-
`
|
|
607
|
-
: `
|
|
608
|
-
var requiredProp = propertyInfo.GetCustomAttributes(typeof(RequiredAttribute), false).Length > 0;
|
|
609
|
-
`}
|
|
610
|
-
if (requiredProp || value != defaultValue)
|
|
611
|
-
{
|
|
612
|
-
d[propertyInfo.Name] = value;
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
return d;
|
|
616
|
-
}
|
|
617
|
-
#endregion
|
|
618
|
-
}
|
|
619
|
-
#endregion`;
|
|
620
|
-
}
|
|
621
|
-
InputObjectTypeDefinition(node) {
|
|
622
|
-
const name = `${this.convertName(node)}`;
|
|
623
|
-
return this.buildInputTransformer(name, node.description, node.fields);
|
|
624
|
-
}
|
|
625
|
-
ObjectTypeDefinition(node) {
|
|
626
|
-
if (this.config.emitRecords) {
|
|
627
|
-
return this.buildRecord(node.name.value, node.description, node.fields, node.interfaces);
|
|
628
|
-
}
|
|
629
|
-
return this.buildClass(node.name.value, node.description, node.fields, node.interfaces);
|
|
630
|
-
}
|
|
631
|
-
InterfaceTypeDefinition(node) {
|
|
632
|
-
return this.buildInterface(node.name.value, node.description, node.fields);
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
const plugin = async (schema, documents, config) => {
|
|
637
|
-
const visitor = new CSharpResolversVisitor(config, schema);
|
|
638
|
-
const astNode = pluginHelpers.getCachedDocumentNodeFromSchema(schema);
|
|
639
|
-
const visitorResult = pluginHelpers.oldVisit(astNode, { leave: visitor });
|
|
640
|
-
const imports = visitor.getImports();
|
|
641
|
-
const blockContent = visitorResult.definitions.filter(d => typeof d === 'string').join('\n');
|
|
642
|
-
const wrappedBlockContent = visitor.wrapWithClass(blockContent);
|
|
643
|
-
const wrappedContent = visitor.wrapWithNamespace(wrappedBlockContent);
|
|
644
|
-
return [imports, wrappedContent].join('\n');
|
|
645
|
-
};
|
|
646
|
-
|
|
647
|
-
exports.plugin = plugin;
|