【问题标题】:GraphApi with Hot Chocolate: No valid mutation field带有热巧克力的 Graph Api:没有有效的突变字段
【发布时间】:2021-10-03 19:31:38
【问题描述】:

我正在尝试在 ASP.NET Core 中使用 Hot Chocolate 设置 GraphApi。

现在我想将我的应用程序拆分为多个项目/组件。 有一个 Users 组件有一个包含 1 个字段的 UsersMutation:

    public sealed class UsersMutation
{
    public Task CreateUser([Service] ICreateUserMutationHandler handler, CreateUserParameters parameters)
        => handler.Handle(parameters);
}

我尝试像这样将它添加到 GraphQl 架构中:

 public sealed class Mutation
{
    public UsersMutation Users => new UsersMutation();
}

配置:

 public static class GraphApiConfiguration
{
    public static IServiceCollection AddGraphApi<TQuery, TMutation>(this IServiceCollection services)
        where TQuery : class
        where TMutation : class
    {
        services.AddGraphQLServer()
            .AddQueryType<TQuery>()
            .AddMutationType<TMutation>();

        services.AddScoped<TQuery>();
        services.AddScoped<TMutation>();

        return services;
    }
}

终于在startup.cs中:

        services.AddGraphApi<Query, Mutation>();

但尝试在操场上查看架构时出现以下错误:

HotChocolate.SchemaException: For more details look at the `Errors` property.

1. The object type `UsersMutation` has to at least define one field in order to be valid. (HotChocolate.Types.ObjectType<ChocoGraph.Components.Users.GraphApi.UsersMutation>)

   at HotChocolate.Configuration.TypeInitializer.Initialize(Func`1 schemaResolver, IReadOnlySchemaOptions options)
   at HotChocolate.SchemaBuilder.Setup.InitializeTypes(SchemaBuilder builder, IDescriptorContext context, IReadOnlyList`1 types, LazySchema lazySchema)
   at HotChocolate.SchemaBuilder.Setup.Create(SchemaBuilder builder, LazySchema lazySchema, IDescriptorContext context)
   at HotChocolate.SchemaBuilder.Create(IDescriptorContext context)
   at HotChocolate.SchemaBuilder.HotChocolate.ISchemaBuilder.Create(IDescriptorContext context)
   at HotChocolate.Execution.RequestExecutorResolver.CreateSchemaAsync(NameString schemaName, RequestExecutorSetup options, RequestExecutorOptions executorOptions, IServiceProvider serviceProvider, TypeModuleChangeMonitor typeModuleChangeMonitor, CancellationToken cancellationToken)
   at HotChocolate.Execution.RequestExecutorResolver.CreateSchemaServicesAsync(NameString schemaName, RequestExecutorSetup options, CancellationToken cancellationToken)
   at HotChocolate.Execution.RequestExecutorResolver.GetRequestExecutorNoLockAsync(NameString schemaName, CancellationToken cancellationToken)
   at HotChocolate.Execution.RequestExecutorResolver.GetRequestExecutorAsync(NameString schemaName, CancellationToken cancellationToken)
   at HotChocolate.Execution.RequestExecutorProxy.GetRequestExecutorAsync(CancellationToken cancellationToken)
   at HotChocolate.AspNetCore.HttpPostMiddleware.HandleRequestAsync(HttpContext context, AllowedContentType contentType)
   at HotChocolate.AspNetCore.HttpPostMiddleware.InvokeAsync(HttpContext context)
   at HotChocolate.AspNetCore.WebSocketSubscriptionMiddleware.InvokeAsync(HttpContext context)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\5.0.10\System.Buffers.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The program '[44280] iisexpress.exe: Program Trace' has exited with code 0 (0x0).
The program '[44280] iisexpress.exe' has exited with code -1 (0xffffffff).

我缺少什么来实现这一目标?如果我在 Mutation.cs 文件中有 CreateUser 字段,这似乎可以正常工作,但是添加这个额外的步骤似乎会破坏它。

【问题讨论】:

    标签: .net-core graphql


    【解决方案1】:

    我发现了问题:

    Mutation 必须返回一些东西才能成为有效的字段。

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 2020-05-27
      • 2022-10-25
      • 2022-08-03
      • 2022-01-10
      • 2021-11-24
      • 2020-12-22
      • 2019-07-21
      • 2020-11-13
      相关资源
      最近更新 更多