I'm trying to learn how to use HotChocolate and GraphQL as an alternative to REST APIs. Now my project does not allow implicit nullable reference types so when I have a model I need a constructor to set every of my properties that aren't nullable to a value.
Below you'll find the model I'm using:
public class TaskEntity
{
public TaskEntity(string name)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
TaskCategoryEntities = new List<TaskCategoryEntity>();
}
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<TaskCategoryEntity> TaskCategoryEntities { get; set; }
}
And this is the query I have set up:
[ExtendObjectType(nameof(Query))]
public class TaskQuery
{
[UseProjection]
[UseFiltering]
[UseSorting]
public IQueryable<TaskEntity> Tasks([Service] TaskifyDbContext context, [Service] IMapper mapper)
{
return context.Tasks;
}
}
Type 'Dev.Taskify.Resources.Entities.TaskEntity' does not have a default constructor (Parameter 'type')
Ofcourse I can resolve this by adding a default constructor in TaskEntity but then I'm greeted by this warning in my IDE
Non-nullable properties 'Name', 'TaskCategoryEntities' are uninitialized. Consider declaring the properties as nullable.
Is there a way for my query to working without the default ctor? If not, why not?