dotansimha / graphql-code-generator-community

MIT License
119 stars 156 forks source link

C# Plugin Should Allow Generating Partial Classes #799

Open mariusmuntean opened 3 months ago

mariusmuntean commented 3 months ago

Which packages are impacted by your issue?

@graphql-codegen/c-sharp

Describe the bug

The C# plugin should be configurable to allow generating C# partial classes. See docs: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/partial-type

This gives C# developers more flexibility to enhance the generated types and it also allows to fix the current shortcoming of the plugin regarding GraphQL union types, which aren't supported.

If a generated class A is marked with the partial keyword, the developer can add another file next to the generated one and enhance the class A, e.g. make it inherit from an interface to fix the unsupported union type or add helper methods.

Class generated by the plugin GeneratedTypes.cs

internal partial class GeneratedTypes
{
    internal partial class A
    {
        public int Idx { get; set; }
    }
}

Class manually added by the dev GeneratedTypes.Partial.cs

internal partial class GeneratedTypes
{
    internal interface IUnionType
    {
    }

    internal partial class A : IUnionType
    {
    }
}

Proposal: extend the current plugin config with an option called partialTypes. Possible values by type:

Your Example Website or App

https://codesandbox.io/p/devbox/github/mariusmuntean/graphql-code-generator-issue-sandbox-template-csharp-partial-types/tree/main/

Steps to Reproduce the Bug or Issue

No repro needed, just an enhancement to allow more flexibility.

Expected behavior

As a C# developer I expect to be able to extend the generated types with new functionality by using the C# partial class feature, but the generated classes aren't marked as partial.

Screenshots or Videos

No response

Platform

Codegen Config File

No response

Additional context

No response

mariusmuntean commented 3 months ago

I'm volunteering to work on this issue as I believe that many would profit from it.