ChilliCream / graphql-platform

Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Banana Cake Pop the awesome Monaco based GraphQL IDE.
https://chillicream.com
MIT License
5.24k stars 744 forks source link

Enums get analyzed as types automatically, but also their members aren't renamed prior to said analysis #6003

Open AntonC9018 opened 1 year ago

AntonC9018 commented 1 year ago

Is there an existing issue for this?

Product

Hot Chocolate

Describe the bug

This issue is in part related to this one.

The problem manifests itself in my case in that HotChocolate fails to initialize my schema, because the original names of enum fields are invalid by its standards.

Steps to reproduce

I somewhat inflate the issue here by naming the fields and the enum type in Russian, using Cyrillic letters, but at the same time providing an adequate HotChocolate EnumType wrapper that renames them:

public enum Статус
{
    Активный,
    Завершенный,
}

public class StatusType : EnumType<Статус>
{
    protected override void Configure(IEnumTypeDescriptor<Статус> descriptor)
    {
        descriptor.Name("Status");
        descriptor.Value(Статус.Активный).Name("ACTIVE");
        descriptor.Value(Статус.Завершенный).Name("COMPLETED");
    }
}

// ...

builder.Services.AddEndpointsApiExplorer();
{
    var graph = builder.Services.AddGraphQLServer();
    graph.InitializeOnStartup();
    graph.AddQueryType<QueryType>();
    graph.AddType<StatusType>();
    graph.AddType<Thing>();
    graph.ModifyOptions(o =>
    {
        o.DefaultBindingBehavior = BindingBehavior.Explicit;
    });
}

See the entire setup here.

In contrast, configuration like this works fine with normal types, regardless of whether the property names are Cyrillic or not, given you rename them. See an example here

Relevant log output

The error happens because the system ends up checking if the enum names in the original enum definition are valid GraphQL symbol names.

System.ArgumentException: The specified name is not a valid GraphQL name. (Parameter 'value')
   at HotChocolate.Utilities.NameUtils.EnsureGraphQLName(String name, String argumentName)
   at HotChocolate.Types.Descriptors.Definitions.DefinitionBase.set_Name(String value)
   at HotChocolate.Types.Descriptors.EnumValueDescriptor..ctor(IDescriptorContext context, Object runtimeValue)
   at HotChocolate.Types.Descriptors.EnumValueDescriptor.New(IDescriptorContext context, Object value)
   at HotChocolate.Types.Descriptors.EnumTypeDescriptor.Value[T](T value)
   at HotChocolate.Types.Descriptors.EnumTypeDescriptor`1.Value(T value)
   at efcore_transactions.StatusType.Configure(IEnumTypeDescriptor`1 descriptor) in E:\Coding\work\efcore_transactions\GraphQL.cs:line 14
   at HotChocolate.Types.EnumType`1.CreateDefinition(ITypeDiscoveryContext context)
   at HotChocolate.Types.TypeSystemObjectBase`1.Initialize(ITypeDiscoveryContext context)
   at HotChocolate.Configuration.TypeRegistrar.InitializeType(TypeSystemObjectBase typeSystemObject, String scope, Boolean isInferred)

Additional Context?

I realize that the existence of Cyrillic names for symbols might seem unrealistic, but I think fixing this would be valuable.

For example, I'm working on a code base where a programmer accidentally wrote the Russian 'С' instead of the English 'C' in the name of an enum value, which ended up producing that incomprehensible error and threw me off the loop for some time.

Though improving the error message would be valuable, the problem with enums described in the linked issue should take priority, I think.

Version

13.0.5

nesherhh commented 1 year ago

Also if EnumType used with T that overrides ToString() and returns text that is invalid GraphQL name.