@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.
@@ -1,318 +1,9 @@
1
- import { getCachedDocumentNodeFromSchema, oldVisit } from '@graphql-codegen/plugin-helpers';
2
- import { indent, indentMultiline, BaseVisitor, buildScalarsFromConfig, getBaseTypeNode } from '@graphql-codegen/visitor-plugin-common';
3
- import { Kind, isScalarType, isInputObjectType, isEnumType } from 'graphql';
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
- const C_SHARP_SCALARS = {
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, {
@@ -628,16 +319,3 @@ ${this._parsedConfig.emitJsonAttributes && this.jsonAttributesConfiguration.requ
628
319
  return this.buildInterface(node.name.value, node.description, node.fields);
629
320
  }
630
321
  }
631
-
632
- const plugin = async (schema, documents, config) => {
633
- const visitor = new CSharpResolversVisitor(config, schema);
634
- const astNode = getCachedDocumentNodeFromSchema(schema);
635
- const visitorResult = oldVisit(astNode, { leave: visitor });
636
- const imports = visitor.getImports();
637
- const blockContent = visitorResult.definitions.filter(d => typeof d === 'string').join('\n');
638
- const wrappedBlockContent = visitor.wrapWithClass(blockContent);
639
- const wrappedContent = visitor.wrapWithNamespace(wrappedBlockContent);
640
- return [imports, wrappedContent].join('\n');
641
- };
642
-
643
- export { plugin };
package/package.json CHANGED
@@ -1,35 +1,43 @@
1
1
  {
2
2
  "name": "@graphql-codegen/c-sharp",
3
- "version": "4.2.15",
3
+ "version": "4.3.0-alpha-29eb1293b.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.4.0",
11
- "@graphql-codegen/visitor-plugin-common": "2.10.0",
12
- "change-case-all": "1.0.14",
10
+ "@graphql-codegen/plugin-helpers": "^2.5.0-alpha-29eb1293b.0",
11
+ "@graphql-codegen/visitor-plugin-common": "^2.11.0-alpha-29eb1293b.0",
12
+ "@graphql-codegen/c-sharp-common": "0.1.0-alpha-29eb1293b.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.mjs",
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": "./index.js",
28
- "import": "./index.mjs"
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
  */
@@ -1,3 +1,3 @@
1
1
  import { PluginFunction } from '@graphql-codegen/plugin-helpers';
2
- import { CSharpResolversPluginRawConfig } from './config';
2
+ import { CSharpResolversPluginRawConfig } from './config.js';
3
3
  export declare const plugin: PluginFunction<CSharpResolversPluginRawConfig>;
@@ -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 '../../common/common';
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;