Skip to content

Commit cb055a7

Browse files
authored
Merge pull request #78 from graphql-python/feat-polygon
feat: Support PolygonField
2 parents 9690d8d + e010d5c commit cb055a7

File tree

7 files changed

+76
-26
lines changed

7 files changed

+76
-26
lines changed

graphene_mongo/advanced_types.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,33 @@ def _resolve_type_coordinates(self, info):
1111
return self['coordinates']
1212

1313

14-
class PointFieldType(graphene.ObjectType):
14+
class _CoordinatesField(graphene.ObjectType):
1515

1616
type = graphene.String()
17-
coordinates = graphene.List(
18-
graphene.Float, resolver=_resolve_type_coordinates)
1917

2018
def resolve_type(self, info):
2119
return self['type']
2220

2321

24-
class MultiPolygonFieldType(graphene.ObjectType):
22+
class PointFieldType(_CoordinatesField):
2523

26-
type = graphene.String()
2724
coordinates = graphene.List(
28-
graphene.List(
29-
graphene.List(
30-
graphene.List(graphene.Float))),
31-
resolver=_resolve_type_coordinates)
25+
graphene.Float, resolver=_resolve_type_coordinates)
3226

33-
def resolve_type(self, info):
34-
return self['type']
27+
28+
class PolygonFieldType(_CoordinatesField):
29+
30+
coordinates = graphene.List(
31+
graphene.List(
32+
graphene.List(graphene.Float)),
33+
resolver=_resolve_type_coordinates
34+
)
35+
36+
37+
class MultiPolygonFieldType(_CoordinatesField):
38+
39+
coordinates = graphene.List(
40+
graphene.List(
41+
graphene.List(
42+
graphene.List(graphene.Float))),
43+
resolver=_resolve_type_coordinates)

graphene_mongo/converter.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import mongoengine
1717

18-
from .advanced_types import PointFieldType, MultiPolygonFieldType
18+
from . import advanced_types
1919
from .utils import import_single_dispatch, get_field_description
2020

2121
singledispatch = import_single_dispatch()
@@ -70,12 +70,17 @@ def convert_dict_to_jsonstring(field, registry=None):
7070

7171
@convert_mongoengine_field.register(mongoengine.PointField)
7272
def convert_point_to_field(field, register=None):
73-
return Field(PointFieldType)
73+
return Field(advanced_types.PointFieldType)
74+
75+
76+
@convert_mongoengine_field.register(mongoengine.PolygonField)
77+
def convert_polygon_to_field(field, register=None):
78+
return Field(advanced_types.PolygonFieldType)
7479

7580

7681
@convert_mongoengine_field.register(mongoengine.MultiPolygonField)
7782
def convert_multipolygon_to_field(field, register=None):
78-
return Field(MultiPolygonFieldType)
83+
return Field(advanced_types.MultiPolygonFieldType)
7984

8085

8186
@convert_mongoengine_field.register(mongoengine.DateTimeField)

graphene_mongo/tests/models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from mongoengine.fields import (
66
DateTimeField, EmailField, EmbeddedDocumentField,
77
FloatField, EmbeddedDocumentListField, ListField, LazyReferenceField,
8-
MapField, PointField, ReferenceField, StringField,
9-
MultiPolygonField
8+
MapField, MultiPolygonField, PointField, PolygonField,
9+
ReferenceField, StringField,
1010
)
1111

1212
connect('graphene-mongo-test', host='mongomock://localhost', alias='default')
@@ -98,6 +98,7 @@ class CellTower(Document):
9898
'collection': 'test_cell_tower',
9999
}
100100
code = StringField()
101+
base = PolygonField()
101102
coverage_area = MultiPolygonField()
102103

103104

graphene_mongo/tests/setup.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,25 @@ def fixtures():
104104
child2.save()
105105

106106
CellTower.drop_collection()
107-
ct = CellTower(code='bar', coverage_area=[[[
108-
[-43.36556, -22.99669],
109-
[-43.36539, -23.01928],
110-
[-43.26583, -23.01802],
111-
[-43.36717, -22.98855],
112-
[-43.36636, -22.99351],
113-
[-43.36556, -22.99669]]]])
107+
ct = CellTower(
108+
code='bar',
109+
base=[[
110+
[-43.36556, -22.99669],
111+
[-43.36539, -23.01928],
112+
[-43.26583, -23.01802],
113+
[-43.36717, -22.98855],
114+
[-43.36636, -22.99351],
115+
[-43.36556, -22.99669]
116+
]],
117+
coverage_area=[[[
118+
[-43.36556, -22.99669],
119+
[-43.36539, -23.01928],
120+
[-43.26583, -23.01802],
121+
[-43.36717, -22.98855],
122+
[-43.36636, -22.99351],
123+
[-43.36556, -22.99669]
124+
]]]
125+
)
114126
ct.save()
115127
ProfessorVector.drop_collection()
116128
professor_metadata = ProfessorMetadata(
@@ -148,4 +160,4 @@ def fixtures():
148160
child3.parent = child4.parent = parent
149161
child3.save()
150162
child4.save()
151-
return True
163+
return True

graphene_mongo/tests/test_converter.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ def test_should_point_convert_field():
9090
assert isinstance(graphene_type.type.coordinates, graphene.List)
9191

9292

93+
def test_should_polygon_covert_field():
94+
graphene_type = convert_mongoengine_field(mongoengine.PolygonField())
95+
assert isinstance(graphene_type, graphene.Field)
96+
assert isinstance(graphene_type.type.type, graphene.String)
97+
assert isinstance(graphene_type.type.coordinates, graphene.List)
98+
99+
93100
def test_should_multipolygon_convert_field():
94101
graphene_type = convert_mongoengine_field(mongoengine.MultiPolygonField())
95102
assert isinstance(graphene_type, graphene.Field)

graphene_mongo/tests/test_query.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ def resolve_cell_towers(self, *args, **kwargs):
328328
query Query {
329329
cellTowers {
330330
code,
331+
base {
332+
type,
333+
coordinates
334+
},
331335
coverageArea {
332336
type,
333337
coordinates
@@ -339,6 +343,17 @@ def resolve_cell_towers(self, *args, **kwargs):
339343
'cellTowers': [
340344
{
341345
'code': 'bar',
346+
'base': {
347+
'type': 'Polygon',
348+
'coordinates': [[
349+
[-43.36556, -22.99669],
350+
[-43.36539, -23.01928],
351+
[-43.26583, -23.01802],
352+
[-43.36717, -22.98855],
353+
[-43.36636, -22.99351],
354+
[-43.36556, -22.99669]
355+
]]
356+
},
342357
'coverageArea': {
343358
'type': 'MultiPolygon',
344359
'coordinates': [[[
@@ -347,7 +362,8 @@ def resolve_cell_towers(self, *args, **kwargs):
347362
[-43.26583, -23.01802],
348363
[-43.36717, -22.98855],
349364
[-43.36636, -22.99351],
350-
[-43.36556, -22.99669]]]]
365+
[-43.36556, -22.99669]
366+
]]]
351367
}
352368
}
353369
]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='graphene-mongo',
5-
version='0.2.0',
5+
version='0.2.1',
66

77
description='Graphene Mongoengine integration',
88
long_description=open('README.rst').read(),

0 commit comments

Comments
 (0)