Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# Plugin Should Allow Generating Partial Classes #799

Open
mariusmuntean opened this issue Jul 29, 2024 · 1 comment
Open

C# Plugin Should Allow Generating Partial Classes #799

mariusmuntean opened this issue Jul 29, 2024 · 1 comment

Comments

@mariusmuntean
Copy link
Contributor

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:

  • string: none (turns feature off), classes (marks all classes as partial), record (marks all record as partial), interfaces (marks all interfaces as partial), all (marks all classes, records and interfaces as partial).
  • predicate function (typeName: string) => bool: decides for every type individually if it should be marked as partial
  • set: a JavaScript set containing the types to mark as partial

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

  • OS: all
  • @graphql-codegen/c-sharp version(s): 5.0.0

Codegen Config File

No response

Additional context

No response

@mariusmuntean
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant