GraphQL HotChoclate 中的接口 - 模式首先出现异常

Interfaces in GraphQL HotChoclate - schema first getting exception

我正在尝试在 Hot Chocolate 中实现模式优先,并在 运行 中实现 SchemaException

我有下面的 GraphQL 架构

type Query {      
  sites(skip: Int, take: Int): SitesResponse  
}

interface PagedResponse {
  hasNextPage: Boolean!
  hasPreviousPage: Boolean!
  totalCount: Int!
}

type SitesResponse implements PagedResponse {
  items: [Site]
}

type Site {
  siteNumber: String!
  siteName: String!
  siteType: String!
  divisionNumber: Int!
}

启动

services
.AddGraphQLServer()
.AddDocumentFromFile("GraphQlSchema.graphql")
.AddResolver<RosterResolver>("Query")
.BindRuntimeType<PagedResponse<Site>>("SitesResponse")

解析器

public class RosterResolver
    {
        public PagedResponse<Site> GetSites(int? skip, int? take)
        {
            return new PagedResponse<Site>
            {
                HasNextPage = false,
                HasPreviousPage = false,
                Items = new List<Site> { new Site { DivisionNumber = 1, SiteName = "test", SiteNumber = "1234", SiteType = "Test" } },
                TotalCount = 1
            };
        }

结果

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

1. The field `hasNextPage` must be implement by object type `SitesResponse`. (HotChocolate.Types.ObjectType)
2. The field `hasPreviousPage` must be implement by object type `SitesResponse`. (HotChocolate.Types.ObjectType)
3. The field `totalCount` must be implement by object type `SitesResponse`. (HotChocolate.Types.ObjectType)

   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)

是否可以 return 通用 class PagedResponse?或者我们是否需要为每个 return 类型实现一个具体的 class?即:SitesResponse、Entity1Response、Entity2Response 等

GraphQL 规范指出接口字段必须在类型上重复。

interface PagedResponse {
  hasNextPage: Boolean!
  hasPreviousPage: Boolean!
  totalCount: Int!
}

type SitesResponse implements PagedResponse {
  hasNextPage: Boolean!
  hasPreviousPage: Boolean!
  totalCount: Int!
  items: [Site]
}

如果您查看模式异常,它基本上只是告诉您:

The field `hasNextPage` must be implement by object type `SitesResponse`. (HotChocolate.Types.ObjectType)

接口说明: https://spec.graphql.org/October2021/#sec-Interfaces

类型验证规则: https://spec.graphql.org/October2021/#sec-Objects.Type-Validation