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

Using DefaultValueAttribute on enum input type field gives Int in schema on F# #7040

Open
cmeeren opened this issue Apr 5, 2024 · 4 comments
Labels

Comments

@cmeeren
Copy link
Contributor

cmeeren commented Apr 5, 2024

Product

Hot Chocolate

Version

14.0.0-p.85

Link to minimal reproduction

See zip below

Steps to reproduce

Repro solution: HotChocolateRepro.zip

Repro code for reference:

open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.DependencyInjection
open Microsoft.Extensions.Hosting

type MyEnum =
    | Value1 = 0
    | Value2 = 1

type MyType() =

    [<HotChocolate.Types.DefaultValue(MyEnum.Value1)>]
    member val Enum: MyEnum = MyEnum.Value1 with get, set

type Query() =

    member _.Test(arg: MyType) = ""


module Program =

    [<EntryPoint>]
    let main args =
        let builder = WebApplication.CreateBuilder(args)
        builder.Services.AddGraphQLServer().AddQueryType<Query>() |> ignore
        let app = builder.Build()
        app.MapGraphQL() |> ignore
        app.Run()
        0

What is expected?

type Query {
  test(arg: MyTypeInput): String
}

input MyTypeInput {
  enum: MyEnum! = VALUE1
}

enum MyEnum {
  VALUE1
  VALUE2
}

What is actually happening?

type Query {
  test(arg: MyTypeInput): String
}

input MyTypeInput {
  enum: Int! = 0
}

Relevant log output

No response

Additional context

Strangely enough, the following C# code, which as far as I know is equivalent to the F# code above, does not exhibit the problem, and produces the expected schema:

var builder = WebApplication.CreateBuilder(args);

builder
    .Services
    .AddGraphQLServer()
    .AddQueryType<Query>();

var app = builder.Build();

app.MapGraphQL();
app.Run();

public enum MyEnum
{
    Value1 = 0,
    Value2 = 1
}

public class MyType
{
    [HotChocolate.Types.DefaultValue(MyEnum.Value1)] public MyEnum Enum { get; set; }
}

public class Query
{
    public string Test(MyType arg) => "";
}

I tried also adding the GraphQLType(typeof<MyEnum>) attribute to the field. This did not help.

The following workaround seems to work (requires removing the [DefaultValue] attribute from the property to avoid other bugs):

type MyTypeDescriptor() =
    inherit InputObjectType<MyType>()

    override this.Configure(descriptor: IInputObjectTypeDescriptor<MyType>) : unit =
        descriptor.Field("enum").Type(typeof<MyEnum>).DefaultValue(MyEnum.Value1)
        |> ignore

// In the builder:
.AddType<MyTypeDescriptor>()
@cmeeren cmeeren added the 🐛 bug Something isn't working label Apr 5, 2024
@ChilliCream ChilliCream deleted a comment from SvdSinner Apr 18, 2024
@ChilliCream ChilliCream deleted a comment from cmeeren Apr 18, 2024
@SvdSinner
Copy link

SvdSinner commented Apr 23, 2024

Why was my comment deleted? I spent more than a day of research on that comment and I come back to check and it is deleted without comment? Please explain.

@cmeeren
Copy link
Contributor Author

cmeeren commented Apr 23, 2024

I did not delete it, but since it was off-topic, I reported it as such after several days of inactivity had passed. My intention was for the owners of this repo to hide it as off-topic in order to clean up this thread, but apparently it was deleted (along with my reply suggesting that you open a separate issue).

@SvdSinner
Copy link

I did open another issue, but initially, I was told on the slack channel to post into this thread. The two are most likely related as they post deal specifically with default values of enums. Not sure why you assumed it was off topic.

@cmeeren
Copy link
Contributor Author

cmeeren commented Apr 24, 2024

There may be many issues related to default enum values. Yours clearly seems to be a completely separate issue for the following reasons, as I described in my (now deleted) original comment:

  • Your bug is about AddMutationConventions throwing. I do not experience that; it is not a symptom of my bug.
  • My bug is specifically about F# and does not repro on C#.

These two points clearly point to these two bugs not overlapping in any meaningful way. That is why I suggested you open a different issue, and why after some time, I reported the comment as off-topic.

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

No branches or pull requests

3 participants