Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL : >= now is not showing entries where date is set to today #10485

Closed
denisyilmaz opened this issue Feb 6, 2022 · 14 comments
Closed

GraphQL : >= now is not showing entries where date is set to today #10485

denisyilmaz opened this issue Feb 6, 2022 · 14 comments
Labels

Comments

@denisyilmaz
Copy link

denisyilmaz commented Feb 6, 2022

Description

I found an issue with an running project that was updated to 3.7.25.1 a while back. Before the update, we were able to query entries by a date field to show only entries where the date is today or in the future with >= now.

This is not working anymore.

The feature was introduced back in 3.7.0 after my initial issue #9117.

Steps to reproduce

  1. add a date field to a entry type
  2. set the date to today
  3. query entry type via:
entries(type:"the-type", orderBy: "date", date: ">= now") {
    id
    title
}

Additional info

  • Craft version: 3.7.25.1
@denisyilmaz denisyilmaz added the bug label Feb 6, 2022
@andris-sevcenko
Copy link
Contributor

I'm unable to reproduce this locally on Craft 3.7.30.1

Please, keep in mind, that, if you're using this on the postDate property, you'll also have to modify the status argument, to allow entries of any other status, besides enabled.

What version did you update from?

@denisyilmaz
Copy link
Author

denisyilmaz commented Feb 7, 2022

I just updated the project to 3.7.30.1 but the problem is still there. with a custom date field as well as for postDate/dateCreated.
I tried the same on several other projects which were updated to 3.7.x and they all have this issue.

the last version where this was working for sure was 3.6.17. after this this bug might already been there but was unnoticed.

Updates

  • 3.6.17
  • 3.7.22
  • 3.7.25.1
  • 3.7.30.1

@andris-sevcenko
Copy link
Contributor

Can you share the full query you are executing?

@denisyilmaz
Copy link
Author

entries(dateCreated: ">= now") {
    title
    id
}
sundays: categories(group: "sunday", orderBy: "date", date: ">= now") {
    id
    language
    title
    ... on sunday_Category {
      date @formatDateTime(timezone: "Europe/Berlin")
      timezone: date @formatDateTime(format: "O", timezone: "Europe/Berlin")
      selectedColor {
        ... on color_Category {
          hex
        }
      }
    }
  }

@denisyilmaz
Copy link
Author

I cleared all cashes and re-applied the full config file just to be sure. nothing changes.

@andris-sevcenko
Copy link
Contributor

entries(dateCreated: ">= now") {
    title
    id
}

Probably not easy to find entries that will be created in the future or at this instant. Maybe you're looking for postDate ?

I'll look into that second query.

@denisyilmaz
Copy link
Author

you are right, the example does not make sense with dateCreated.
my issue was meant specially for custom date fields, but in the process i realized this is also true queries that should get entries from same day (or with future publish date).

this is the query (where date is a custom date field) that i'm using in the project since 3.6.17 which worked and later it did not:

sundays: categories(group: "sunday", orderBy: "date", date: ">= now") {
    id
    language
    title
    ... on sunday_Category {
      date @formatDateTime(timezone: "Europe/Berlin")
      timezone: date @formatDateTime(format: "O", timezone: "Europe/Berlin")
      selectedColor {
        ... on color_Category {
          hex
        }
      }
    }
  }

@andris-sevcenko
Copy link
Contributor

Hmm, still unable to reproduce this. Thought that maybe something changed and date as a field name is throwing a wrench in somewhere.

Is it possible to send over a DB dump and your composer.json + composer.lock files over to [email protected] and reference this issue? I'd greatly appreciate it!

@benediktblank
Copy link

benediktblank commented Aug 12, 2022

I just came accross this bug today, when I was searching for a solution to another issue.
That "now" is not working in that case is still a valid issue. I'm on version 4.2.1.1.

I have these three fields:
image
eventDate, eventStartTime, eventEndTime
A date field and two time fields.

When I configure 08/12/2022 as date (MM/DD/YYYY) and save it, Craft saves "2022-08-11 22:00:00" into the database.
Which is correct I guess, because Craft saves the date as GMT+0.

