GraphQL - search string in multiple fields

Viewed 1640

I am trying to search for a string across multiple fields with GraphQL.

I was able to use filter function with an or field, but it was not retrieving anything. I want to be able to retrieve an array with all the items that contain the searched string in title or/and body ==> so if the query string was found in title or body retrieve it to array.

My code is:

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

`

Works ok with only one field, but I want to have it in both fields

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

`
1 Answers

Seems that they changed the API.

Here is some code:

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

`

Basically you need to use $filters with an or to search in body or in the tile.

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

Cheers to all!

Related