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

resolve subtypes of postgres RangeField to new RangeType #677

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion graphene_django/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .types import DjangoObjectType
from .fields import DjangoConnectionField

__version__ = "2.3.0"
__version__ = "2.3.2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert this? The version number will be incremented when we create a new release.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check. I didn't remember I changed version, could it be due to merge from some branch?


__all__ = ["__version__", "DjangoObjectType", "DjangoConnectionField"]
10 changes: 9 additions & 1 deletion graphene_django/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ class MissingType(object):
HStoreField,
JSONField,
RangeField,
DateTimeRangeField,
DateRangeField,
IntegerRangeField,
BigIntegerRangeField,
FloatRangeField,
DecimalRangeField
)
except ImportError:
ArrayField, HStoreField, JSONField, RangeField = (MissingType,) * 4
ArrayField, HStoreField, JSONField, RangeField, \
DateTimeRangeField, DateRangeField, IntegerRangeField, \
BigIntegerRangeField, FloatRangeField, DecimalRangeField = (MissingType,) * 10
39 changes: 38 additions & 1 deletion graphene_django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,26 @@
from graphene.utils.str_converters import to_camel_case, to_const
from graphql import assert_valid_name

from .compat import ArrayField, HStoreField, JSONField, RangeField
from .compat import (
ArrayField,
HStoreField,
JSONField,
RangeField,
DateTimeRangeField,
DateRangeField,
IntegerRangeField,
BigIntegerRangeField,
FloatRangeField,
DecimalRangeField
)

from .range_types import (
DateRangeType,
DateTimeRangeType,
IntRangeType,
FloatRangeType
)

from .fields import DjangoListField, DjangoConnectionField
from .utils import import_single_dispatch

Expand Down Expand Up @@ -236,3 +255,21 @@ def convert_posgres_range_to_string(field, registry=None):
if not isinstance(inner_type, (List, NonNull)):
inner_type = type(inner_type)
return List(inner_type, description=field.help_text, required=not field.null)

@convert_django_field.register(DateTimeRangeField)
def convert_posgres_datetime_range_to_datetime(field, registry=None):
return Field(DateTimeRangeType, description=field.help_text, required=not field.null)

@convert_django_field.register(DateRangeField)
def convert_posgres_date_range_to_date(field, registry=None):
return Field(DateRangeType, description=field.help_text, required=not field.null)

@convert_django_field.register(IntegerRangeField)
@convert_django_field.register(BigIntegerRangeField)
def convert_posgres_int_range_to_int(field, registry=None):
return Field(IntRangeType, description=field.help_text, required=not field.null)

@convert_django_field.register(FloatRangeField)
@convert_django_field.register(DecimalRangeField)
def convert_posgres_float_range_to_float(field, registry=None):
return Field(FloatRangeType, description=field.help_text, required=not field.null)
29 changes: 29 additions & 0 deletions graphene_django/range_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from graphene import ObjectType
from graphene import (
Float,
Int,
DateTime,
Date
)

class RangeResolver:
def resolve_lower(parent, info):
return parent.lower
def resolve_upper(parent, info):
return parent.upper
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the resolvers just pick attributes on the parent object that are the same name as the field then you don't need these resolvers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try


class DateTimeRangeType(RangeResolver, ObjectType):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to http://initd.org/psycopg/docs/extras.html#adapt-range bounds and empty fields are missing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bounds is not accessible. you said missing is referring to mutation?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkimbo here is attributes:

Object attributes:

isempty
True if the range is empty.

lower
The lower bound of the range. None if empty or unbound.

upper
The upper bound of the range. None if empty or unbound.

lower_inc
True if the lower bound is included in the range.

upper_inc
True if the upper bound is included in the range.

lower_inf
True if the range doesn’t have a lower bound.

upper_inf
True if the range doesn’t have an upper bound.

I see original method either not implement it, so I didn't too.

lower = DateTime()
upper = DateTime()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these fields required?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite, I think it could be optional.

One problem is the original method which returns a tuple for (lower, upper), it is working for IntegerRange which is tested in test cases, but it is not working for DateTimeRange.


class DateRangeType(RangeResolver, ObjectType):
lower = Date()
upper = Date()

class IntRangeType(RangeResolver, ObjectType):
lower = Int()
upper = Int()

class FloatRangeType(RangeResolver, ObjectType):
lower = Float()
upper = Float()
1 change: 1 addition & 0 deletions graphene_django/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,4 @@ def get_node(cls, info, id):
class ErrorType(ObjectType):
field = graphene.String(required=True)
messages = graphene.List(graphene.NonNull(graphene.String), required=True)