Home > Enterprise >  how to display enum number instead of enum name in response
how to display enum number instead of enum name in response

Time:09-09

When trying to get an answer using hotchocolate, in fields with enum type the answer is in the form of a string, and I need an enum value

[UseProjection]
    public IQueryable<Cart> GetActualCart([Service] ApplicationDbContext context, ClaimsPrincipal claimsPrincipal, int pageNumber = 1, int pageSize = 10)
    {
        var userId = claimsPrincipal.FindFirstValue(ClaimTypes.NameIdentifier);
        return context.Carts
            .Where(f => f.CourierId == new Guid(userId))
            .FromPage(pageNumber, pageSize);
    }

public class Cart : AuditableBaseEntity<Guid>
{
    public Guid CourierId { get; set; }
    public CartStatus Status { get; set; } = CartStatus.Building;
    public virtual ICollection<CartItem> Items { get; set; } = new HashSet<CartItem>();
}

public enum CartStatus
{
    Building = 1,
    WarehouseDelivery = 2,
    WarehouseRefund = 3, 
    CartRefund = 4, 
}

RESPONSE:

"data": {
"actualCart": [
  {
    "courierId": "efb60c9e-c6fe-4479-bd93-82fb23ad63b5",
    "status": "BUILDING"
  },
  {
    "courierId": "efb60c9e-c6fe-4479-bd93-82fb23ad63b5",
    "status": "WAREHOUSE_DELIVERY"
  }
]

Example call

  • Related