Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter GraphQL articles by datetime

Tags:

graphql

strapi

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.

like image 441
João Belo Avatar asked Oct 22 '25 04:10

João Belo


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.

like image 53
João Belo Avatar answered Oct 25 '25 17:10

João Belo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!