I am tying to validate my mutation input fields with HotChocolate.FluentValidation. Lookint at the example code, I do not have Argument on IInputObjectTypeDescriptor (descriptor). I have tried the following.
I wrote my injector:
public static class DependenciesInjector
{
public static void AddInjectableFluentValidationDependencies(this IServiceCollection services)
{
Assembly assembly = Assembly.GetExecutingAssembly();
IEnumerable<TypeInfo> classTypes = assembly.ExportedTypes.Select(t => IntrospectionExtensions.GetTypeInfo(t))
.Where(t => t.IsClass && !t.IsAbstract);
foreach (TypeInfo type in classTypes)
{
IEnumerable<TypeInfo> interfaces = type.ImplementedInterfaces.Select(i => i.GetTypeInfo());
foreach (TypeInfo handlerType in interfaces.Where(i => i.IsGenericType))
{
services.AddTransient(handlerType.AsType(), type.AsType());
}
}
}
}
and used on service collection
services.AddInjectableFluentValidationDependencies();
services.AddTransient<IValidatorInterceptor, FluentvalidationInterceptor>();
Configured my GraphQL:
public static void ConfigureGraphQL(this IServiceCollection services)
{
services.AddGraphQLServer()
.AddFluentValidation()
.ModifyOptions(options =>
{
options.DefaultResolverStrategy = ExecutionStrategy.Serial;
})
.AddQueryType<Query>()
.AddMutationType<Mutation>()
.AddSubscriptionType<Subscription>()
.AddType<PlatformType>()
.AddType<AddPlatformInputType>()
.AddType<AddPlatformPayloadType>()
.AddType<CommandType>()
.AddType<AddCommandInputType>()
.AddType<AddCommandPayloadType>()
.AddFiltering()
.AddSorting()
.AddInMemorySubscriptions();
}
and called it in the Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureDbContext(Configuration);
services.InjectContainerServices(Configuration);
services.ConfigureGraphQL();
services.AddFluentValidation(options =>
{
options.DisableDataAnnotationsValidation = false;
options.ImplicitlyValidateChildProperties = true;
});
}
Wrote my validator class
public class AddPlatformInputValidator : AbstractValidator<AddPlatformInput>
{
public AddPlatformInputValidator()
{
RuleFor(input => input.Name).NotEmpty()
.WithMessage("Name is required.");
}
}
Applied to input type record
public record AddPlatformInput([UseFluentValidation] string Name);
Applied it on input type class
public class AddPlatformInputType : InputObjectType<AddPlatformInput>
{
protected override void Configure(IInputObjectTypeDescriptor<AddPlatformInput> descriptor)
{
descriptor.Description("Represents the input to add for a platform.");
descriptor.Field(x => x.Name)
.Type<StringType>()
//.Argument("input", argument => argument.UseFluentValidation()) //error .Argument not recognized
.Description("Represents the name for the platform.");
base.Configure(descriptor);
}
}
Any idea what did I missed, or did wrong? thnx