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

Posted

技术标签:

【中文标题】GraphQL HotChoclate 中的接口 - 模式首先出现异常【英文标题】:Interfaces in GraphQL HotChoclate - schema first getting exception 【发布时间】:2022-01-04 12:41:53 【问题描述】:

我正在尝试在 Hot Chocolate 中实现模式优先并遇到 SchemaException

我有以下 GraphQL Schema

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)

是否可以返回泛型类 PagedResponse?还是我们需要为每个返回类型实现一个具体的类?即:SitesResponse、Entity1Response、Entity2Response 等

【问题讨论】:

【参考方案1】:

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

【讨论】:

注意,如果你有实现接口的接口,同样的规则是正确的。在每个界面级别上,您需要重复以下字段:GraphQL Interface Foo bar: String interface Baz implements Foo bar: String cux: String 太棒了,谢谢!做到了,刚刚进入 GraphQL 错过了关于如何指定接口的部分,很高兴知道。感谢您回复并非常感谢您在这里回答所有 HotChoclate 问题和 ChilliCream 平台的投入,这是一个精心设计的框架。

以上是关于GraphQL HotChoclate 中的接口 - 模式首先出现异常的主要内容,如果未能解决你的问题,请参考以下文章

第1590期GraphQL和Thrift在Airbnb架构中的配合使用

透过源码分析:GraphQL在开源项目skywalking中的应用

具有外键关系的对象的石墨烯中的 GraphQL 突变

Meteor Apollo GraphQL 接口:返回一个对象数组并在另一个解析器中调用解析器

GraphQL接口概念及测试ABC

使用接口构建 graphql 查询