From 09665ce866faf22dc1425062f402ebe5ae30fd3f Mon Sep 17 00:00:00 2001 From: Marius Muntean Date: Tue, 30 Jul 2024 22:03:05 +0200 Subject: [PATCH 01/10] added test for namingConvention=pascalCase --- .../c-sharp/c-sharp/test/c-sharp.spec.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts b/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts index 431123637..1a0b95cc3 100644 --- a/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts +++ b/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts @@ -283,6 +283,31 @@ describe('C#', () => { const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result).toContain('public class User {'); }); + it('Should generate a C# class with pascal case property names for type', async () => { + const schema = buildSchema(/* GraphQL */ ` + type User { + id: Int + chosenName: String + } + `); + const result = await plugin( + schema, + [], + { + namingConvention: 'change-case-all#pascalCase', + }, + { + outputFile: '', + }, + ); + expect(result).toBeSimilarStringTo(` + [JsonProperty("id")] + public int? Id { get; set; } + + [JsonProperty("chosenName")] + public string ChosenName { get; set; } + `); + }); it('Should generate C# record for type', async () => { const schema = buildSchema(/* GraphQL */ ` type User { @@ -339,6 +364,7 @@ describe('C#', () => { `); const config: CSharpResolversPluginRawConfig = { jsonAttributesSource: source, + namingConvention: 'change-case-all#pascalCase', }; const result = await plugin(schema, [], config, { outputFile: '' }); const jsonConfig = getJsonAttributeSourceConfiguration(source); From b20f2b8d31f2acd317f87cdc4c2bf661ad5510ac Mon Sep 17 00:00:00 2001 From: Marius Muntean Date: Wed, 31 Jul 2024 23:26:23 +0200 Subject: [PATCH 02/10] added support for choosing the field naming convention (camel case or pascal case) for a class' properties --- .../plugins/c-sharp/c-sharp/src/config.ts | 16 +++++++++++++ .../c-sharp/c-sharp/src/field-naming.ts | 23 +++++++++++++++++++ .../plugins/c-sharp/c-sharp/src/visitor.ts | 10 +++++--- .../c-sharp/c-sharp/test/c-sharp.spec.ts | 2 +- 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 packages/plugins/c-sharp/c-sharp/src/field-naming.ts diff --git a/packages/plugins/c-sharp/c-sharp/src/config.ts b/packages/plugins/c-sharp/c-sharp/src/config.ts index de4ac3060..280c69b88 100644 --- a/packages/plugins/c-sharp/c-sharp/src/config.ts +++ b/packages/plugins/c-sharp/c-sharp/src/config.ts @@ -110,4 +110,20 @@ export interface CSharpResolversPluginRawConfig extends RawConfig { * ``` */ jsonAttributesSource?: JsonAttributesSource; + + /** + * @default GraphQLCodeGen + * @description Allow you to customize the naming convention for interface/class/record fields. + * + * @exampleMarkdown + * ```yaml + * generates: + * src/main/c-sharp/my-org/my-app/MyTypes.cs: + * plugins: + * - c-sharp + * config: + * fieldNameConvention: pascalCase + * ``` + */ + fieldNameConvention?: 'camelCase' | 'pascalCase'; } diff --git a/packages/plugins/c-sharp/c-sharp/src/field-naming.ts b/packages/plugins/c-sharp/c-sharp/src/field-naming.ts new file mode 100644 index 000000000..03a304fff --- /dev/null +++ b/packages/plugins/c-sharp/c-sharp/src/field-naming.ts @@ -0,0 +1,23 @@ +import { camelCase, pascalCase } from 'change-case-all'; +import { NameNode } from 'graphql'; +import { CSharpResolversPluginRawConfig } from './config'; + +type FieldNamingFunctionInput = string | NameNode; + +export type FieldNamingFunction = (nameOrNameNode: FieldNamingFunctionInput) => string; + +export function getFieldNamingFunction( + rawConfig: CSharpResolversPluginRawConfig, +): FieldNamingFunction { + switch (rawConfig.fieldNameConvention) { + case 'camelCase': + return (input: FieldNamingFunctionInput) => + camelCase(typeof input === 'string' ? input : input.value); + case 'pascalCase': + return (input: FieldNamingFunctionInput) => + pascalCase(typeof input === 'string' ? input : input.value); + default: + return (input: FieldNamingFunctionInput) => + camelCase(typeof input === 'string' ? input : input.value); + } +} diff --git a/packages/plugins/c-sharp/c-sharp/src/visitor.ts b/packages/plugins/c-sharp/c-sharp/src/visitor.ts index 74688575b..be4f6c309 100644 --- a/packages/plugins/c-sharp/c-sharp/src/visitor.ts +++ b/packages/plugins/c-sharp/c-sharp/src/visitor.ts @@ -38,6 +38,7 @@ import { ParsedConfig, } from '@graphql-codegen/visitor-plugin-common'; import { CSharpResolversPluginRawConfig } from './config.js'; +import { FieldNamingFunction, getFieldNamingFunction } from './field-naming.js'; import { getJsonAttributeSourceConfiguration, JsonAttributesSource, @@ -52,6 +53,7 @@ export interface CSharpResolverParsedConfig extends ParsedConfig { emitRecords: boolean; emitJsonAttributes: boolean; jsonAttributesSource: JsonAttributesSource; + fieldNamingFunction: FieldNamingFunction; } export class CSharpResolversVisitor extends BaseVisitor< @@ -70,6 +72,7 @@ export class CSharpResolversVisitor extends BaseVisitor< emitJsonAttributes: rawConfig.emitJsonAttributes ?? true, jsonAttributesSource: rawConfig.jsonAttributesSource || 'Newtonsoft.Json', scalars: buildScalarsFromConfig(_schema, rawConfig, C_SHARP_SCALARS), + fieldNamingFunction: getFieldNamingFunction(rawConfig), }); if (this._parsedConfig.emitJsonAttributes) { @@ -324,10 +327,10 @@ ${recordMembers} const classMembers = inputValueArray .map(arg => { const fieldType = this.resolveInputFieldType(arg.type); - const fieldHeader = this.getFieldHeader(arg, fieldType); - const fieldName = convertSafeName(arg.name); + const fieldAttribute = this.getFieldHeader(arg, fieldType); + const fieldName = convertSafeName(this._parsedConfig.fieldNamingFunction(arg.name)); const csharpFieldType = wrapFieldType(fieldType, fieldType.listType, this.config.listType); - return fieldHeader + indent(`public ${csharpFieldType} ${fieldName} { get; set; }`); + return fieldAttribute + indent(`public ${csharpFieldType} ${fieldName} { get; set; }`); }) .join('\n\n'); @@ -360,6 +363,7 @@ ${classMembers} fieldName = convertSafeName(pascalCase(this.convertName(arg.name))); getterSetter = '{ get; }'; } else { + // ToDo: maybe just use pascalCase here, like the field names for records are created // class fieldName = convertSafeName(arg.name); getterSetter = '{ get; set; }'; diff --git a/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts b/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts index 1a0b95cc3..69a81202b 100644 --- a/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts +++ b/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts @@ -294,7 +294,7 @@ describe('C#', () => { schema, [], { - namingConvention: 'change-case-all#pascalCase', + fieldNameConvention: 'pascalCase', }, { outputFile: '', From b3f9d86dc62529f320846bf4c2c4b5414d466ec6 Mon Sep 17 00:00:00 2001 From: Marius Muntean Date: Thu, 1 Aug 2024 22:36:34 +0200 Subject: [PATCH 03/10] Renamed fieldNamingConvention to memberNamingConvention to reflect that it actually applies to all members, not only fields --- .../plugins/c-sharp/c-sharp/src/config.ts | 7 +++--- .../c-sharp/c-sharp/src/field-naming.ts | 23 ------------------- .../c-sharp/c-sharp/src/member-naming.ts | 21 +++++++++++++++++ .../plugins/c-sharp/c-sharp/src/visitor.ts | 8 +++---- .../c-sharp/c-sharp/test/c-sharp.spec.ts | 2 +- 5 files changed, 30 insertions(+), 31 deletions(-) delete mode 100644 packages/plugins/c-sharp/c-sharp/src/field-naming.ts create mode 100644 packages/plugins/c-sharp/c-sharp/src/member-naming.ts diff --git a/packages/plugins/c-sharp/c-sharp/src/config.ts b/packages/plugins/c-sharp/c-sharp/src/config.ts index 280c69b88..b003452d4 100644 --- a/packages/plugins/c-sharp/c-sharp/src/config.ts +++ b/packages/plugins/c-sharp/c-sharp/src/config.ts @@ -112,8 +112,9 @@ export interface CSharpResolversPluginRawConfig extends RawConfig { jsonAttributesSource?: JsonAttributesSource; /** - * @default GraphQLCodeGen - * @description Allow you to customize the naming convention for interface/class/record fields. + * @default camelCase + * Supported: camelCase, pascalCase + * @description Allow you to customize the naming convention for interface/class/record members. * * @exampleMarkdown * ```yaml @@ -125,5 +126,5 @@ export interface CSharpResolversPluginRawConfig extends RawConfig { * fieldNameConvention: pascalCase * ``` */ - fieldNameConvention?: 'camelCase' | 'pascalCase'; + memberNameConvention?: 'camelCase' | 'pascalCase'; } diff --git a/packages/plugins/c-sharp/c-sharp/src/field-naming.ts b/packages/plugins/c-sharp/c-sharp/src/field-naming.ts deleted file mode 100644 index 03a304fff..000000000 --- a/packages/plugins/c-sharp/c-sharp/src/field-naming.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { camelCase, pascalCase } from 'change-case-all'; -import { NameNode } from 'graphql'; -import { CSharpResolversPluginRawConfig } from './config'; - -type FieldNamingFunctionInput = string | NameNode; - -export type FieldNamingFunction = (nameOrNameNode: FieldNamingFunctionInput) => string; - -export function getFieldNamingFunction( - rawConfig: CSharpResolversPluginRawConfig, -): FieldNamingFunction { - switch (rawConfig.fieldNameConvention) { - case 'camelCase': - return (input: FieldNamingFunctionInput) => - camelCase(typeof input === 'string' ? input : input.value); - case 'pascalCase': - return (input: FieldNamingFunctionInput) => - pascalCase(typeof input === 'string' ? input : input.value); - default: - return (input: FieldNamingFunctionInput) => - camelCase(typeof input === 'string' ? input : input.value); - } -} diff --git a/packages/plugins/c-sharp/c-sharp/src/member-naming.ts b/packages/plugins/c-sharp/c-sharp/src/member-naming.ts new file mode 100644 index 000000000..88754870a --- /dev/null +++ b/packages/plugins/c-sharp/c-sharp/src/member-naming.ts @@ -0,0 +1,21 @@ +import { camelCase, pascalCase } from 'change-case-all'; +import { NameNode } from 'graphql'; +import { CSharpResolversPluginRawConfig } from './config'; + +type MemberNamingFunctionInput = string | NameNode; + +export type MemberNamingFn = (nameOrNameNode: MemberNamingFunctionInput) => string; + +export function getMemberNamingFunction(rawConfig: CSharpResolversPluginRawConfig): MemberNamingFn { + switch (rawConfig.memberNameConvention) { + case 'camelCase': + return (input: MemberNamingFunctionInput) => + camelCase(typeof input === 'string' ? input : input.value); + case 'pascalCase': + return (input: MemberNamingFunctionInput) => + pascalCase(typeof input === 'string' ? input : input.value); + default: + return (input: MemberNamingFunctionInput) => + camelCase(typeof input === 'string' ? input : input.value); + } +} diff --git a/packages/plugins/c-sharp/c-sharp/src/visitor.ts b/packages/plugins/c-sharp/c-sharp/src/visitor.ts index be4f6c309..f00f32e9d 100644 --- a/packages/plugins/c-sharp/c-sharp/src/visitor.ts +++ b/packages/plugins/c-sharp/c-sharp/src/visitor.ts @@ -38,12 +38,12 @@ import { ParsedConfig, } from '@graphql-codegen/visitor-plugin-common'; import { CSharpResolversPluginRawConfig } from './config.js'; -import { FieldNamingFunction, getFieldNamingFunction } from './field-naming.js'; import { getJsonAttributeSourceConfiguration, JsonAttributesSource, JsonAttributesSourceConfiguration, } from './json-attributes.js'; +import { getMemberNamingFunction, MemberNamingFn } from './member-naming.js'; export interface CSharpResolverParsedConfig extends ParsedConfig { namespaceName: string; @@ -53,7 +53,7 @@ export interface CSharpResolverParsedConfig extends ParsedConfig { emitRecords: boolean; emitJsonAttributes: boolean; jsonAttributesSource: JsonAttributesSource; - fieldNamingFunction: FieldNamingFunction; + memberNamingFunction: MemberNamingFn; } export class CSharpResolversVisitor extends BaseVisitor< @@ -72,7 +72,7 @@ export class CSharpResolversVisitor extends BaseVisitor< emitJsonAttributes: rawConfig.emitJsonAttributes ?? true, jsonAttributesSource: rawConfig.jsonAttributesSource || 'Newtonsoft.Json', scalars: buildScalarsFromConfig(_schema, rawConfig, C_SHARP_SCALARS), - fieldNamingFunction: getFieldNamingFunction(rawConfig), + memberNamingFunction: getMemberNamingFunction(rawConfig), }); if (this._parsedConfig.emitJsonAttributes) { @@ -328,7 +328,7 @@ ${recordMembers} .map(arg => { const fieldType = this.resolveInputFieldType(arg.type); const fieldAttribute = this.getFieldHeader(arg, fieldType); - const fieldName = convertSafeName(this._parsedConfig.fieldNamingFunction(arg.name)); + const fieldName = convertSafeName(this._parsedConfig.memberNamingFunction(arg.name)); const csharpFieldType = wrapFieldType(fieldType, fieldType.listType, this.config.listType); return fieldAttribute + indent(`public ${csharpFieldType} ${fieldName} { get; set; }`); }) diff --git a/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts b/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts index 69a81202b..696445179 100644 --- a/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts +++ b/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts @@ -294,7 +294,7 @@ describe('C#', () => { schema, [], { - fieldNameConvention: 'pascalCase', + memberNameConvention: 'pascalCase', }, { outputFile: '', From e88eb04eadd05c4f53cf03647ffba892d2c3c78b Mon Sep 17 00:00:00 2001 From: Marius Muntean Date: Thu, 1 Aug 2024 22:38:07 +0200 Subject: [PATCH 04/10] added a test for camelCase naming class properties --- .../c-sharp/c-sharp/test/c-sharp.spec.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts b/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts index 696445179..c7c215a9c 100644 --- a/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts +++ b/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts @@ -283,6 +283,31 @@ describe('C#', () => { const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result).toContain('public class User {'); }); + it('Should generate a C# class with camel case property names for type', async () => { + const schema = buildSchema(/* GraphQL */ ` + type User { + id: Int + chosenName: String + } + `); + const result = await plugin( + schema, + [], + { + memberNameConvention: 'camelCase', + }, + { + outputFile: '', + }, + ); + expect(result).toBeSimilarStringTo(` + [JsonProperty("id")] + public int? id { get; set; } + + [JsonProperty("chosenName")] + public string chosenName { get; set; } + `); + }); it('Should generate a C# class with pascal case property names for type', async () => { const schema = buildSchema(/* GraphQL */ ` type User { From 7972e77bc68a6ad683f50e937fda4668977beaac Mon Sep 17 00:00:00 2001 From: Marius Muntean Date: Thu, 1 Aug 2024 22:49:43 +0200 Subject: [PATCH 05/10] the casing of record member names can be controlled too --- .../plugins/c-sharp/c-sharp/src/visitor.ts | 8 ++++-- .../c-sharp/c-sharp/test/c-sharp.spec.ts | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/plugins/c-sharp/c-sharp/src/visitor.ts b/packages/plugins/c-sharp/c-sharp/src/visitor.ts index f00f32e9d..69011cde1 100644 --- a/packages/plugins/c-sharp/c-sharp/src/visitor.ts +++ b/packages/plugins/c-sharp/c-sharp/src/visitor.ts @@ -287,7 +287,9 @@ export class CSharpResolversVisitor extends BaseVisitor< .map(arg => { const fieldType = this.resolveInputFieldType(arg.type); const fieldHeader = this.getFieldHeader(arg, fieldType); - const fieldName = convertSafeName(pascalCase(this.convertName(arg.name))); + const fieldName = convertSafeName( + this._parsedConfig.memberNamingFunction(this.convertName(arg.name)), + ); const csharpFieldType = wrapFieldType(fieldType, fieldType.listType, this.config.listType); return ( fieldHeader + @@ -298,7 +300,9 @@ export class CSharpResolversVisitor extends BaseVisitor< const recordInitializer = inputValueArray .map(arg => { const fieldType = this.resolveInputFieldType(arg.type); - const fieldName = convertSafeName(pascalCase(this.convertName(arg.name))); + const fieldName = convertSafeName( + this._parsedConfig.memberNamingFunction(this.convertName(arg.name)), + ); const csharpFieldType = wrapFieldType(fieldType, fieldType.listType, this.config.listType); return `${csharpFieldType} ${fieldName}`; }) diff --git a/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts b/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts index c7c215a9c..53c5ab10e 100644 --- a/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts +++ b/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts @@ -340,8 +340,36 @@ describe('C#', () => { } `); const result = await plugin(schema, [], { emitRecords: true }, { outputFile: '' }); + expect(result).toContain('public record User(int? id) {'); + }); + it('Should generate C# record with pascal case property names', async () => { + const schema = buildSchema(/* GraphQL */ ` + type User { + id: Int + } + `); + const result = await plugin( + schema, + [], + { emitRecords: true, memberNameConvention: 'pascalCase' }, + { outputFile: '' }, + ); expect(result).toContain('public record User(int? Id) {'); }); + it('Should generate C# record with camel case property names', async () => { + const schema = buildSchema(/* GraphQL */ ` + type User { + id: Int + } + `); + const result = await plugin( + schema, + [], + { emitRecords: true, memberNameConvention: 'camelCase' }, + { outputFile: '' }, + ); + expect(result).toContain('public record User(int? id) {'); + }); it('Should wrap generated classes in Type class', async () => { const schema = buildSchema(/* GraphQL */ ` type User { From afa62bf7be8637d9443f826337201435db59b2b5 Mon Sep 17 00:00:00 2001 From: Marius Muntean Date: Thu, 1 Aug 2024 22:55:39 +0200 Subject: [PATCH 06/10] control the casing of input type class properties --- .../plugins/c-sharp/c-sharp/src/visitor.ts | 2 +- .../c-sharp/c-sharp/test/c-sharp.spec.ts | 28 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/packages/plugins/c-sharp/c-sharp/src/visitor.ts b/packages/plugins/c-sharp/c-sharp/src/visitor.ts index 69011cde1..e5e3aa554 100644 --- a/packages/plugins/c-sharp/c-sharp/src/visitor.ts +++ b/packages/plugins/c-sharp/c-sharp/src/visitor.ts @@ -395,7 +395,7 @@ ${classMembers} .map(arg => { const fieldType = this.resolveInputFieldType(arg.type, !!arg.defaultValue); const fieldHeader = this.getFieldHeader(arg, fieldType); - const fieldName = convertSafeName(arg.name); + const fieldName = convertSafeName(this._parsedConfig.memberNamingFunction(arg.name)); const csharpFieldType = wrapFieldType(fieldType, fieldType.listType, this.config.listType); return fieldHeader + indent(`public ${csharpFieldType} ${fieldName} { get; set; }`); }) diff --git a/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts b/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts index 53c5ab10e..e7332ac0f 100644 --- a/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts +++ b/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts @@ -218,20 +218,44 @@ describe('C#', () => { expect(result).toContain('public class UserInput {'); }); - it('Should generate properties for input type fields', async () => { + it('Should generate camelCase properties for input type fields', async () => { const schema = buildSchema(/* GraphQL */ ` input UserInput { id: Int email: String } `); - const result = await plugin(schema, [], {}, { outputFile: '' }); + const result = await plugin( + schema, + [], + { memberNameConvention: 'camelCase' }, + { outputFile: '' }, + ); expect(result).toBeSimilarStringTo(` public int? id { get; set; } public string email { get; set; } `); }); + it('Should generate pascalCase properties for input type fields', async () => { + const schema = buildSchema(/* GraphQL */ ` + input UserInput { + id: Int + email: String + } + `); + const result = await plugin( + schema, + [], + { memberNameConvention: 'pascalCase' }, + { outputFile: '' }, + ); + expect(result).toBeSimilarStringTo(` + public int? Id { get; set; } + public string Email { get; set; } + `); + }); + it('Should generate C# method for creating input object', async () => { const schema = buildSchema(/* GraphQL */ ` input UserInput { From 5e3b7128acad36003c9e55e49d12773bde9f961a Mon Sep 17 00:00:00 2001 From: Marius Muntean Date: Thu, 1 Aug 2024 23:15:55 +0200 Subject: [PATCH 07/10] the casing of interface member names can be controlled too --- .../plugins/c-sharp/c-sharp/src/visitor.ts | 8 ++-- .../c-sharp/c-sharp/test/c-sharp.spec.ts | 37 +++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/packages/plugins/c-sharp/c-sharp/src/visitor.ts b/packages/plugins/c-sharp/c-sharp/src/visitor.ts index e5e3aa554..cdbe4d7b3 100644 --- a/packages/plugins/c-sharp/c-sharp/src/visitor.ts +++ b/packages/plugins/c-sharp/c-sharp/src/visitor.ts @@ -364,12 +364,12 @@ ${classMembers} if (this.config.emitRecords) { // record - fieldName = convertSafeName(pascalCase(this.convertName(arg.name))); + fieldName = convertSafeName( + this._parsedConfig.memberNamingFunction(this.convertName(arg.name)), + ); getterSetter = '{ get; }'; } else { - // ToDo: maybe just use pascalCase here, like the field names for records are created - // class - fieldName = convertSafeName(arg.name); + fieldName = convertSafeName(this._parsedConfig.memberNamingFunction(arg.name)); getterSetter = '{ get; set; }'; } diff --git a/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts b/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts index e7332ac0f..1aeb2132c 100644 --- a/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts +++ b/packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts @@ -547,6 +547,43 @@ describe('C#', () => { expect(result).toContain('public interface Node {'); }); + it('Should generate C# interface with pascalCase properties', async () => { + const schema = buildSchema(/* GraphQL */ ` + interface Node { + id: ID! + } + `); + const result = await plugin( + schema, + [], + { memberNameConvention: 'pascalCase' }, + { outputFile: '' }, + ); + + expect(result).toBeSimilarStringTo(`public interface Node { + [JsonProperty("id")] + string Id { get; set; } + }`); + }); + it('Should generate C# interface with camelCase properties', async () => { + const schema = buildSchema(/* GraphQL */ ` + interface Node { + id: ID! + } + `); + const result = await plugin( + schema, + [], + { memberNameConvention: 'camelCase' }, + { outputFile: '' }, + ); + + expect(result).toBeSimilarStringTo(`public interface Node { + [JsonProperty("id")] + string id { get; set; } + }`); + }); + it('Should generate C# class that implements given interfaces', async () => { const schema = buildSchema(/* GraphQL */ ` interface INode { From 4fe8fafcaf1515c86729be799fe6262eea864a89 Mon Sep 17 00:00:00 2001 From: Marius Muntean Date: Mon, 5 Aug 2024 21:30:10 +0200 Subject: [PATCH 08/10] typo --- packages/plugins/c-sharp/c-sharp/src/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugins/c-sharp/c-sharp/src/config.ts b/packages/plugins/c-sharp/c-sharp/src/config.ts index b003452d4..893a39e54 100644 --- a/packages/plugins/c-sharp/c-sharp/src/config.ts +++ b/packages/plugins/c-sharp/c-sharp/src/config.ts @@ -114,7 +114,7 @@ export interface CSharpResolversPluginRawConfig extends RawConfig { /** * @default camelCase * Supported: camelCase, pascalCase - * @description Allow you to customize the naming convention for interface/class/record members. + * @description Allows you to customize the naming convention for interface/class/record members. * * @exampleMarkdown * ```yaml From 3d16d09a3e885db7ce8462b50eeefd56957fb7fb Mon Sep 17 00:00:00 2001 From: Marius Muntean Date: Mon, 5 Aug 2024 21:33:13 +0200 Subject: [PATCH 09/10] removed unused import --- packages/plugins/c-sharp/c-sharp/src/visitor.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/plugins/c-sharp/c-sharp/src/visitor.ts b/packages/plugins/c-sharp/c-sharp/src/visitor.ts index cdbe4d7b3..648150a20 100644 --- a/packages/plugins/c-sharp/c-sharp/src/visitor.ts +++ b/packages/plugins/c-sharp/c-sharp/src/visitor.ts @@ -1,4 +1,3 @@ -import { pascalCase } from 'change-case-all'; import { DirectiveNode, EnumTypeDefinitionNode, From f044f3cb3461c6bacce3c166b61e08b7a75a3465 Mon Sep 17 00:00:00 2001 From: Marius Muntean Date: Mon, 5 Aug 2024 21:50:04 +0200 Subject: [PATCH 10/10] added changeset --- .changeset/strong-turkeys-sneeze.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/strong-turkeys-sneeze.md diff --git a/.changeset/strong-turkeys-sneeze.md b/.changeset/strong-turkeys-sneeze.md new file mode 100644 index 000000000..d87191ebc --- /dev/null +++ b/.changeset/strong-turkeys-sneeze.md @@ -0,0 +1,6 @@ +--- +'@graphql-codegen/c-sharp': Minor +--- + +Added `memberNameConvention` which allows you to customize the naming convention for +interface/class/record members.