Reputation: 8201
I have the following Graphql type class including the POCO class: Order.cs
.
Here I want to set the default value of each field. For example for Name
I want to return "No Name" in case there is no value returned for it. In case of Created
, I want to return today's date by default.
public class OrderType : ObjectGraphType<Order>
{
public OrderType(ICustomerService customers)
{
Field(o => o.Id);
Field(o => o.Name);
Field(o => o.Description);
Field(o => o.Created);
}
}
public class Order
{
public Order(string name, string description, DateTime created, string Id)
{
Name = name;
Description = description;
Created = created;
this.Id = Id;
}
public string Name { get; set; }
public string Description { get; set; }
public DateTime Created { get; private set; }
public string Id { get; private set; }
}
Can anyone help me to fix this issue?
Upvotes: 2
Views: 6935
Reputation: 39946
If you are using C# 6+, it has added the ability to assign a default value to auto-properties. So you can simply write something like this:
public string Name { get; set; } = "No Name";
public DateTime Created { get; private set; } = DateTime.Today;
This will set the default value of the Name
property to No Name
and the Created
property to today's date, as you wanted.
Upvotes: 11