使用 Gatsby 的元查询查询 wordpress

Querying wordpress with meta queries from Gatsby

我正在尝试使用元查询从我的 Wordpress 后端获取数据。我正在使用这个插件: https://www.wpgraphql.com/extenstion-plugins/wpgraphql-meta-query/

我可以 运行 在 GraphiQL 中查询 IDE 在 Wordpress 中,但不能在 Gatsbys GraphiQL 工具中查询。

我收到这个错误: 字段“Query.allWpPage”

上的未知参数“where”

查询:

query test {
  allWpPage(
    where: {metaQuery: {
      relation: OR,
      metaArray: [
        {
          key: "some_value",
          value: null,
          compare: EQUAL_TO
        },
        {key: "some_value",
          value: "536",
          compare: EQUAL_TO
        }
      ]
    }}
  ) {
    edges {
      node {
        id
        uri
      }
    }
  }
}

我试过删除缓存目录并重建,没有帮助。

澄清一下,运行我没有遇到其他查询和获取 ACL 数据等问题。我(现在)唯一的问题是将 where 参数暴露给 Gatsby。

where 过滤器在 Gatsby 中受到限制。这里有详细的list of comparators,不过他们是:

  • eq(等于)
  • ne(不等于)
  • in(包括)
  • nin(不包括)
  • ltltegtgte(分别小于、等于或小于、大于、等于或大于)
  • regexglob(正则表达式)
  • elemMatch(元素匹配)

另一方面,filters available. In your case, filter 列表正是您要查找的内容。您的最终查询应如下所示:

query test {
  allWpPage(
   filter : {uri : {ne : "" }}
  ) {
    edges {
      node {
        id
        uri
      }
    }
  }
}

当然,请根据您的需要调整 filterelemMatch 应该也适合你。

您需要为要匹配的每个 属性 对象添加每个条件。


为什么 where 受到限制?

因为它属于 Sift,Gatsby 用来使用的库 MongoDB queries, where where is available. Since Gatsby 2.23.0 (June 2020) this library is not being used anymore. More details at History and Sift:

For a long time Gatsby used the Sift library through which you can use MongoDB queries in JavaScript.

Unfortunately Sift did not align with how Gatsby used it and so a custom system was written to slowly replace it. This system was called “fast filters” and as of gatsby@2.23.0 (June 2020) the Sift library is no longer used.