postgresql 动态添加过滤条件_graphql:使用动态过滤条件进行查询

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

}

}

}

}'

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.

解决方案

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.

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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值