graphql:使用动态过滤条件进行查询 [英] graphql: querying with dynamic filtering criteria

查看:932
本文介绍了graphql:使用动态过滤条件进行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用in运算符执行查询,其中条件基于数组中的值.我如何执行下面的查询以获取数组并将其标准基于变量数组?我正在使用reactjs + gatsby.

I am trying to perform a query using the in operator where the criteria is based on values in an array. How can I perform the query below to take an array and base its in criteria off a variable array? I am using reactjs + gatsby.

... graphql`
      query pageHeader {
         .... there is another query in the real code above this line
         allContentInSites (filter: {slug: {in: ` + JSON.stringify(searchCriteria.map(item => item.value)) + ` }})  {
          edges {
            node {
              title,
              link
            }
          }
        }
      }'

最初,我最初认为上面的示例代码将为in条件传递一个json字符串对象,但这似乎破坏了页面.

I was initially thinking the sample code above would pass a json string object for the in criteria but this seems to break the page.

推荐答案

如果您想实现动态过滤条件,则有几种选择 1)您可以使用字符串化的json并将其作为String发送并在解析器中的服务器上进行解析 2)您可以使用自定义标量 graphql-type-json 作为参数类型或实现您的自己的自定义标量.

if you would like to implement dynamic filtering criteria, you have couple options 1) You can use stringified json and send it as String and parse it on the server in resolver 2) You can use custom scalar graphql-type-json as argument type or implement your own custom scalar.

最大的缺点是您失去了GraphQL的强类型功能.您也可以实现动态模式,但这有点过大,通常不是一个好习惯.仅当它是唯一选项时,我才会使用动态参数.通常,可以重新安排它,以便您不必使用动态参数.

The big downsides of this is that you are loosing strongly typed features of GraphQL. You can also implement dynamic schema, but it is a little bit overkill and in general not a good practice. I would go for dynamic arguments only if it is the only option. Usually it can be rearanged so that you do not have to use dynamic arguments.

我不确定这是否有帮助,因为我不熟悉gatsby的特定问题,但这在一般的GraphQL模式中应该可以使用.

I am not sure if it is helpful as i am not familiar with gatsby specific issues with this, but this should work in general GraphQL schemas.

这篇关于graphql:使用动态过滤条件进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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