I'm still wondering why it even saves a time when it's only a date field, but that's another thing.

When I try to query the entry now and filter the eventDate field, it somehow still finds my entry with this:

query getEntries {
  entries(eventDate: ">= 2022-08-12") {
    ... on events_event_Entry {
      id
      eventDate
      eventStartTime
      eventEndTime
    }
  }
}

Despite it being saved in the database as "2022-08-11 22:00:00".
Can somebody explain to me how and why that works? Somewhere, my filter gets "normalized" I guess?
But based on what? How does it know the filter I've set is GMT+2?

To come back to the original issue here, that does not get me anything:

query getEntries {
  entries(eventDate: ">= now") {
    ... on events_event_Entry {
      id
      eventDate
      eventStartTime
      eventEndTime
    }
  }
}

@brandonkelly
Copy link
Member

brandonkelly commented Aug 16, 2022

When I configure 08/12/2022 as date (MM/DD/YYYY) and save it, Craft saves "2022-08-11 22:00:00" into the database. Which is correct I guess, because Craft saves the date as GMT+0.

I'm still wondering why it even saves a time when it's only a date field, but that's another thing.

@benediktblank Date fields always store date + time, and the time is defaulted to midnight in the system time zone (converted to whatever the UTC equivalent is), if not set explicitly via the “Show date and time” option.

When I try to query the entry now and filter the eventDate field, it somehow still finds my entry with this:

query getEntries {
  entries(eventDate: ">= 2022-08-12") {
    ... on events_event_Entry {
      id
      eventDate
      eventStartTime
      eventEndTime
    }
  }
}

Because like Date field values, 2022-08-12 without an explicit time is going to default to midnight. And then converted to UTC for the actual query.

The following are all identical:

  • 2022-08-12 (GMT+2)
  • 2022-08-12 00:00:00 (GMT+2)
  • 2022-08-11 22:00:00 (UTC, as stored in the DB)

Can somebody explain to me how and why that works? Somewhere, my filter gets "normalized" I guess?
But based on what? How does it know the filter I've set is GMT+2?

All inputted dates are assumed to be set it the system time zone (SettingsGeneralTime Zone), unless explicitly set to another time zone (e.g. if the date field’s “Show Time Zone” setting is enabled, or the GraphQL param value specifies a time zone).

To come back to the original issue here, that does not get me anything:

query getEntries {
  entries(eventDate: ">= now") {
    ... on events_event_Entry {
      id
      eventDate
      eventStartTime
      eventEndTime
    }
  }
}

Probably because now references the exact current date and time. Events that are set to “today” are set to midnight of today. And midnight < now, not >=.

@denisyilmaz
Copy link
Author

denisyilmaz commented Aug 17, 2022

@brandonkelly This means the only way to query for events on the current day would be to get the day-string via the frontend and use it as a query?

query GetEntriesUpdatedToday($today:String){
  entries(orderBy: "dateUpdated DESC", dateUpdated: [$today]) {
    dateUpdated
    title
  }
}

with params

 {
  "today": ">= 2022-08-17"
}

Is there a reason why twig filters are not allowed in the query string? Only thing I can guess right now is cache reasons (as the query is not changing for now it would mean the query could return an older result)!?

With twig filters enabled it would be possible to use the same syntax as in normal queries themselves like eventDate: ">= now|date('Y-m-d')"

brandonkelly added a commit that referenced this issue Aug 17, 2022
@brandonkelly
Copy link
Member

Just resolved this for Craft 4.3: going forward, date params for both element queries and GraphQL will support today, tomorrow, and yesterday keywords (alongside now), which represent midnight of the relative specified date. So instead of >= now, you could do >= today, to mean “on/after midnight of today”.

Is there a reason why twig filters are not allowed in the query string?

Because Twig and GraphQL are two completely different languages.

@denisyilmaz
Copy link
Author

Great to hear.

Regarding twig in graphQL: I meant that the string (QueryArgument) gets parsed as twig when the query is received in Craft. But yes, would probably makes things confusing Mixing both languages.

@brandonkelly
Copy link
Member

Craft 4.3 was released earlier this week with those changes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants