Logical operators (or/and) in GraphQL where parameter #7795

This discussion has been migrated from our Github Discussion #7795


skourismanolis104d ago

Hi, I couldn’t find a way to use “and” and “or” operators in graphql. As I understand from #3194 the functionality exists, but I couldn’t find any documentation. What I’m trying to do is something like:

people( where: { OR: [ { first_name_contains: “something” } { last_name_contains: “something else” } ] } ) { id first_name last_name } }

I also tried using $or,or and $OR in place of OR but no dice.

Responses to the discussion on Github


derrickmehaffy104d ago

Collaborator

@alexandrebodin I believe you had a way to handle this with the new complex queries but I can’t seem to find the reference. Can you describe how we use those and I can try to write documentation for it?


alexandrebodin104d ago

Maintainer

rename the OR with _or. the syntax inside of where is the same as the one described in the parameters doc here: https://strapi.io/documentation/v3.x/content-api/parameters.html#complex-queries.


skourismanolis104d ago

Author

Thanks a lot! This worked, I initially read that part but since it was referring to REST filtering and I found the resulting query string quite complex I didn’t think of applying it to GraphQL. It would be amazing if you wrote documentation for it @derrickmehaffy so it can be clearer.


wujun4code75d ago

@aalasolutions how abount AND ?
i try the following query, it did not work:

query {
  products(where:{ _and: [ { labels: { name:"category",value: "blade" }},
    { labels: { name:"category",value: "featured" }}]}) {
    name
    unique_name
    labels {
      name
    }
  }
}

Thanks,


wujun4code75d ago

my related issue is from graphcool - GraphQL: Filter data in an array - Stack Overflow, i use a category like a tag for product.


[

wujun4code](wujun4code (Wu Jun) · GitHub)75d ago

my rest api request is :

curl --location --request GET 'localhost:1337/products?_where[0][labels.name]=category&_where[1][labels.value]=blade&_where[2][labels.name]=category&_where[3][labels.value]=featured'

it works well.


alexandrebodin74d ago

Maintainer

THere are no and this is implicit in the _where

You can add

query {
  products(where:{ _where: [ { labels: { name:"category",value: "blade" }},
    { labels: { name:"category",value: "featured" }}]}) {
    name
    unique_name
    labels {
      name
    }
  }
}


wujun4code72d ago

@alexandrebodin but i got the following error:

{
    "errors": [
        {
            "message": "Cast to ObjectId failed for value \"{ name: 'category', value: 'pupular-items' }\" at path \"labels\" for model \"product\"",
            "locations": [
                {
                    "line": 2,
                    "column": 3
                }
            ],
            "path": [
                "products"
            ],
            "extensions": {
                "code": "INTERNAL_SERVER_ERROR",
                "exception": {
                    "stringValue": "\"{ name: 'category', value: 'pupular-items' }\"",
                    "kind": "ObjectId",
                    "value": {
                        "name": "category",
                        "value": "pupular-items"
                    },
                    "path": "labels",
                    "reason": {}
                }
            }
        }
    ],
    "data": {
        "products": null
    }
}

Hey @wujun4code I’ve been feeling your pain but the above advice from @alexandrebodin does work. However it’s surely clear that this syntax of nested statements and objects is really ugly and easy to get wrong. Any plans to improve it? seems like a fairly common use case. For me I’m querying for tagged products which match all the tags in the query