GraphQL Multiple Search Filters and Case Insensitive Search

Learn how to get around GraphQL limitations with case insensitive search and adding multitple search filters to the query

Photo by Christina @ wocintechchat.com on Unsplash

Problem 1: Multiple Where Clauses in Filters Not Allowed

Currently, you cannot filter GraphQL results with more than two “where” clause. Wouldn’t it be awesome if you could execute and manipulate result with conditional filters as below:

Select * from table-name where item1 = <item_1> and item2= <item_2>
  1. https://stackoverflow.com/questions/52347310/graphql-conditional-queries
  2. https://stackoverflow.com/questions/37981269/how-to-search-string-values-in-graphql
  3. https://stackoverflow.com/questions/54215349/graphql-where-clause-angular-apollo-client
  4. https://stackoverflow.com/questions/48912739/how-can-i-do-a-wpgraphql-query-with-a-where-clause

Problem 2: Search in Filters is Case Sensitive

Believe it or not, if you want to query your GraphQL table, it has to be a case sensitive search. If one of the value is “mind and matter”, you cannot query it as :

query MyQuery {
listData (filter: {data: {contains: "Mind"}}) {
items{
id,
name
}
}
}
  1. https://stackoverflow.com/questions/53878566/how-to-make-a-case-insensitive-graphql-query
  2. https://stackoverflow.com/questions/48142347/graphql-sorting-case-insensitive
  3. https://stackoverflow.com/questions/51526079/how-to-use-or-and-in-graphql-query-filter-or-make-a-case-insensitive-filter
  4. https://github.com/graphql/graphql-js/issues/585#issuecomment-262402544

Solution:

To explore a workaround, I tried changing my schema a little. I added a new field in my table schema, “searchQuery” to index search results based on the query

type data @model {
id: ID!
name: String!
address: String!
searchString: String
}
data = new data() 
data.id = self.id
data.name = self.name
data.address = self.address
data.searchString = String(self.name.lowercased() + self.address.lowercased())
query = "SoME StrInG HeRE"
query = query.lowercased() //Yes I love SWIFT
query MyQuery {
listData (filter: {searchString: {contains: query}}) {
items{
id,
name
}
}
}

International writer with diverse interests

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store