Skip to content

Source Generator cannot deal with [BindField] #7786

Closed

Description

@PascalSenn

Product

Hot Chocolate

Version

14.1.0

Link to minimal reproduction

.

Steps to reproduce

[ObjectType<LineItem>]
public static partial class LineItemType
{
    [BindField("product")]
    public static Product GetProduct([Parent] LineItem lineItem)
        => new(lineItem.ProductId);
}

generates:

public static partial class LineItemType
{
    internal static void Initialize(global::HotChocolate.Types.IObjectTypeDescriptor<global::quick_start.Ordering.Types.LineItem> descriptor)
    {
        const global::System.Reflection.BindingFlags bindingFlags =
            global::System.Reflection.BindingFlags.Public
                | global::System.Reflection.BindingFlags.NonPublic
                | global::System.Reflection.BindingFlags.Static;

        var thisType = typeof(global::quick_start.Ordering.Types.LineItemType);
        var bindingResolver = descriptor.Extend().Context.ParameterBindingResolver;
        global::quick_start.Ordering.Types.LineItemTypeResolvers.InitializeBindings(bindingResolver);

        descriptor
            .Field(thisType.GetMember("GetProduct", bindingFlags)[0])
            .ExtendWith(c =>
            {
                c.Definition.SetSourceGeneratorFlags();
                c.Definition.Resolvers = global::quick_start.Ordering.Types.LineItemTypeResolvers.LineItemType_GetProduct();
            });

        descriptor // <---- HERE IS THE ISSUE

        Configure(descriptor);
    }

    static partial void Configure(global::HotChocolate.Types.IObjectTypeDescriptor<global::quick_start.Ordering.Types.LineItem> descriptor);
}
}


Workaround:

public static partial class LineItemType
{
    static partial void Configure(IObjectTypeDescriptor<LineItem> descriptor)
    {
        descriptor.Ignore(x => x.ProductId);
    }

    public static Product GetProduct([Parent] LineItem lineItem)
        => new(lineItem.ProductId);
}

What is expected?

The code is generated correctly

What is actually happening?

There is a compilation errros

Relevant log output


Additional context

No response

Activity

  1. changed the title [-]Source Generator cannot deal with [BindMember][/-] [+]Source Generator cannot deal with [BindFIeld][/+] on Dec 2, 2024
  2. added this to the HC-14.3.0 milestone on Dec 3, 2024
  3. self-assigned this
    on Dec 3, 2024
  4. changed the title [-]Source Generator cannot deal with [BindFIeld][/-] [+]Source Generator cannot deal with [BindField][/+] on Dec 17, 2024
  5. michaelstaib commented on Feb 27, 2026

    @michaelstaib
    Member

    Hey,

    I have tested this on main.
    I retested at commit d7dafeb.

    What I tested:

    [Fact]
    public void BindField_Does_Not_Produce_Assembly_Emit_Diagnostics()
    {
        var generated = TestHelper.GetGeneratedSourceSnapshot(
            """
            using HotChocolate;
            using HotChocolate.Types;
    
            namespace TestNamespace;
    
            public sealed class LineItem
            {
                public int ProductId { get; set; }
            }
    
            public sealed record Product(int Id);
    
            [ObjectType<LineItem>]
            public static partial class LineItemType
            {
                [BindField("product")]
                public static Product GetProduct([Parent] LineItem lineItem)
                    => new(lineItem.ProductId);
            }
            """).Match();
    
        Assert.DoesNotContain("Assembly Emit Diagnostics", generated, StringComparison.Ordinal);
    }

    Result on net8/net9/net10: passed.

    I could not reproduce the source-generator compile failure with [BindField] on current main.

  6. michaelstaib commented on Feb 27, 2026

    @michaelstaib
    Member

    Closing this since I couldn't reproduce it on current main after the retest above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions