首页 > 解决方案 > GraphQL.Types.EnumerationGraphType 出错

问题描述

我正在尝试使用枚举创建类,但出现错误。

public class MarsWheather {
    public Season MarsSeason { get; set; }
}

public enum Season {
    winter,
    spring,
    summer,
    autumn
}

我的 graphql 类看起来像:

public class SolSchema : Schema
{
    public SolSchema(IServiceProvider sp) : base(sp)
    {
        Query = sp.GetRequiredService<SolDataQuery>();
        Mutation = sp.GetRequiredService<SolDataMutation>();
    }
}
public class SolDataQuery : ObjectGraphType<object>
{
    public SolDataQuery(INasaProvider nasaProvider)
    {
            Field<MarsWheatherType>("wheather", resolve: context => nasaProvider.GetAsync());
    }
}
public class MarsWheatherType : ObjectGraphType<MarsWheather>
{
    public MarsWheatherType()
    {
        Field(w => w.MarsSeason);
    }
}

public class SeasonEnum : EnumerationGraphType<Season>
{ }

我已经在 startup.cs(ConfigureServices 方法)中注册了它:

services.AddSingleton<SolDataQuery>();
        services.AddSingleton<SolDataMutation>();
        services.AddSingleton<SeasonEnum>();
        services.AddSingleton<MarsWheatherType>();
        services.AddSingleton<ISchema, SolSchema>();
        

我正在使用 GraphQLPlayground。我的要求是:

wheather {     
 marsSeason}

毕竟我有一个错误:

GraphQL.Execution.UnhandledError: 执行文档时出错。\r\n ---> >System.InvalidOperationException: 在 >GraphQL 中找不到类型 >GraphQL.Types.EnumerationGraphType`1[Mars.Season] 所需的服务\r\n。 Utilities.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type >serviceType) in /_/src/GraphQL/Utilities/ServiceProviderExtensions.cs:line 42

此错误的代码为“INVALID_OPERATION”

有人可以帮我吗?我做错了什么?PS 如果有帮助,我将这个未完成的项目推送到github

标签: c#graphql.net-core-3.0

解决方案


好的。MarsWheatherType 的正确代码是:

public class MarsWheatherType : ObjectGraphType<MarsWheather>
{
    public MarsWheatherType()
    {
        Field<SeasonEnum>("season", resolve: w => w.Source.MarsSeason);
    }
}

推荐阅读