GraphQL 按对象查询

问题描述

我对几乎所有东西都是新手; js、graphql 等。但我使用 WP GraphQL 从我的 wordpress 安装中获取内容以填充我在 NextJS 中制作的应用程序。我已经设法让 WP GraphQL 做我需要它做的事情,但现在我需要弄清楚如何让 javascript、nextjs 和 apolloClient 做我需要它做的事情!

基本上,当我想获取具有“钓鱼”和“洗衣”功能的所有露营地时,我有一个 WPGraphQL 查询,如下所示:

query{
 campgrounds(
    where: {
      taxQuery: {
        relation: OR,taxArray: [
          {
            terms: ["Fishing"],taxonomy: FEATURE,operator: IN,field: SLUG
          },{
            terms: ["Laundry"],field: SLUG
          }
        ]
      }
      }
  ){
    edges{
      cursor
      node{
        id
        title
      }
    }
  }
}

问题是,我希望能够使用选定的功能列表动态更改该 taxArray。我猜这样做的方法是对所选对象进行某种类型的数组映射(来自查询参数,例如 localhost:3000/http://localhost:3000/camps ?rcampfeatures=Cable%2CCampground+Store) 并将它们添加到...对象的对象中?包含类似 - 此处的伪代码 - :

let featurearray = {};
campfeatures.map(m => 
featurearray.push(

{
terms: ["${m}"],field: SLUG
}
) 
)

但我想我的问题是....我如何告诉 GraphQL 插入这些对象?我尝试构建的函数如下所示:

export async function getCampgroundsByFeature(query) {
  const data = await fetchAPI(
    `
    query($string: String){
      campgrounds(
         where: {
           taxQuery: {
             relation: OR,taxArray: [
              $string
             ]
           }
           }
       ){
         edges{
           cursor
           node{
             id
             title
           }
         }
       }
     }
    `,{
      variables: {
        string: query,},}
  );
}

但显然这是不正确的。

感谢您提供的任何帮助!!! :)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)