Filter GraphQL articles by datetime

Viewed 4993

I'm using Strapi with GraphQL. I need to query articles that have the publish date after the current date. The intention behind this is to allow editors to publish with future dates so they can plan ahead. Right now I have only this:

export const ARTICLES_QUERY = gql`
  query Articles {
    articles(where: { display: true }) {
      id
      slug
      title
      publish
      display
      time_to_read
      article_categories {
        slug
        title
      }
      user {
        username
        name
      }
      cover {
        url
      }
    }
  }
`

I think I need something like this:

export const ARTICLES_QUERY = gql`
  query Articles($today: String!) {
    articles(where: { display: true, publish >= $today }) {
      id
      slug
...

The format of that string is Strapi's default for the date time input and the result is "publish": "2020-02-28T02:00:00.000Z"

I'm aware this is not the way to go, but it illustrates what I need to acomplish.

1 Answers

Figured it out.

GraphQL allows us to set it like so:

export const ARTICLES_QUERY = gql`
  query Articles($today: String!) {
    articles(where: { display: true, publish_lt: $today }) {
      id
      slug
...

Using _lt or _gt on the field name indicates that we want to filter the list for dates before (_lt) or after (_gt) the set date.

Related