【问题标题】:GraphQL - search string in multiple fieldsGraphQL - 在多个字段中搜索字符串
【发布时间】:2022-01-14 18:38:14
【问题描述】:

我正在尝试使用 GraphQL 在多个字段中搜索字符串。

我能够使用带有 or 字段的过滤器功能,但它没有检索任何内容。 我希望能够检索一个数组,其中包含标题或/和正文 ==> 中包含搜索字符串的所有项目,因此如果在标题或正文中找到查询字符串,则将其检索到数组中。

我的代码是:

const search_reviews= gql`
 query SearchReviews ($my_query: String) {
    reviews (filters: {title: {contains: $my_query}, or: {body: {contains: $my_query}} }) {
      data{
        id
        attributes{
          title
          rating
          body
          categories{
            data{
              id
              attributes
              {
                name
              }
            }
          }
        }
    }
  
 }
}

`

只适用于一个字段,但我想在两个字段中都有它

const search_reviews= gql`
 query SearchReviews ($my_query: String!) {
    reviews (filters: {body: {contains: $my_query} }) {
      data{
        id
        attributes{
          title
          rating
          body
          categories{
            data{
              id
              attributes
              {
                name
              }
            }
          }
        }
    }
  
 }
}

`

【问题讨论】:

    标签: graphql graphql-js


    【解决方案1】:

    似乎他们改变了 API。

    这里有一些代码:

    const search_reviews = gql`
     query SearchReviews ($my_query: String!) {
        reviews (filters: {or: [{body: {contains: $my_query} }, {title: {contains: $my_query}}]}) {
          data{
            id
            attributes{
              title
              rating
              body
              categories{
                data{
                  id
                  attributes
                  {
                    name
                  }
                }
              }
            }
        }
      
     }
    }
    
    `
    

    基本上您需要使用$filtersor 在正文或磁贴中进行搜索。

    reviews (filters: {or: [{body: {contains: $my_query} }, {title: {contains: $my_query}}]})

    祝大家好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多