graphql-python / graphql-python/graphene-django

Input of type date required when filtering using double underscore day, month, or year in date fields.

Open
#1,314 3 comments 2 reactions 0 assignees View on GitHub
🐛bug
Dominant language
Python
Stars
4.4k
Forks
760
PR merge metrics
No merged PRs in 30d

Description

**Current behavior**
Given a node like this:
```python

class EventNode(DjangoObjectType):
class Meta:
model = Event
filter_fields = {
"event_date": ["exact", "year", "month", "day"],
"status": ["exact"],
}
interfaces = (relay.Node,)

```
and a query structure like this:

```graph
query filterEvents{
allEvents(){
edges{
node{
id
eventDate
}
}
}
}
```
In order to perform a filter for all events in the year 2022, I tried the following:
```
allEvents(eventDate_Year:"2022")
> "message": "['{\"event_date__year\": [{\"message\": \"Enter a number.\", \"code\": \"invalid\"}]}']",

allEvents(eventDate_Year:2022)
> "message": "Argument \"eventDate_Year\" has invalid value 2022.\nExpected type \"Date\", found 2022.",

allEvents(eventDate_Year:"2022-02-14")
> "message": "['{\"event_date__year\": [{\"message\": \"Enter a number.\", \"code\": \"invalid\"}]}']",

allEvents(eventDate_Year:"+2022")
> "message": "ISO 8601 extended year representation not supported."
```
I tried using `django_filters` but the field `eventDate_Year` ,`eventDate_Month` and `eventDate_Day` arguments where missing in the resulting api.
```python
class EventFilter(django_filters.FilterSet):
class Meta:
model = Event
fields = "__all__"
filter_fields = {
"event_date": [ "exact","year","month", "day" ],
"status": ["exact"],
}

class EventNode(DjangoObjectType):
class Meta:
model = Event
filterset_class = EventFilter
interfaces = (relay.Node,)
```
**Expected Behavior**
I was expecting a `eventDate_Year` to behave like the django double underscore fields queries, e.g. `date__year` and take either a string or number as input, e.g.
```python
Model.objects.filter(date_field__year="2022")
```
It is not possible to pass year only as a valid date.

**What is the motivation / use case for changing the behavior?**
I would like to be able to filter a large data set by time period. So if only data from April 2022 is required, that is what is asked for. It doesn't seem very efficient to explicitly declare the same filters for every date field in every node, instead of specifying in the filter fields. Specifying a from-to date filter might be a viable solution, but it wouldn't work if data for say the 5th of every month in 2022 is required.

**Please tell us about your environment:**
- Version: 2.15.0
- Platform:
- Local: Arch Linux 5.16.13-arch1-1
- Remotes: Ubuntu 20.04

**Other information**
I am not sure if this is a bug or a problem with my implementation. Before creating this issue,[ I asked on stack overflow](https://stackoverflow.com/questions/71424540/how-to-filter-by-year-month-or-day-in-django-graphene-query), but got no response.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.