shu*_*qui 8 c# graphql hotchocolate .net-6.0
我正在使用热巧克力 graphql。我有一个场景,我有两个单独的查询类型类。
我的文件夹结构
这是我的配置方式
.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();
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误
System.ArgumentException: The root type `Query` has already been registered
Run Code Online (Sandbox Code Playgroud)
无论如何,它可以配置吗?否则我必须将所有内容放在一个类中?
sjo*_*ten 21
您需要注册查询类型“Query”并添加解析器来处理“Query”类型的多个模式
builder.Services
.AddQueryType(q => q.Name("Query"))
.AddType<PostQuery>()
.AddType<UserQuery>()
Run Code Online (Sandbox Code Playgroud)
在您的查询类中:
[ExtendObjectType("Query")]
public class PostQuery
{
public List<Post> GetAllPosts()
{
return List<Post>{...};
}
}
[ExtendObjectType("Query")]
public class UserQuery
{
public List<User> GetAllUsers()
{
return List<User>{...};
}
}
Run Code Online (Sandbox Code Playgroud)
首先感谢@sjokkogutten 的回答。我强烈不同意他的做法。随着您的应用程序规模变大,您的类型管理起来将变得更加繁琐。
更好的方法是在部分类中定义查询。
postQuery.cs
public partial class Query
{
public List<Post> GetAllPosts()
{
return List<Post>{...};
}
}
Run Code Online (Sandbox Code Playgroud)
用户查询.cs
public partial class Query
{
public List<User> GetAllUsers()
{
return List<User>{...};
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4738 次 |
| 最近记录: |