Search code examples
graphqlpostgraphile

Or filtering with PostGraphile not working


I am trying to search multiple fields in one query with the use of the OR and filter.

query SearchQuery($searchTerm: String) {
  allArtists(
    filter: {surname: {includesInsensitive: $searchTerm}, or: {firstname: {includesInsensitive:  $searchTerm}}}
  ) {
    edges {
      node {
        firstname
        surname
      }
    }
  }

Both surname and firstname will return results if done separately, however when combining with a OR it will return no results.

Is it possible to search multiple fields?


Solution

  • The filter takes all options and combines with AND. The or option takes a list of filters and combines them with OR. Since you only want the OR condition you need to supply your options inside the OR list:

    query SearchQuery($searchTerm: String) {
      allArtists(
        filter: {or: [
          {surname: {includesInsensitive: $searchTerm}},
          {firstname: {includesInsensitive:  $searchTerm}}
        ]}
    
      ) {
        edges {
          node {
            firstname
            surname
          }
        }
      }
    }