Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add an OR condition in a graphql where statement?

I cannot get a WHERE statement working with an 'OR' condition in Strapi via graphql playground.

I would like to return all results where either the 'title' OR 'content' fields contain the search_text.

I have tried the following:

articles(where: { 
or: [
  {"title_contains" : "search_text"},
  {"content_contains" : "search_text"}
 ]
}) {
title
content
}

but an error is returned.

ERROR: "Your filters contain a field 'or' that doesnt appear on your model definition nor it's relations.

Some statements that work (but not what I am after):

where: { "title_contains" : "sometext" }

working, but behaves as an 'AND'

where: { 
"title_contains" : "search_text", 
"content_contains" : "search_text"
}
like image 301
Tommy K Avatar asked Aug 23 '19 03:08

Tommy K


2 Answers

As of July it's possible to do it like this

(where: { _or: [{ someField: "1" }, { someField2: "2" }] })
like image 91
Oran Gilboa Avatar answered Oct 29 '22 02:10

Oran Gilboa


The workaround here is to create a custom Query and make a custom database query that matches your need.

Here is how to create a custom GraphQL query: https://strapi.io/documentation/3.0.0-beta.x/guides/graphql.html#example-2

To access the data model, you will have to use strapi.models.article (For an Article model) and inside this variable, you will access to native Mongoose or Bookshelf function. So you will be able to query with an OR

like image 23
Jim LAURIE Avatar answered Oct 29 '22 02:10

Jim LAURIE