Skip to content

Commit

Permalink
Merge pull request #32 from asemio/enum-desc
Browse files Browse the repository at this point in the history
handle enum description attribute
  • Loading branch information
floyd-may authored Nov 17, 2020
2 parents a84ea99 + 16909aa commit 2f9f4be
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 4 additions & 1 deletion OttoTheGeek.Tests/InputTypeTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
Expand All @@ -13,6 +14,7 @@ public sealed class InputTypeTests
{
public enum Texture
{
[Description("Desc of Crunchy")]
Crunchy,
Smooth,
Chunky,
Expand Down Expand Up @@ -177,6 +179,7 @@ public void ConfiguresEnumValues()
kind
enumValues {
name
description
}
}
}");
Expand All @@ -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" }
Expand Down
14 changes: 12 additions & 2 deletions OttoTheGeek/Internal/OttoEnumGraphType.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using GraphQL.Types;

namespace OttoTheGeek.Internal
Expand All @@ -7,9 +11,15 @@ public sealed class OttoEnumGraphType<TEnum> : EnumerationGraphType
public OttoEnumGraphType()
{
Name = typeof(TEnum).Name;
foreach(var val in typeof(TEnum).GetEnumValues())

var valuesByName = Enum.GetValues(typeof(TEnum))
.Cast<TEnum>()
.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<DescriptionAttribute>();
AddValue(member.Name, descAttr?.Description, Enum.Parse(typeof(TEnum), member.Name));
}
}
}
Expand Down

0 comments on commit 2f9f4be

Please sign in to comment.