如何使用GraphQL动态构建EnumerationGraphType

时间:2019-05-21 16:25:08

标签: c# graphql graphql-dotnet

我想对查询进行排序,但是我不想为我拥有的每个类创建硬编码的EnumerationGraphType,以便知道我应该在哪个字段上进行排序。 我在GraphQL for .NET中使用C#。

我尝试使用模板,并尝试向OrderByFieldEnum传递包含枚举可能值的列表。

这是我的图形类型,我想以某种方式使其动态化:

public class OrderByGraphType : ObjectGraphType<OrderBy>
{   
    public OrderByGraphType()
    {
        Field<OrderByFieldEnum>().Name("OrderBy"); // This is the field that I'd like to build dynamically, from a List<string>
        Field<OrderDirectionTypeEnum>().Name("OrderDirection").Resolve(context => context.Source.OrderDirection);
    } 
}

// The enum that should be built dynamically
public class OrderByFieldEnum : EnumerationGraphType
{
    public OrderByFieldEnum(string graphTypeName, List<string> possibleValues)
    {
        Name = graphTypeName; 

        if (possibleValues != null)   
        {    
            foreach (var option in possibleValues)
            {
                AddValue(option.ToUpper(), string.Empty, option);
            }
        }
    }
}

这显然行不通。 GraphQL无法以这种方式构建其架构。 有人有解决方案吗?

0 个答案:

没有答案