reactjs - 在 GraphQL Wordpress 查询搜索中包含 mediaItems
问题描述
我有一个无头 WordPress 安装,并使用 React 前端通过以下搜索查询来查询帖子:
posts(where: {search: $searchStr}) {
nodes {
title
content
link
}
}
但也想在搜索结果中包含媒体文件。我可以从以下附加查询中查询和注销 mediaItems:
mediaItems {
nodes {
title
sourceUrl
}
}
但无法弄清楚如何将这些包含在搜索结果中。如何在同一个搜索查询中组合posts
andmediaItems
并根据搜索字符串返回所有结果?
更新了完整的查询
我正在使用的完整查询将搜索查询传递到帖子 GraphQL 查询中。由于mediaItems
和posts
都是 WordPress 中的顶级,我如何结合这两个查询,以便根据搜索参数返回所有帖子和所有媒体项?
query appQuery($searchStr: String) {
posts(where: {search: $searchStr}) {
nodes {
title
content
link
tags{
nodes {
name
}
}
}
}
mediaItems {
nodes {
title
sourceUrl
}
}
}
解决方案
您可以为此创建另一个 GraphQL 并将type
其嵌套在您的 maintype
中,以便您也可以查询您的数据。还要为要检索的数据添加解析器。
例如:假设您有一个博客网站并且您有一些帖子。为了那个原因
type Post {
_id: String
title: String
credit: PostCredit # Name of nested type
}
type PostCredit {
authorId: String # You can also perform further nesting
publicationId: String
}
推荐阅读
- typescript - 如何在 ngOnInit 上订阅两次?
- c# - 调用 RequestClientCredentialsTokenAsync 时,如何修复“对象引用未设置为对象的实例”?
- neo4j - 为什么不建议在图数据库中索引关系
- oracle - 插入语句中的 PL/SQL 绑定变量,
- php - 连接到 klick-tipp “API Zugriff verweigert”时出现 API 错误(拒绝访问)
- angular - 无法解析“@angular/material/typings/”
- .net - 在 PayPal 中执行付款后,我可以获得任何收款人的链接或交易 ID 吗?
- python - 在 PySpark 中添加滚动最新的列
- asp.net - 插入 ASP.NET 中的 Session 对象时,Serializable 类中的属性是否会自动序列化?
- python-3.x - 如何从multipledropdown相关列表中抓取所有选项值