.Net6 + GraphQL + MongoDb 实现Query过滤(查询)功能
系列文章列表,点击展示/隐藏
正文
介绍
Query返回数据我们已经看完了,这里我们将如果筛选数据,给查询加上条件
正文
修改Program
builder.Services
.AddGraphQLServer()
.AddQueryType<PostQuery>()
.AddMongoDbFiltering()
.AddMongoDbProjections();
修改PostQuery.cs
public class PostQuery
{
[UseProjection]
[UseFiltering]
public IExecutable<Post> GetPosts([Service] DbContext db)
{
return db.Post.AsExecutable();
}
}
这时候我们去https://localhost:7145/graphql/
调用的接口.加上where条件
query testGetPost {
posts(where: {
title: { eq: "1 - introduction to graphql" }
}) {
id
title
comments {
name
}
}
}
我们看下mongodb的监控信息,已经对我们的where做出了变化
{
"op": "query",
"ns": "GQL_Example.post",
"command": {
"find": "post",
"filter": {
"Title": {
"$eq": "1 - introduction to graphql"
}
},
"sort": {
"Title": NumberInt("1")
},
"projection": {
"Comments.Name": NumberInt("1"),
"Title": NumberInt("1"),
"_id": NumberInt("1")
},
"$db": "GQL_Example",
"lsid": {
"id": UUID("3b2fc182-6821-4216-96a3-d3db42350015")
},
"$clusterTime": {
"clusterTime": Timestamp(1676094001, 1),
"signature": {
"hash": BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId": NumberLong("0")
}
}
}
}
结语
本系列主要将GraphQL的使用,示例项目不能应用于生产,后续发一些GraphQL库出来讲解生产中的实际应用
联系作者:加群:867095512 @MrChuJiu
分类:
GraphQL
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端