0

I have a project, where I use Hot Chocolate for a GraphQL API.

I create everything like this:

private static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        // graphQL ---
        var graphQlServerBuilder = builder.Services.AddGraphQLServer();
        // configure server
        graphQlServerBuilder.ModifyOptions(options =>
        {
            options.DefaultBindingBehavior = BindingBehavior.Explicit;
        });
        // query
        builder.Services.AddScoped<ExampleQuery>();
        graphQlServerBuilder.AddQueryType<ExampleQuery>();
        // types
        graphQlServerBuilder.AddType<ProductType>();

        var app = builder.Build();

        // expose /graphql endpoint
        app.MapGraphQL();

        app.Run();
    }

public class ExampleQuery
    {
        [GraphQLName("products")]
        [GraphQLDescription("Returns all products in the data base.")]
        [GraphQLType(typeof(ListType<ProductType>))]
        public IEnumerable<ProductDto> GetAllProducts()
        {
            return new List<ProductDto> ();
        }
    }

 public class ProductType : ObjectType<ProductDto>
    {
        protected override void Configure(IObjectTypeDescriptor<ProductDto> descriptor)
        {
            //descriptor.BindFields(BindingBehavior.Explicit);
            // name the product field inside the query
            descriptor.Name("product");
            // create fields
            descriptor.Field(product => product.Name)
                .Name("name")
                .Type<StringType>()
                .Description("The name of the product.");

            descriptor.Field(product => product.TotalPrice())
                .Name("totalprice")
                .Type<FloatType>()
                .Description("The price of a product including all costs of material and assembling.");
    }
}

public class ProductDto
    {
        [Required]
        public string Name { get; set; }
        [Required]
        public double ManufacturingCosts { get; set; }
        [Required]
        public double TotalMaterialCosts { get; set; }

        public double TotalPrice() => TotalMaterialCosts + ManufacturingCosts;

    }

But when I execute and look at the product object inside the schema reference, I can still see all fields (e.g. ManufacturingCosts should not be visible).

If I add descriptor.BindFields(BindingBehavior.Explicit) to my type, they are no longer visible.

I read the description here, but I can not figure out, what could cause this problem. Besides that, GraphQL seems to work fine.

Update

This might be a bug described here. I will try it again, if the bug is fixed. For now I will just set it in every class.

  • Please post complete, compilable code that reproduces the problem. Right now someone would have to add types, queries etc by hand, probably getting a different result because the types and queries don't match – Panagiotis Kanavos May 10 '23 at 10:50

0 Answers0