【问题标题】:Include mediaItems in GraphQL Wordpress query search在 GraphQL Wordpress 查询搜索中包含 mediaItems
【发布时间】:2020-09-11 14:30:14
【问题描述】:

我有一个无头 WordPress 安装,并使用 React 前端通过以下搜索查询来查询帖子:

  posts(where: {search: $searchStr}) {
    nodes {
        title
        content
        link
    }
  }

但也希望在搜索结果中包含媒体文件。我可以从以下附加查询中查询和注销 mediaItems:

  mediaItems {
    nodes {
      title
      sourceUrl
    }
  }

但无法弄清楚如何将这些内容包含在搜索结果中。如何将postsmediaItems 组合在同一个搜索查询中,并根据搜索字符串返回所有结果?

更新了完整的查询

我正在使用的完整查询将搜索查询传递到帖子 GraphQL 查询中。由于mediaItemsposts 在 WordPress 中都是顶级的,我该如何组合这两个查询,以便根据搜索参数返回所有帖子和所有媒体项?

query appQuery($searchStr: String) {
  posts(where: {search: $searchStr}) {
    nodes {
        title
        content
        link
        tags{
          nodes {
            name
          }
        }
    }
  }
  mediaItems {
    nodes {
      title
      sourceUrl
    }
  }
}

【问题讨论】:

    标签: reactjs wordpress graphql


    【解决方案1】:

    您可以为此创建另一个 GraphQL type 并将其嵌套在您的主 type 中,这样您也可以查询您的数据。还要为要检索的数据添加解析器。

    例如:假设您有一个博客网站并且您有一些帖子。为此

    type Post {
      _id: String
      title: String
      credit: PostCredit # Name of nested type
    }
    
    type PostCredit {
      authorId: String # You can also perform further nesting
      publicationId: String
    }
    

    【讨论】:

    • 感谢您的回复。你能举个例子吗?
    • 我不确定上述建议如何适用于我的实施。我已使用完整的搜索查询更新了我的问题,但不确定如何将 mediaItems 查询与 posts 查询集成,以便在搜索内容时同时使用它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多