graphql icon indicating copy to clipboard operation
graphql copied to clipboard

Support for date math expressions when comparing date and time fields

Open i-like-robots opened this issue 1 year ago • 0 comments

Is your feature request related to a problem? Please describe.

We have many users who wish to query data within a relative date range - for example within the last year or 30 days. This currently requires users to dynamically calculate the date, which is fine for scripts, but frustrating for non-automated queries which are run regularly. For example one of our users regularly runs a report to find a list of items which have not been updated within the last 4 weeks:

query {
  systems(
    where: {
      isArchived: false
      team: { code: "ft-interactive/data-journalism-development" }
      lastUpdated_GT: "2024-02-16"
    }
  ) {
    name
    lastUpdated
  }
}

Although they have their query saved for reuse they must remember to manually (and carefully!) amend the value of the lastUpdated filter.

Describe the solution you'd like

Support for resolving simple date math expressions for the Date and DateTime scalars would enable dates and times to be defined in a more user-friendly way. For example, rather than calculating the date 4 weeks ago and writing this as an ISO-8601 string ("2024-02-16") a user could write -4w instead:

query {
  systems(
    where: {
      isArchived: false
      team: { code: "ft-interactive/data-journalism-development" }
      lastUpdated_GT: "-4w"
    }
  ) {
    name
    lastUpdated
  }
}

Other timespans may be written as (when compared to 2024-03-14T14:32:00.000Z):

Description Actual Date Date math expression
Two hours ago 2024-03-14T12:32:00.000Z -2h
Yesterday 2024-03-14 -1d
1 week ago 2024-03-07 -1w
3 months ago 2023-12-15 -3m
2 years ago 2022-03-15 -2y

Describe alternatives you've considered

I cannot think of an alternative for users who cannot write or run dynamic scripts to execute their queries.

Additional context

We use date math expressions in our Elastic/OpenSearch queries and relative time calculations in BigQuery (e.g. TIMESTAMP_ADD(CURRENT_TIMESTAMP, INTERVAL -28 DAY)) so our users are familiar with the capability to specify relative dates and timespans.

I've put together a very quick and naive implementation here.

i-like-robots avatar Mar 15 '24 14:03 i-like-robots