【问题标题】:Multiple Query Type in Graphql HotchocolateGraphql Hotchocolate 中的多个查询类型
【发布时间】:2022-10-19 19:34:15
【问题描述】:

我正在使用热巧克力graphql。我有一个场景,我有两个单独的查询类型类。

  1. PostQuery -> 包含与帖子相关的查询
  2. UserQuery -> 包含与用户相关的查询

    我的文件夹结构

    这是我配置它的方式

     .AddAuthorization()
        //for inmemory subscription
        .AddInMemorySubscriptions()
        .AddQueryType<PostQuery>()
        .AddQueryType<UserQuery>()
        .AddMutationType<Mutation>()
        .AddSubscriptionType<Subscription>()
        .AddGlobalObjectIdentification()
        // Registers the filter convention of MongoDB
        .AddMongoDbFiltering()
        // Registers the sorting convention of MongoDB
        .AddMongoDbSorting()
        // Registers the projection convention of MongoDB
        .AddMongoDbProjections()
        // Registers the paging providers of MongoDB
        .AddMongoDbPagingProviders();
    

    但是,我收到以下错误

    System.ArgumentException: The root type `Query` has already been registered
    

    无论如何它可以配置,否则我必须将所有内容放在一个类中?

【问题讨论】:

    标签: c# graphql .net-6.0 hotchocolate


    【解决方案1】:

    您需要注册查询类型“Query”并添加解析器来处理多个“Query”类型的模式

    builder.Services
    .AddQueryType(q => q.Name("Query"))
    .AddType<PostQuery>()
    .AddType<UserQuery>()
    

    在您的查询类中:

    [ExtendObjectType("Query")]
    public class PostQuery 
    {
        public List<Post> GetAllPosts()
        {
            return List<Post>{...};
        }
    }
    
    [ExtendObjectType("Query")]
    public class UserQuery
    {
        public List<User> GetAllUsers()
        {
            return List<User>{...};
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-05
      • 2022-06-16
      • 2021-11-02
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 2018-12-21
      • 2016-04-23
      相关资源
      最近更新 更多