diff --git a/OttoTheGeek.Tests/InputTypeTests.cs b/OttoTheGeek.Tests/InputTypeTests.cs index f4b7579..f36b029 100644 --- a/OttoTheGeek.Tests/InputTypeTests.cs +++ b/OttoTheGeek.Tests/InputTypeTests.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Threading.Tasks; using FluentAssertions; @@ -13,6 +14,7 @@ public sealed class InputTypeTests { public enum Texture { + [Description("Desc of Crunchy")] Crunchy, Smooth, Chunky, @@ -177,6 +179,7 @@ public void ConfiguresEnumValues() kind enumValues { name + description } } }"); @@ -185,7 +188,7 @@ public void ConfiguresEnumValues() Kind = ObjectKinds.Enum, Name = "Texture", EnumValues = new [] { - new EnumValue { Name = "Crunchy" }, + new EnumValue { Name = "Crunchy", Description = "Desc of Crunchy" }, new EnumValue { Name = "Smooth" }, new EnumValue { Name = "Chunky" }, new EnumValue { Name = "Grainy" } diff --git a/OttoTheGeek/Internal/OttoEnumGraphType.cs b/OttoTheGeek/Internal/OttoEnumGraphType.cs index 38ce064..9017c73 100644 --- a/OttoTheGeek/Internal/OttoEnumGraphType.cs +++ b/OttoTheGeek/Internal/OttoEnumGraphType.cs @@ -1,3 +1,7 @@ +using System; +using System.ComponentModel; +using System.Linq; +using System.Reflection; using GraphQL.Types; namespace OttoTheGeek.Internal @@ -7,9 +11,15 @@ public sealed class OttoEnumGraphType : EnumerationGraphType public OttoEnumGraphType() { Name = typeof(TEnum).Name; - foreach(var val in typeof(TEnum).GetEnumValues()) + + var valuesByName = Enum.GetValues(typeof(TEnum)) + .Cast() + .ToDictionary(x => x.ToString()); + + foreach(var member in typeof(TEnum).GetMembers().Where(x => valuesByName.ContainsKey(x.Name))) { - AddValue(typeof(TEnum).GetEnumName(val), null, val); + var descAttr = member.GetCustomAttribute(); + AddValue(member.Name, descAttr?.Description, Enum.Parse(typeof(TEnum), member.Name)); } } }