@graphql-codegen/c-sharp 4.2.14 → 4.3.0-alpha-2fbcdb6d3.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} +9 -333
- 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 -649
|
@@ -1,318 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { BaseVisitor, indentMultiline, indent, getBaseTypeNode, buildScalarsFromConfig, } from '@graphql-codegen/visitor-plugin-common';
|
|
2
|
+
import { Kind, isScalarType, isInputObjectType, isEnumType, } from 'graphql';
|
|
3
|
+
import { C_SHARP_SCALARS, CSharpDeclarationBlock, transformComment, isValueType, getListInnerTypeNode, CSharpFieldType, convertSafeName, wrapFieldType, getListTypeField, } from '@graphql-codegen/c-sharp-common';
|
|
4
4
|
import { pascalCase } from 'change-case-all';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
ID: 'string',
|
|
8
|
-
String: 'string',
|
|
9
|
-
Boolean: 'bool',
|
|
10
|
-
Int: 'int',
|
|
11
|
-
Float: 'double',
|
|
12
|
-
Date: 'DateTime',
|
|
13
|
-
};
|
|
14
|
-
const csharpValueTypes = [
|
|
15
|
-
'bool',
|
|
16
|
-
'byte',
|
|
17
|
-
'sbyte',
|
|
18
|
-
'char',
|
|
19
|
-
'decimal',
|
|
20
|
-
'double',
|
|
21
|
-
'float',
|
|
22
|
-
'int',
|
|
23
|
-
'uint',
|
|
24
|
-
'long',
|
|
25
|
-
'ulong',
|
|
26
|
-
'short',
|
|
27
|
-
'ushort',
|
|
28
|
-
'DateTime',
|
|
29
|
-
];
|
|
30
|
-
|
|
31
|
-
// This file is bundled and inlined.
|
|
32
|
-
function transformComment(comment, indentLevel = 0) {
|
|
33
|
-
if (!comment) {
|
|
34
|
-
return '';
|
|
35
|
-
}
|
|
36
|
-
if (isStringValueNode(comment)) {
|
|
37
|
-
comment = comment.value;
|
|
38
|
-
}
|
|
39
|
-
comment = comment.trimStart().split('*/').join('*\\/');
|
|
40
|
-
let lines = comment.split('\n');
|
|
41
|
-
lines = ['/// <summary>', ...lines.map(line => `/// ${line}`), '/// </summary>'];
|
|
42
|
-
return lines
|
|
43
|
-
.map(line => indent(line, indentLevel))
|
|
44
|
-
.concat('')
|
|
45
|
-
.join('\n');
|
|
46
|
-
}
|
|
47
|
-
function isStringValueNode(node) {
|
|
48
|
-
return node && typeof node === 'object' && node.kind === Kind.STRING;
|
|
49
|
-
}
|
|
50
|
-
function isValueType(type) {
|
|
51
|
-
// Limitation: only checks the list of known built in value types
|
|
52
|
-
// Eg .NET types and struct types won't be detected correctly
|
|
53
|
-
return csharpValueTypes.includes(type);
|
|
54
|
-
}
|
|
55
|
-
function getListTypeField(typeNode) {
|
|
56
|
-
if (typeNode.kind === Kind.LIST_TYPE) {
|
|
57
|
-
return {
|
|
58
|
-
required: false,
|
|
59
|
-
type: getListTypeField(typeNode.type),
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
if (typeNode.kind === Kind.NON_NULL_TYPE && typeNode.type.kind === Kind.LIST_TYPE) {
|
|
63
|
-
return Object.assign(getListTypeField(typeNode.type), {
|
|
64
|
-
required: true,
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
if (typeNode.kind === Kind.NON_NULL_TYPE) {
|
|
68
|
-
return getListTypeField(typeNode.type);
|
|
69
|
-
}
|
|
70
|
-
return undefined;
|
|
71
|
-
}
|
|
72
|
-
function getListInnerTypeNode(typeNode) {
|
|
73
|
-
if (typeNode.kind === Kind.LIST_TYPE) {
|
|
74
|
-
return getListInnerTypeNode(typeNode.type);
|
|
75
|
-
}
|
|
76
|
-
if (typeNode.kind === Kind.NON_NULL_TYPE && typeNode.type.kind === Kind.LIST_TYPE) {
|
|
77
|
-
return getListInnerTypeNode(typeNode.type);
|
|
78
|
-
}
|
|
79
|
-
return typeNode;
|
|
80
|
-
}
|
|
81
|
-
function wrapFieldType(fieldType, listTypeField, listType = 'IEnumerable') {
|
|
82
|
-
if (listTypeField) {
|
|
83
|
-
const innerType = wrapFieldType(fieldType, listTypeField.type, listType);
|
|
84
|
-
return `${listType}<${innerType}>`;
|
|
85
|
-
}
|
|
86
|
-
return fieldType.innerTypeName;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// This file is bundled and inlined.
|
|
90
|
-
class CSharpDeclarationBlock {
|
|
91
|
-
constructor() {
|
|
92
|
-
this._name = null;
|
|
93
|
-
this._extendStr = [];
|
|
94
|
-
this._implementsStr = [];
|
|
95
|
-
this._kind = null;
|
|
96
|
-
this._access = 'public';
|
|
97
|
-
this._final = false;
|
|
98
|
-
this._static = false;
|
|
99
|
-
this._block = null;
|
|
100
|
-
this._comment = null;
|
|
101
|
-
this._nestedClasses = [];
|
|
102
|
-
}
|
|
103
|
-
nestedClass(nstCls) {
|
|
104
|
-
this._nestedClasses.push(nstCls);
|
|
105
|
-
return this;
|
|
106
|
-
}
|
|
107
|
-
access(access) {
|
|
108
|
-
this._access = access;
|
|
109
|
-
return this;
|
|
110
|
-
}
|
|
111
|
-
asKind(kind) {
|
|
112
|
-
this._kind = kind;
|
|
113
|
-
return this;
|
|
114
|
-
}
|
|
115
|
-
final() {
|
|
116
|
-
this._final = true;
|
|
117
|
-
return this;
|
|
118
|
-
}
|
|
119
|
-
static() {
|
|
120
|
-
this._static = true;
|
|
121
|
-
return this;
|
|
122
|
-
}
|
|
123
|
-
withComment(comment) {
|
|
124
|
-
if (comment) {
|
|
125
|
-
this._comment = transformComment(comment, 1);
|
|
126
|
-
}
|
|
127
|
-
return this;
|
|
128
|
-
}
|
|
129
|
-
withBlock(block) {
|
|
130
|
-
this._block = block;
|
|
131
|
-
return this;
|
|
132
|
-
}
|
|
133
|
-
extends(extendStr) {
|
|
134
|
-
this._extendStr = extendStr;
|
|
135
|
-
return this;
|
|
136
|
-
}
|
|
137
|
-
implements(implementsStr) {
|
|
138
|
-
this._implementsStr = implementsStr;
|
|
139
|
-
return this;
|
|
140
|
-
}
|
|
141
|
-
withName(name) {
|
|
142
|
-
this._name = typeof name === 'object' ? name.value : name;
|
|
143
|
-
return this;
|
|
144
|
-
}
|
|
145
|
-
get string() {
|
|
146
|
-
let result = '';
|
|
147
|
-
if (this._kind) {
|
|
148
|
-
let name = '';
|
|
149
|
-
if (this._name) {
|
|
150
|
-
name = this._name;
|
|
151
|
-
}
|
|
152
|
-
if (this._kind === 'namespace') {
|
|
153
|
-
result += `${this._kind} ${name} `;
|
|
154
|
-
}
|
|
155
|
-
else {
|
|
156
|
-
let extendStr = '';
|
|
157
|
-
let implementsStr = '';
|
|
158
|
-
const final = this._final ? ' final' : '';
|
|
159
|
-
const isStatic = this._static ? ' static' : '';
|
|
160
|
-
if (this._extendStr.length > 0) {
|
|
161
|
-
extendStr = ` : ${this._extendStr.join(', ')}`;
|
|
162
|
-
}
|
|
163
|
-
if (this._implementsStr.length > 0) {
|
|
164
|
-
implementsStr = ` : ${this._implementsStr.join(', ')}`;
|
|
165
|
-
}
|
|
166
|
-
result += `${this._access}${isStatic}${final} ${this._kind} ${name}${extendStr}${implementsStr} `;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
const nestedClasses = this._nestedClasses.length
|
|
170
|
-
? this._nestedClasses.map(c => indentMultiline(c.string)).join('\n\n')
|
|
171
|
-
: null;
|
|
172
|
-
const before = '{';
|
|
173
|
-
const after = '}';
|
|
174
|
-
const block = [before, nestedClasses, this._block, after].filter(f => f).join('\n');
|
|
175
|
-
result += block;
|
|
176
|
-
return (this._comment ? this._comment : '') + result + '\n';
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
class CSharpFieldType {
|
|
181
|
-
constructor(fieldType) {
|
|
182
|
-
Object.assign(this, fieldType);
|
|
183
|
-
}
|
|
184
|
-
get innerTypeName() {
|
|
185
|
-
const nullable = this.baseType.valueType && !this.baseType.required ? '?' : '';
|
|
186
|
-
return `${this.baseType.type}${nullable}`;
|
|
187
|
-
}
|
|
188
|
-
get isOuterTypeRequired() {
|
|
189
|
-
return this.listType ? this.listType.required : this.baseType.required;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* C# keywords
|
|
195
|
-
* https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/
|
|
196
|
-
*/
|
|
197
|
-
const csharpKeywords = new Set([
|
|
198
|
-
'abstract',
|
|
199
|
-
'as',
|
|
200
|
-
'base',
|
|
201
|
-
'bool',
|
|
202
|
-
'break',
|
|
203
|
-
'byte',
|
|
204
|
-
'case',
|
|
205
|
-
'catch',
|
|
206
|
-
'char',
|
|
207
|
-
'checked',
|
|
208
|
-
'class',
|
|
209
|
-
'const',
|
|
210
|
-
'continue',
|
|
211
|
-
'decimal',
|
|
212
|
-
'default',
|
|
213
|
-
'delegate',
|
|
214
|
-
'do',
|
|
215
|
-
'double',
|
|
216
|
-
'else',
|
|
217
|
-
'enum',
|
|
218
|
-
'event',
|
|
219
|
-
'explicit',
|
|
220
|
-
'extern',
|
|
221
|
-
'false',
|
|
222
|
-
'finally',
|
|
223
|
-
'fixed',
|
|
224
|
-
'float',
|
|
225
|
-
'for',
|
|
226
|
-
'foreach',
|
|
227
|
-
'goto',
|
|
228
|
-
'if',
|
|
229
|
-
'implicit',
|
|
230
|
-
'in',
|
|
231
|
-
'int',
|
|
232
|
-
'interface',
|
|
233
|
-
'internal',
|
|
234
|
-
'is',
|
|
235
|
-
'lock',
|
|
236
|
-
'long',
|
|
237
|
-
'namespace',
|
|
238
|
-
'new',
|
|
239
|
-
'null',
|
|
240
|
-
'object',
|
|
241
|
-
'operator',
|
|
242
|
-
'out',
|
|
243
|
-
'override',
|
|
244
|
-
'params',
|
|
245
|
-
'private',
|
|
246
|
-
'protected',
|
|
247
|
-
'public',
|
|
248
|
-
'readonly',
|
|
249
|
-
'record',
|
|
250
|
-
'ref',
|
|
251
|
-
'return',
|
|
252
|
-
'sbyte',
|
|
253
|
-
'sealed',
|
|
254
|
-
'short',
|
|
255
|
-
'sizeof',
|
|
256
|
-
'stackalloc',
|
|
257
|
-
'static',
|
|
258
|
-
'string',
|
|
259
|
-
'struct',
|
|
260
|
-
'switch',
|
|
261
|
-
'this',
|
|
262
|
-
'throw',
|
|
263
|
-
'true',
|
|
264
|
-
'try',
|
|
265
|
-
'typeof',
|
|
266
|
-
'uint',
|
|
267
|
-
'ulong',
|
|
268
|
-
'unchecked',
|
|
269
|
-
'unsafe',
|
|
270
|
-
'ushort',
|
|
271
|
-
'using',
|
|
272
|
-
'virtual',
|
|
273
|
-
'void',
|
|
274
|
-
'volatile',
|
|
275
|
-
'while',
|
|
276
|
-
]);
|
|
277
|
-
/**
|
|
278
|
-
* Checks name against list of keywords. If it is, will prefix value with @
|
|
279
|
-
*
|
|
280
|
-
* Note:
|
|
281
|
-
* This class should first invoke the convertName from base-visitor to convert the string or node
|
|
282
|
-
* value according the naming configuration, eg upper or lower case. Then resulting string checked
|
|
283
|
-
* against the list or keywords.
|
|
284
|
-
* However the generated C# code is not yet able to handle fields that are in a different case so
|
|
285
|
-
* the invocation of convertName is omitted purposely.
|
|
286
|
-
*/
|
|
287
|
-
function convertSafeName(node) {
|
|
288
|
-
const name = typeof node === 'string' ? node : node.value;
|
|
289
|
-
return csharpKeywords.has(name) ? `@${name}` : name;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
function unsupportedSource(attributesSource) {
|
|
293
|
-
throw new Error(`Unsupported JSON attributes source: ${attributesSource}`);
|
|
294
|
-
}
|
|
295
|
-
class JsonAttributesSourceConfiguration {
|
|
296
|
-
constructor(namespace, propertyAttribute, requiredAttribute) {
|
|
297
|
-
this.namespace = namespace;
|
|
298
|
-
this.propertyAttribute = propertyAttribute;
|
|
299
|
-
this.requiredAttribute = requiredAttribute;
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
const newtonsoftConfiguration = new JsonAttributesSourceConfiguration('Newtonsoft.Json', 'JsonProperty', 'JsonRequired');
|
|
303
|
-
// System.Text.Json does not have support of `JsonRequired` alternative (as for .NET 5)
|
|
304
|
-
const systemTextJsonConfiguration = new JsonAttributesSourceConfiguration('System.Text.Json.Serialization', 'JsonPropertyName', null);
|
|
305
|
-
function getJsonAttributeSourceConfiguration(attributesSource) {
|
|
306
|
-
switch (attributesSource) {
|
|
307
|
-
case 'Newtonsoft.Json':
|
|
308
|
-
return newtonsoftConfiguration;
|
|
309
|
-
case 'System.Text.Json':
|
|
310
|
-
return systemTextJsonConfiguration;
|
|
311
|
-
}
|
|
312
|
-
unsupportedSource(attributesSource);
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
class CSharpResolversVisitor extends BaseVisitor {
|
|
5
|
+
import { getJsonAttributeSourceConfiguration, } from './json-attributes.js';
|
|
6
|
+
export class CSharpResolversVisitor extends BaseVisitor {
|
|
316
7
|
constructor(rawConfig, _schema) {
|
|
317
8
|
var _a;
|
|
318
9
|
super(rawConfig, {
|
|
@@ -386,12 +77,10 @@ class CSharpResolversVisitor extends BaseVisitor {
|
|
|
386
77
|
const deprecationReason = this.getDeprecationReason(deprecationDirective);
|
|
387
78
|
attributes.push(`[Obsolete("${deprecationReason}")]`);
|
|
388
79
|
}
|
|
389
|
-
if (this._parsedConfig.emitJsonAttributes) {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
attributes.push(`[${jsonPropertyAttribute}("${node.name.value}")]`);
|
|
394
|
-
}
|
|
80
|
+
if (this._parsedConfig.emitJsonAttributes && node.kind === Kind.FIELD_DEFINITION) {
|
|
81
|
+
const jsonPropertyAttribute = this.jsonAttributesConfiguration.propertyAttribute;
|
|
82
|
+
if (jsonPropertyAttribute != null) {
|
|
83
|
+
attributes.push(`[${jsonPropertyAttribute}("${node.name.value}")]`);
|
|
395
84
|
}
|
|
396
85
|
}
|
|
397
86
|
if (node.kind === Kind.INPUT_VALUE_DEFINITION && fieldType.isOuterTypeRequired) {
|
|
@@ -630,16 +319,3 @@ ${this._parsedConfig.emitJsonAttributes && this.jsonAttributesConfiguration.requ
|
|
|
630
319
|
return this.buildInterface(node.name.value, node.description, node.fields);
|
|
631
320
|
}
|
|
632
321
|
}
|
|
633
|
-
|
|
634
|
-
const plugin = async (schema, documents, config) => {
|
|
635
|
-
const visitor = new CSharpResolversVisitor(config, schema);
|
|
636
|
-
const astNode = getCachedDocumentNodeFromSchema(schema);
|
|
637
|
-
const visitorResult = oldVisit(astNode, { leave: visitor });
|
|
638
|
-
const imports = visitor.getImports();
|
|
639
|
-
const blockContent = visitorResult.definitions.filter(d => typeof d === 'string').join('\n');
|
|
640
|
-
const wrappedBlockContent = visitor.wrapWithClass(blockContent);
|
|
641
|
-
const wrappedContent = visitor.wrapWithNamespace(wrappedBlockContent);
|
|
642
|
-
return [imports, wrappedContent].join('\n');
|
|
643
|
-
};
|
|
644
|
-
|
|
645
|
-
export { plugin };
|
package/package.json
CHANGED
|
@@ -1,35 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/c-sharp",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0-alpha-2fbcdb6d3.0",
|
|
4
4
|
"description": "GraphQL Code Generator plugin for generating CSharp code based on a GraphQL schema",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"peerDependencies": {
|
|
7
7
|
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@graphql-codegen/plugin-helpers": "^2.
|
|
11
|
-
"@graphql-codegen/visitor-plugin-common": "2.
|
|
12
|
-
"
|
|
10
|
+
"@graphql-codegen/plugin-helpers": "^2.5.0-alpha-2fbcdb6d3.0",
|
|
11
|
+
"@graphql-codegen/visitor-plugin-common": "^2.11.0-alpha-2fbcdb6d3.0",
|
|
12
|
+
"@graphql-codegen/c-sharp-common": "0.1.0-alpha-2fbcdb6d3.0",
|
|
13
13
|
"tslib": "~2.4.0",
|
|
14
|
-
"unixify": "^1.0.0"
|
|
14
|
+
"unixify": "^1.0.0",
|
|
15
|
+
"change-case-all": "1.0.14"
|
|
15
16
|
},
|
|
16
17
|
"repository": "git@github.com:dotansimha/graphql-code-generator.git",
|
|
17
18
|
"license": "MIT",
|
|
18
|
-
"main": "index.js",
|
|
19
|
-
"module": "index.
|
|
20
|
-
"typings": "index.d.ts",
|
|
19
|
+
"main": "cjs/index.js",
|
|
20
|
+
"module": "esm/index.js",
|
|
21
|
+
"typings": "typings/index.d.ts",
|
|
21
22
|
"typescript": {
|
|
22
|
-
"definition": "index.d.ts"
|
|
23
|
+
"definition": "typings/index.d.ts"
|
|
23
24
|
},
|
|
25
|
+
"type": "module",
|
|
24
26
|
"exports": {
|
|
25
|
-
"./package.json": "./package.json",
|
|
26
27
|
".": {
|
|
27
|
-
"require":
|
|
28
|
-
|
|
28
|
+
"require": {
|
|
29
|
+
"types": "./typings/index.d.ts",
|
|
30
|
+
"default": "./cjs/index.js"
|
|
31
|
+
},
|
|
32
|
+
"import": {
|
|
33
|
+
"types": "./typings/index.d.ts",
|
|
34
|
+
"default": "./esm/index.js"
|
|
35
|
+
},
|
|
36
|
+
"default": {
|
|
37
|
+
"types": "./typings/index.d.ts",
|
|
38
|
+
"default": "./esm/index.js"
|
|
39
|
+
}
|
|
29
40
|
},
|
|
30
|
-
"
|
|
31
|
-
"require": "./*.js",
|
|
32
|
-
"import": "./*.mjs"
|
|
33
|
-
}
|
|
41
|
+
"./package.json": "./package.json"
|
|
34
42
|
}
|
|
35
|
-
}
|
|
43
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { RawConfig, EnumValuesMap } from '@graphql-codegen/visitor-plugin-common';
|
|
2
|
-
import { JsonAttributesSource } from './json-attributes';
|
|
2
|
+
import { JsonAttributesSource } from './json-attributes.js';
|
|
3
3
|
/**
|
|
4
4
|
* @description This plugin generates C# `class` identifier for your schema types.
|
|
5
5
|
*/
|
|
File without changes
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ParsedConfig, BaseVisitor, EnumValuesMap } from '@graphql-codegen/visitor-plugin-common';
|
|
2
|
-
import { CSharpResolversPluginRawConfig } from './config';
|
|
2
|
+
import { CSharpResolversPluginRawConfig } from './config.js';
|
|
3
3
|
import { GraphQLSchema, EnumTypeDefinitionNode, EnumValueDefinitionNode, InterfaceTypeDefinitionNode, InputObjectTypeDefinitionNode, ObjectTypeDefinitionNode, FieldDefinitionNode, InputValueDefinitionNode, TypeNode, DirectiveNode, StringValueNode, NamedTypeNode } from 'graphql';
|
|
4
|
-
import { CSharpFieldType } from '
|
|
5
|
-
import { JsonAttributesSource } from './json-attributes';
|
|
4
|
+
import { CSharpFieldType } from '@graphql-codegen/c-sharp-common';
|
|
5
|
+
import { JsonAttributesSource } from './json-attributes.js';
|
|
6
6
|
export interface CSharpResolverParsedConfig extends ParsedConfig {
|
|
7
7
|
namespaceName: string;
|
|
8
8
|
className: string;
|