8

How do I convert an enum to the EnumerationGraphType that GraphQL uses? Here is an example to illustrate what I'm talking about:

public enum MeetingStatusType
{
    Tentative,
    Unconfirmed,
    Confirmed,
}
public class MeetingDto
{
    public string Id { get; set; }
    public string Name { get; set; }
    public MeetingStatusType Status { get; set; }
}
public class MeetingStatusEnumType : EnumerationGraphType<MeetingStatusType>
{
    public MeetingStatusEnumType()
    {
        Name = "MeetingStatusType";
    }
}
public class MeetingType : ObjectGraphType<MeetingDto>
{
    public MeetingType()
    {
        Field(m => m.Id);
        Field(m => m.Name, nullable: true);
        Field<MeetingStatusEnumType>(m => m.Status); // Fails here
     }
}

Obviously this doesn't work because there's no implicit conversion from MeetingStatusType to MeetingStatusEnumType. In the documentation, the models that they were mapping would rely directly on MeetingStatusEnumType, but it doesn't seem good to introduce the dependency on GraphQL on something like your domain types and objects. I feel like I'm missing a painfully easy way to register this field, but I can't figure it out for the life of me. Any help would be greatly appreciated!

Daric
  • 440
  • 1
  • 4
  • 12
  • 1
    Can you show the defination of `Field` or the exact error message? I can only find one with first parameter is string. – shingo May 08 '19 at 04:01
  • Two errors show up: `Cannot implicitly convert type 'MeetingStatusType' to 'MeetingStatusEnumType'` `Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type` This is the definition of the Field overload I'm using: `public FieldType Field(string name, string description = null, QueryArguments arguments = null, Func, object> resolve = null, string deprecationReason = null) where TGraphType : IGraphType;` – Daric May 09 '19 at 01:20

3 Answers3

7

Looks like I should not have been trying to use the expression overload for mapping the fields. Switching it out to be the following instead seems to have solved the issue:

Field(e => e.Id);
Field(e => e.Name, nullable: true);
Field<MeetingStatusEnumType>("meetingStatus", resolve: e => e.Source.Status);
Daric
  • 440
  • 1
  • 4
  • 12
1

You need to tell graphql dotnet how to map the Enum type to the EnumerationGraphType.

GraphTypeTypeRegistry.Register(typeof(MeetingStatusType), typeof(EnumerationGraphType<MeetingStatusType>));
furier
  • 1,934
  • 1
  • 21
  • 39
  • Is this a typo? I don't see anything like `GraphTypeTypeRegistry` – George Mauer Mar 06 '20 at 22:15
  • 1
    Here it is, not a typo... [GraphTypeTypeRegistry](https://github.com/graphql-dotnet/graphql-dotnet/blob/09effc2e44d5d99edeb7bd85caa99f25c53b06d0/src/GraphQL/Utilities/GraphTypeTypeRegistry.cs) – furier Mar 12 '20 at 16:48
1

Another way;

Field(m => m.Status, type: typeof(EnumerationGraphType<MeetingStatusType>));
moustapha
  • 71
  • 1
  • 5