GraphQL - 在多个字段中搜索字符串

3 graphql graphql-js

我正在尝试使用 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
              }
            }
          }
        }
    }
  
 }
}

`
Run Code Online (Sandbox Code Playgroud)

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

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

`
Run Code Online (Sandbox Code Playgroud)

小智 5

看来他们改变了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
              }
            }
          }
        }
    }
  
 }
}

`
Run Code Online (Sandbox Code Playgroud)

基本上,您需要使用$filtersan 来or在正文或图块中进行搜索。

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

祝大家干杯!