How to use OR / AND in graphql query filter or make a case insensitive filter?
Asked Answered
P

4

19

Just like the post ask I need to be able to search with 3 possible scenarios. I need to have all uppercase, or lowercase, or normal casing.

If that is not possible is there a way to do a case insensitive filter instead?

  allMarkdownRemark(filter: { brand: { eq: $normalBrand } }) { //==> Need more here
        edges {
            node {
                webImages {
                    url
                }
            }
        }
    }

I found some people doing this:

filter: { OR: [
  {brand: { eq: $normalBrand }}, 
  {brand: { eq: $normalBrand2 }}, 
  {brand: { eq: $normalBrand3 }}
]}

But it does not work for me

Prescott answered 25/7, 2018 at 19:10 Comment(1)
This might be relevant: github.com/graphql-compose/graphql-compose-mongoose/issues/93Wingless
B
9

Currently there is not anything built-in to graphql itself that uses case insenstive search, its completely based on whatever library the graphql server is running and if that chooses to add support for that. If you can modify the graphql query code, you can add that in yourself in some fashion, or request whoever is hosting/building that to add in that feature somehow.

Regarding OR, I doubt I could say it better than https://github.com/graphql/graphql-js/issues/585#issuecomment-262402544

GraphQL doesn't have support for AND/OR operators for exactly the reason you mentioned at the top of your issue: it does not map to any kind of storage. I think to properly understand why it does not have these requires a shift in mental models. We think of GraphQL as more similar to RPC languages than database languages.

To help understand the mental model shift, it's useful to think about fields and arguments like function calls in programming languages. So suppose we ask a similar question of javascript: Why can't you type: getCaseByStatus("open" OR "closed")? The answer, I would assume, is because JavaScript does not know what OR means in this context. Should the values "open" and "closed" be combined in some way to produce another value? Should the execution of the function getCaseByStatus change in some way? JavaScript can't be sure - because the concept of field-based filtering is not part of JavaScript's semantics. ...

Blood answered 25/7, 2018 at 19:52 Comment(1)
Its not a query language if you cant ask it things.Gombach
J
1

Does regex work? Something like

  allMarkdownRemark(filter: { brand: { 
     regex: "/($normalBrand)|($normalBrand2)|($normalBrand3)/" 
  } }) { 
        edges {
            node {
                webImages {
                    url
                }
            }
        }
    }
Justinajustine answered 25/7, 2018 at 19:10 Comment(0)
M
0

Try to filter with a lower case or:

filter: { or: [{brand: { eq: $normalBrand }}, {brand: { eq: $normalBrand2 }},{brand: { eq: $normalBrand3 }}]}
Mundford answered 6/5, 2021 at 3:53 Comment(0)
T
0

GraphQL doesn't directly support case insensitive, but regex sure does. This worked for me:

{brand: {regexp: "/$normalBrand/i"}}
Tother answered 19/9, 2022 at 13:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.