使用来自 Gatsby 的元查询查询 wordpress [英] Querying wordpress with meta queries from Gatsby

查看:28
本文介绍了使用来自 Gatsby 的元查询查询 wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to fetch data from my Wordpress backend using a meta query. I'm using this plugin: https://www.wpgraphql.com/extenstion-plugins/wpgraphql-meta-query/

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

I can run my query in GraphiQL IDE in Wordpress, but not in Gatsbys GraphiQL tool.

我收到此错误:未知参数where"在字段Query.allWpPage"上

I get this error: Unknown argument "where" on field "Query.allWpPage"

查询:

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
      }
    }
  }
}

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

I've tried deleting the cache directory and rebuilding, didn't help.

为了澄清一下,我在运行其他查询和获取 ACL 数据方面没有任何问题.我(现在)唯一的问题是向 Gatsby 公开 where 参数.

And just to clarify, I have no problems running other queries and getting ACL-data and what not. The only problem I have (right now) is exposing the where argument to Gatsby.

推荐答案

where 过滤器在 Gatsby 中受到限制.这里有一个详细的比较器列表,但它们是:

where filter is restricted in Gatsby. Here you have a detailed list of comparators, but they are:

  • eq(等于)
  • ne(不等于)
  • in(包含)
  • nin(不包括)
  • ltltegtgte(小于、等于或小于、大于、分别等于或大于)
  • regexglob(正则表达式)
  • elemMatch(元素匹配)
  • eq (equals)
  • ne (not equals)
  • in (includes)
  • nin (not includes)
  • lt, lte, gt, gte (less than, equal or less than, greater than, equal or greater than respectively)
  • regex, glob (regular expression)
  • elemMatch (element matches)

另一方面,有一个可用过滤器的列表.在你的情况下,filter 就是你正在找.您的最终查询应如下所示:

On the other hand, there is a list of filters available. In your case, filter is what you are looking for. Your final query should look like:

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

当然,根据您的需要调整 filter.elemMatch 应该也适合你.

Of course, adapt the filter to your needs. elemMatch should work for you either.

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

You will need to add each condition for each property of the object you're trying to match.

因为它属于 Sift,一个 Gatsby 用来使用 MongoDB 查询的库,其中 where 可用.由于 Gatsby 2.23.0 (2020 年 6 月)不再使用该库.历史和筛选:

Because it belonged to Sift, a library that Gatsby was using to use 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:

很长一段时间 Gatsby 使用了 Sift 库,通过它你可以使用JavaScript 中的 MongoDB 查询.

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

不幸的是,Sift 与 Gatsby 使用它的方式不一致,因此编写自定义系统是为了慢慢替换它.这个系统被称为快速过滤器"以及从 gatsby@2.23.0(2020 年 6 月)开始,Sift 库是不再使用.

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.

这篇关于使用来自 Gatsby 的元查询查询 wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