Skip to content

Validation Attributes Not Working with [GraphQLInputType] but Work with [GraphQLArguments] #429

@samyonr

Description

@samyonr

Hello,

I'm experiencing an issue where the data annotation validation attributes [StringLength] and [Range] are not enforced when using [GraphQLInputType] on input classes for mutations. However, these validation attributes work correctly when using [GraphQLArguments].

Environment:

EntityGraphQL.AspNet Version: 5.5.0
.NET Version: .NET 8
C# Version: C# 12

Details

  1. Using [GraphQLInputType]:
[GraphQLInputType]
public class PersonMutationArgs
{
    [StringLength(5, ErrorMessage = "Person name must be less than 5 characters")]
    public string PersonName { get; set; } = default!;

    [Range(0, 100, ErrorMessage = "Age must be between 0 and 100")]
    public int Age { get; set; }
}
  1. Using [GraphQLArguments]:
[GraphQLArguments]
public class PersonMutationFlatArgs
{
    [StringLength(5, ErrorMessage = "Person name must be less than 5 characters")]
    public string PersonName { get; set; } = default!;

    [Range(0, 100, ErrorMessage = "Age must be between 0 and 100")]
    public int Age { get; set; }
}

And the corresponding mutations:

public class PersonMutation : IGraphQlMutation
{
    [GraphQLMutation("Add a new person to the system")]
    public async Task<Expression<Func<EgDbContext, Person>>?> AddNewPerson(
        EgDbContext db, PersonMutationArgs args, IGraphQLValidator validator)
    {
        // Implementation omitted for brevity
    }

    [GraphQLMutation("Add a new person to the system")]
    public async Task<Expression<Func<EgDbContext, Person>>?> AddNewPersonFlat(
        EgDbContext db, PersonMutationFlatArgs args, IGraphQLValidator validator)
    {
        // Implementation omitted for brevity
    }
}
  • With [GraphQLInputType]: When I use AddNewPerson and provide invalid inputs (e.g., a name longer than 5 characters or an age outside the range 0-100), the mutation does not produce validation errors. The person is incorrectly added to the database.

  • With [GraphQLArguments]: When I use AddNewPersonFlat with the same invalid inputs, the validation works as expected, and the mutation returns the appropriate validation errors.

Expected Behavior:

Validation attributes like [StringLength] and [Range] should be enforced when using [GraphQLInputType], just as they are with [GraphQLArguments].

Reproduction:

I have prepared a minimal reproducible example demonstrating this issue. The code can be found here.

Also, adding it as a zip file here: EntityGraphQL_StringLength.zip

In the example, I've added two tests: one that uses [GraphQLInputType] and another that uses [GraphQLArguments]. While both pass, one of them is actually able to add data to the DB while it shouldn't due to a validation failure. The other one fails to add data to the DB as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions