AND filter on query

Hey there,

I’m currently trying to figure out if the DatoAPI supports the AND-operator on filters?
In other GraphQL tutorials I see that the same way the OR-operator works, there should be an AND-operator: https://grandstack.io/docs/graphql-filtering/#logical-operators-and-or

But in reality the API tells me InputObject 'ShoeModelFilter' doesn't accept argument 'AND'.

In my use case I have a field on a record which specifies a certain “category”, let’s say the available categories are: Coming Soon, In Stock, Countdown, Sold Out
I want to query all records, which are not Coming Soon and not In Stock.
So every record that would have Countdown set, or Sold Out set etc.
If I use the OR-operator, obv. this won’t work since a record with Coming Soon would match the neq: "In Stock" filter and vice versa.

The filter I tried to use is:

AND: [
        {shoeShowInReleases: {eq: "true"}},
        {releaseStatus: {neq: "Coming Soon"}},
        {releaseStatus: {neq: "Countdown"}}
      ]

Does the API really not support the AND-operator? And is there a good reason for it? I think this is quite a needed feature for an API.

Thanks for a heads up into the right direction.

Stay Safe!

hello @heat-mvmnt

the Content Delivery API supports both AND and OR in the filters.

Have a look at the beginning of this page: https://www.datocms.com/docs/content-delivery-api/filtering-records there are examples for both cases. Does that helps?

Hi, I have a similar requirement. I’m trying to use multiple OR filters. I see in the docs there is this example:

query {
  allAlbums(
    filter: {
      { artist: { eq: "212" } },
      { releaseDate: { gt: "2016-01-01" } }
    }
  ) {
    id
    slug
    artist { name }
    coverImage { url }
  }
}

However this syntax isn’t working for me - I can’t put another object inside the filter object - Dato complains about the double brackets. Which also makes sense to me. Can someone provide a working example of how multiple OR conditions could be achieved. Simplifying things a bit, I’m looking to implement this kind of logic in my query:

select events where (isOpen = true OR isLive = true) AND (registrationUrl != '' OR registrationId != '')

1 Like

Hello @alex.m

The query you are showing to do is an AND query, and for it to work it should be:

query {
  allAlbums(
    filter: {
       artist: { eq: "212" } ,
       releaseDate: { gt: "2016-01-01" }
    }
  ) {
    id
    slug
    artist { name }
    coverImage { url }
  }
}

That query means, a record that has an artist eq to 212 AND has the release date greater than “2016-01-01”

If you wanted an OR, it should be:

query {
  allAlbums(
    filter: {
       artist: { eq: "212" } ,
      OR: {title: {eq: "Some title"},  releaseDate: { gt: "2016-01-01" }}
    }
  ) {
    id
    slug
    artist { name }
    coverImage { url }
  }
}

This would get all records that have the artist being 212 AND have a release date greater than “2016-01-01” or a title eq to “Some title”

Thanks @m.finamor . However this doesn’t really cover my situation. I still don’t see a way to have two different OR conditions. In your example, if I wanted to change the artist to be 212 or 215, how could I implement this? I’ve tried to do it as below but the second OR condition overwrites the first one.

query {
  allAlbums(
    filter: {
       OR: [{artist: { eq: "212" } }, {artist: {eq: "215"}}],
      OR: {title: {eq: "Some title"},  releaseDate: { gt: "2016-01-01" }}
    }
  ) {
    id
    slug
    artist { name }
    coverImage { url }
  }
}

Does anyone have other suggestions for this? Does this feature not exist right now?

Hello @alex.m, sorry for the delayed response.

Unfortunately for the moment you can’t have multiple OR queries to filter records: