@graphql-codegen/c-sharp 4.2.15 → 4.3.0-alpha-bd464a586.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 ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/cjs/index.js ADDED
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.plugin = void 0;
4
+ const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
5
+ const visitor_js_1 = require("./visitor.js");
6
+ const plugin = async (schema, documents, config) => {
7
+ const visitor = new visitor_js_1.CSharpResolversVisitor(config, schema);
8
+ const astNode = (0, plugin_helpers_1.getCachedDocumentNodeFromSchema)(schema);
9
+ const visitorResult = (0, plugin_helpers_1.oldVisit)(astNode, { leave: visitor });
10
+ const imports = visitor.getImports();
11
+ const blockContent = visitorResult.definitions.filter(d => typeof d === 'string').join('\n');
12
+ const wrappedBlockContent = visitor.wrapWithClass(blockContent);
13
+ const wrappedContent = visitor.wrapWithNamespace(wrappedBlockContent);
14
+ return [imports, wrappedContent].join('\n');
15
+ };
16
+ exports.plugin = plugin;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getJsonAttributeSourceConfiguration = exports.JsonAttributesSourceConfiguration = void 0;
4
+ function unsupportedSource(attributesSource) {
5
+ throw new Error(`Unsupported JSON attributes source: ${attributesSource}`);
6
+ }
7
+ class JsonAttributesSourceConfiguration {
8
+ constructor(namespace, propertyAttribute, requiredAttribute) {
9
+ this.namespace = namespace;
10
+ this.propertyAttribute = propertyAttribute;
11
+ this.requiredAttribute = requiredAttribute;
12
+ }
13
+ }
14
+ exports.JsonAttributesSourceConfiguration = JsonAttributesSourceConfiguration;
15
+ const newtonsoftConfiguration = new JsonAttributesSourceConfiguration('Newtonsoft.Json', 'JsonProperty', 'JsonRequired');
16
+ // System.Text.Json does not have support of `JsonRequired` alternative (as for .NET 5)
17
+ const systemTextJsonConfiguration = new JsonAttributesSourceConfiguration('System.Text.Json.Serialization', 'JsonPropertyName', null);
18
+ function getJsonAttributeSourceConfiguration(attributesSource) {
19
+ switch (attributesSource) {
20
+ case 'Newtonsoft.Json':
21
+ return newtonsoftConfiguration;
22
+ case 'System.Text.Json':
23
+ return systemTextJsonConfiguration;
24
+ }
25
+ unsupportedSource(attributesSource);
26
+ }
27
+ exports.getJsonAttributeSourceConfiguration = getJsonAttributeSourceConfiguration;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
package/cjs/visitor.js ADDED
@@ -0,0 +1,325 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CSharpResolversVisitor = void 0;
4
+ const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
5
+ const graphql_1 = require("graphql");
6
+ const c_sharp_common_1 = require("@graphql-codegen/c-sharp-common");
7
+ const change_case_all_1 = require("change-case-all");
8
+ const json_attributes_js_1 = require("./json-attributes.js");
9
+ class CSharpResolversVisitor extends visitor_plugin_common_1.BaseVisitor {
10
+ constructor(rawConfig, _schema) {
11
+ var _a;
12
+ super(rawConfig, {
13
+ enumValues: rawConfig.enumValues || {},
14
+ listType: rawConfig.listType || 'List',
15
+ namespaceName: rawConfig.namespaceName || 'GraphQLCodeGen',
16
+ className: rawConfig.className || 'Types',
17
+ emitRecords: rawConfig.emitRecords || false,
18
+ emitJsonAttributes: (_a = rawConfig.emitJsonAttributes) !== null && _a !== void 0 ? _a : true,
19
+ jsonAttributesSource: rawConfig.jsonAttributesSource || 'Newtonsoft.Json',
20
+ scalars: (0, visitor_plugin_common_1.buildScalarsFromConfig)(_schema, rawConfig, c_sharp_common_1.C_SHARP_SCALARS),
21
+ });
22
+ this._schema = _schema;
23
+ if (this._parsedConfig.emitJsonAttributes) {
24
+ this.jsonAttributesConfiguration = (0, json_attributes_js_1.getJsonAttributeSourceConfiguration)(this._parsedConfig.jsonAttributesSource);
25
+ }
26
+ }
27
+ getImports() {
28
+ const allImports = ['System', 'System.Collections.Generic', 'System.ComponentModel.DataAnnotations'];
29
+ if (this._parsedConfig.emitJsonAttributes) {
30
+ const jsonAttributesNamespace = this.jsonAttributesConfiguration.namespace;
31
+ allImports.push(jsonAttributesNamespace);
32
+ }
33
+ return allImports.map(i => `using ${i};`).join('\n') + '\n';
34
+ }
35
+ wrapWithNamespace(content) {
36
+ return new c_sharp_common_1.CSharpDeclarationBlock()
37
+ .asKind('namespace')
38
+ .withName(this.config.namespaceName)
39
+ .withBlock((0, visitor_plugin_common_1.indentMultiline)(content)).string;
40
+ }
41
+ wrapWithClass(content) {
42
+ return new c_sharp_common_1.CSharpDeclarationBlock()
43
+ .access('public')
44
+ .asKind('class')
45
+ .withName((0, c_sharp_common_1.convertSafeName)(this.config.className))
46
+ .withBlock((0, visitor_plugin_common_1.indentMultiline)(content)).string;
47
+ }
48
+ getEnumValue(enumName, enumOption) {
49
+ if (this.config.enumValues[enumName] &&
50
+ typeof this.config.enumValues[enumName] === 'object' &&
51
+ this.config.enumValues[enumName][enumOption]) {
52
+ return this.config.enumValues[enumName][enumOption];
53
+ }
54
+ return enumOption;
55
+ }
56
+ EnumValueDefinition(node) {
57
+ return (enumName) => {
58
+ const enumHeader = this.getFieldHeader(node);
59
+ const enumOption = (0, c_sharp_common_1.convertSafeName)(node.name);
60
+ return enumHeader + (0, visitor_plugin_common_1.indent)(this.getEnumValue(enumName, enumOption));
61
+ };
62
+ }
63
+ EnumTypeDefinition(node) {
64
+ const enumName = this.convertName(node.name);
65
+ const enumValues = node.values.map(enumValue => enumValue(node.name.value)).join(',\n');
66
+ const enumBlock = [enumValues].join('\n');
67
+ return new c_sharp_common_1.CSharpDeclarationBlock()
68
+ .access('public')
69
+ .asKind('enum')
70
+ .withComment(node.description)
71
+ .withName(enumName)
72
+ .withBlock(enumBlock).string;
73
+ }
74
+ getFieldHeader(node, fieldType) {
75
+ var _a;
76
+ const attributes = [];
77
+ const commentText = (0, c_sharp_common_1.transformComment)((_a = node.description) === null || _a === void 0 ? void 0 : _a.value);
78
+ const deprecationDirective = node.directives.find(v => { var _a; return ((_a = v.name) === null || _a === void 0 ? void 0 : _a.value) === 'deprecated'; });
79
+ if (deprecationDirective) {
80
+ const deprecationReason = this.getDeprecationReason(deprecationDirective);
81
+ attributes.push(`[Obsolete("${deprecationReason}")]`);
82
+ }
83
+ if (this._parsedConfig.emitJsonAttributes && node.kind === graphql_1.Kind.FIELD_DEFINITION) {
84
+ const jsonPropertyAttribute = this.jsonAttributesConfiguration.propertyAttribute;
85
+ if (jsonPropertyAttribute != null) {
86
+ attributes.push(`[${jsonPropertyAttribute}("${node.name.value}")]`);
87
+ }
88
+ }
89
+ if (node.kind === graphql_1.Kind.INPUT_VALUE_DEFINITION && fieldType.isOuterTypeRequired) {
90
+ // Should be always inserted for required fields to use in `GetInputObject()` when JSON attributes are not used
91
+ // or there are no JSON attributes in selected attribute source that provides `JsonRequired` alternative
92
+ attributes.push('[Required]');
93
+ if (this._parsedConfig.emitJsonAttributes) {
94
+ const jsonRequiredAttribute = this.jsonAttributesConfiguration.requiredAttribute;
95
+ if (jsonRequiredAttribute != null) {
96
+ attributes.push(`[${jsonRequiredAttribute}]`);
97
+ }
98
+ }
99
+ }
100
+ if (commentText || attributes.length > 0) {
101
+ const summary = commentText ? (0, visitor_plugin_common_1.indentMultiline)(commentText.trimRight()) + '\n' : '';
102
+ const attributeLines = attributes.length > 0
103
+ ? attributes
104
+ .map(attr => (0, visitor_plugin_common_1.indent)(attr))
105
+ .concat('')
106
+ .join('\n')
107
+ : '';
108
+ return summary + attributeLines;
109
+ }
110
+ return '';
111
+ }
112
+ getDeprecationReason(directive) {
113
+ if (directive.name.value !== 'deprecated') {
114
+ return '';
115
+ }
116
+ const hasArguments = directive.arguments.length > 0;
117
+ let reason = 'Field no longer supported';
118
+ if (hasArguments && directive.arguments[0].value.kind === graphql_1.Kind.STRING) {
119
+ reason = directive.arguments[0].value.value;
120
+ }
121
+ return reason;
122
+ }
123
+ resolveInputFieldType(typeNode, hasDefaultValue = false) {
124
+ const innerType = (0, visitor_plugin_common_1.getBaseTypeNode)(typeNode);
125
+ const schemaType = this._schema.getType(innerType.name.value);
126
+ const listType = (0, c_sharp_common_1.getListTypeField)(typeNode);
127
+ const required = (0, c_sharp_common_1.getListInnerTypeNode)(typeNode).kind === graphql_1.Kind.NON_NULL_TYPE;
128
+ let result = null;
129
+ if ((0, graphql_1.isScalarType)(schemaType)) {
130
+ if (this.scalars[schemaType.name]) {
131
+ const baseType = this.scalars[schemaType.name];
132
+ result = new c_sharp_common_1.CSharpFieldType({
133
+ baseType: {
134
+ type: baseType,
135
+ required,
136
+ valueType: (0, c_sharp_common_1.isValueType)(baseType),
137
+ },
138
+ listType,
139
+ });
140
+ }
141
+ else {
142
+ result = new c_sharp_common_1.CSharpFieldType({
143
+ baseType: {
144
+ type: 'object',
145
+ required,
146
+ valueType: false,
147
+ },
148
+ listType,
149
+ });
150
+ }
151
+ }
152
+ else if ((0, graphql_1.isInputObjectType)(schemaType)) {
153
+ result = new c_sharp_common_1.CSharpFieldType({
154
+ baseType: {
155
+ type: `${this.convertName(schemaType.name)}`,
156
+ required,
157
+ valueType: false,
158
+ },
159
+ listType,
160
+ });
161
+ }
162
+ else if ((0, graphql_1.isEnumType)(schemaType)) {
163
+ result = new c_sharp_common_1.CSharpFieldType({
164
+ baseType: {
165
+ type: this.convertName(schemaType.name),
166
+ required,
167
+ valueType: true,
168
+ },
169
+ listType,
170
+ });
171
+ }
172
+ else {
173
+ result = new c_sharp_common_1.CSharpFieldType({
174
+ baseType: {
175
+ type: `${schemaType.name}`,
176
+ required,
177
+ valueType: false,
178
+ },
179
+ listType,
180
+ });
181
+ }
182
+ if (hasDefaultValue) {
183
+ // Required field is optional when default value specified, see #4273
184
+ (result.listType || result.baseType).required = false;
185
+ }
186
+ return result;
187
+ }
188
+ buildRecord(name, description, inputValueArray, interfaces) {
189
+ const classSummary = (0, c_sharp_common_1.transformComment)(description === null || description === void 0 ? void 0 : description.value);
190
+ const interfaceImpl = interfaces && interfaces.length > 0 ? ` : ${interfaces.map(ntn => ntn.name.value).join(', ')}` : '';
191
+ const recordMembers = inputValueArray
192
+ .map(arg => {
193
+ const fieldType = this.resolveInputFieldType(arg.type);
194
+ const fieldHeader = this.getFieldHeader(arg, fieldType);
195
+ const fieldName = (0, c_sharp_common_1.convertSafeName)((0, change_case_all_1.pascalCase)(this.convertName(arg.name)));
196
+ const csharpFieldType = (0, c_sharp_common_1.wrapFieldType)(fieldType, fieldType.listType, this.config.listType);
197
+ return fieldHeader + (0, visitor_plugin_common_1.indent)(`public ${csharpFieldType} ${fieldName} { get; init; } = ${fieldName};`);
198
+ })
199
+ .join('\n\n');
200
+ const recordInitializer = inputValueArray
201
+ .map(arg => {
202
+ const fieldType = this.resolveInputFieldType(arg.type);
203
+ const fieldName = (0, c_sharp_common_1.convertSafeName)((0, change_case_all_1.pascalCase)(this.convertName(arg.name)));
204
+ const csharpFieldType = (0, c_sharp_common_1.wrapFieldType)(fieldType, fieldType.listType, this.config.listType);
205
+ return `${csharpFieldType} ${fieldName}`;
206
+ })
207
+ .join(', ');
208
+ return `
209
+ #region ${name}
210
+ ${classSummary}public record ${(0, c_sharp_common_1.convertSafeName)(name)}(${recordInitializer})${interfaceImpl} {
211
+ #region members
212
+ ${recordMembers}
213
+ #endregion
214
+ }
215
+ #endregion`;
216
+ }
217
+ buildClass(name, description, inputValueArray, interfaces) {
218
+ const classSummary = (0, c_sharp_common_1.transformComment)(description === null || description === void 0 ? void 0 : description.value);
219
+ const interfaceImpl = interfaces && interfaces.length > 0 ? ` : ${interfaces.map(ntn => ntn.name.value).join(', ')}` : '';
220
+ const classMembers = inputValueArray
221
+ .map(arg => {
222
+ const fieldType = this.resolveInputFieldType(arg.type);
223
+ const fieldHeader = this.getFieldHeader(arg, fieldType);
224
+ const fieldName = (0, c_sharp_common_1.convertSafeName)(arg.name);
225
+ const csharpFieldType = (0, c_sharp_common_1.wrapFieldType)(fieldType, fieldType.listType, this.config.listType);
226
+ return fieldHeader + (0, visitor_plugin_common_1.indent)(`public ${csharpFieldType} ${fieldName} { get; set; }`);
227
+ })
228
+ .join('\n\n');
229
+ return `
230
+ #region ${name}
231
+ ${classSummary}public class ${(0, c_sharp_common_1.convertSafeName)(name)}${interfaceImpl} {
232
+ #region members
233
+ ${classMembers}
234
+ #endregion
235
+ }
236
+ #endregion`;
237
+ }
238
+ buildInterface(name, description, inputValueArray) {
239
+ const classSummary = (0, c_sharp_common_1.transformComment)(description === null || description === void 0 ? void 0 : description.value);
240
+ const classMembers = inputValueArray
241
+ .map(arg => {
242
+ const fieldType = this.resolveInputFieldType(arg.type);
243
+ const fieldHeader = this.getFieldHeader(arg, fieldType);
244
+ let fieldName;
245
+ let getterSetter;
246
+ if (this.config.emitRecords) {
247
+ // record
248
+ fieldName = (0, c_sharp_common_1.convertSafeName)((0, change_case_all_1.pascalCase)(this.convertName(arg.name)));
249
+ getterSetter = '{ get; }';
250
+ }
251
+ else {
252
+ // class
253
+ fieldName = (0, c_sharp_common_1.convertSafeName)(arg.name);
254
+ getterSetter = '{ get; set; }';
255
+ }
256
+ const csharpFieldType = (0, c_sharp_common_1.wrapFieldType)(fieldType, fieldType.listType, this.config.listType);
257
+ return fieldHeader + (0, visitor_plugin_common_1.indent)(`${csharpFieldType} ${fieldName} ${getterSetter}`);
258
+ })
259
+ .join('\n\n');
260
+ return `
261
+ ${classSummary}public interface ${(0, c_sharp_common_1.convertSafeName)(name)} {
262
+ ${classMembers}
263
+ }`;
264
+ }
265
+ buildInputTransformer(name, description, inputValueArray) {
266
+ const classSummary = (0, c_sharp_common_1.transformComment)(description === null || description === void 0 ? void 0 : description.value);
267
+ const classMembers = inputValueArray
268
+ .map(arg => {
269
+ const fieldType = this.resolveInputFieldType(arg.type, !!arg.defaultValue);
270
+ const fieldHeader = this.getFieldHeader(arg, fieldType);
271
+ const fieldName = (0, c_sharp_common_1.convertSafeName)(arg.name);
272
+ const csharpFieldType = (0, c_sharp_common_1.wrapFieldType)(fieldType, fieldType.listType, this.config.listType);
273
+ return fieldHeader + (0, visitor_plugin_common_1.indent)(`public ${csharpFieldType} ${fieldName} { get; set; }`);
274
+ })
275
+ .join('\n\n');
276
+ return `
277
+ #region ${name}
278
+ ${classSummary}public class ${(0, c_sharp_common_1.convertSafeName)(name)} {
279
+ #region members
280
+ ${classMembers}
281
+ #endregion
282
+
283
+ #region methods
284
+ public dynamic GetInputObject()
285
+ {
286
+ IDictionary<string, object> d = new System.Dynamic.ExpandoObject();
287
+
288
+ var properties = GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
289
+ foreach (var propertyInfo in properties)
290
+ {
291
+ var value = propertyInfo.GetValue(this);
292
+ var defaultValue = propertyInfo.PropertyType.IsValueType ? Activator.CreateInstance(propertyInfo.PropertyType) : null;
293
+ ${this._parsedConfig.emitJsonAttributes && this.jsonAttributesConfiguration.requiredAttribute != null
294
+ ? `
295
+ var requiredProp = propertyInfo.GetCustomAttributes(typeof(${this.jsonAttributesConfiguration.requiredAttribute}Attribute), false).Length > 0;
296
+ `
297
+ : `
298
+ var requiredProp = propertyInfo.GetCustomAttributes(typeof(RequiredAttribute), false).Length > 0;
299
+ `}
300
+ if (requiredProp || value != defaultValue)
301
+ {
302
+ d[propertyInfo.Name] = value;
303
+ }
304
+ }
305
+ return d;
306
+ }
307
+ #endregion
308
+ }
309
+ #endregion`;
310
+ }
311
+ InputObjectTypeDefinition(node) {
312
+ const name = `${this.convertName(node)}`;
313
+ return this.buildInputTransformer(name, node.description, node.fields);
314
+ }
315
+ ObjectTypeDefinition(node) {
316
+ if (this.config.emitRecords) {
317
+ return this.buildRecord(node.name.value, node.description, node.fields, node.interfaces);
318
+ }
319
+ return this.buildClass(node.name.value, node.description, node.fields, node.interfaces);
320
+ }
321
+ InterfaceTypeDefinition(node) {
322
+ return this.buildInterface(node.name.value, node.description, node.fields);
323
+ }
324
+ }
325
+ exports.CSharpResolversVisitor = CSharpResolversVisitor;
package/esm/config.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/esm/index.js ADDED
@@ -0,0 +1,12 @@
1
+ import { getCachedDocumentNodeFromSchema, oldVisit } from '@graphql-codegen/plugin-helpers';
2
+ import { CSharpResolversVisitor } from './visitor.js';
3
+ export const plugin = async (schema, documents, config) => {
4
+ const visitor = new CSharpResolversVisitor(config, schema);
5
+ const astNode = getCachedDocumentNodeFromSchema(schema);
6
+ const visitorResult = oldVisit(astNode, { leave: visitor });
7
+ const imports = visitor.getImports();
8
+ const blockContent = visitorResult.definitions.filter(d => typeof d === 'string').join('\n');
9
+ const wrappedBlockContent = visitor.wrapWithClass(blockContent);
10
+ const wrappedContent = visitor.wrapWithNamespace(wrappedBlockContent);
11
+ return [imports, wrappedContent].join('\n');
12
+ };
@@ -0,0 +1,22 @@
1
+ function unsupportedSource(attributesSource) {
2
+ throw new Error(`Unsupported JSON attributes source: ${attributesSource}`);
3
+ }
4
+ export class JsonAttributesSourceConfiguration {
5
+ constructor(namespace, propertyAttribute, requiredAttribute) {
6
+ this.namespace = namespace;
7
+ this.propertyAttribute = propertyAttribute;
8
+ this.requiredAttribute = requiredAttribute;
9
+ }
10
+ }
11
+ const newtonsoftConfiguration = new JsonAttributesSourceConfiguration('Newtonsoft.Json', 'JsonProperty', 'JsonRequired');
12
+ // System.Text.Json does not have support of `JsonRequired` alternative (as for .NET 5)
13
+ const systemTextJsonConfiguration = new JsonAttributesSourceConfiguration('System.Text.Json.Serialization', 'JsonPropertyName', null);
14
+ export function getJsonAttributeSourceConfiguration(attributesSource) {
15
+ switch (attributesSource) {
16
+ case 'Newtonsoft.Json':
17
+ return newtonsoftConfiguration;
18
+ case 'System.Text.Json':
19
+ return systemTextJsonConfiguration;
20
+ }
21
+ unsupportedSource(attributesSource);
22
+ }