Skip to content

Commit

Permalink
Merge pull request #98 from graphql-python/feat-support-sequence-field
Browse files Browse the repository at this point in the history
Feat support sequence field
  • Loading branch information
abawchen authored Jun 21, 2019
2 parents 1d6a933 + 0a73b71 commit baed65b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 36 deletions.
57 changes: 23 additions & 34 deletions graphene_mongo/converter.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import graphene
import mongoengine
import uuid
from graphene import (
ID,
Boolean,
DateTime,
Dynamic,
Field,
Float,
Int,
List,
NonNull,
String,
Union,
is_node
)

from graphene.types.json import JSONString
from mongoengine.base import get_document

Expand All @@ -40,35 +28,36 @@ def convert_mongoengine_field(field, registry=None):
@convert_mongoengine_field.register(mongoengine.StringField)
@convert_mongoengine_field.register(mongoengine.URLField)
def convert_field_to_string(field, registry=None):
return String(description=get_field_description(field, registry), required=field.required)
return graphene.String(description=get_field_description(field, registry), required=field.required)


@convert_mongoengine_field.register(mongoengine.UUIDField)
@convert_mongoengine_field.register(mongoengine.ObjectIdField)
def convert_field_to_id(field, registry=None):
return ID(description=get_field_description(field, registry), required=field.required)
return graphene.ID(description=get_field_description(field, registry), required=field.required)


@convert_mongoengine_field.register(mongoengine.IntField)
@convert_mongoengine_field.register(mongoengine.LongField)
@convert_mongoengine_field.register(mongoengine.SequenceField)
def convert_field_to_int(field, registry=None):
return Int(description=get_field_description(field, registry), required=field.required)
return graphene.Int(description=get_field_description(field, registry), required=field.required)


@convert_mongoengine_field.register(mongoengine.BooleanField)
def convert_field_to_boolean(field, registry=None):
return Boolean(description=get_field_description(field, registry), required=field.required)
return graphene.Boolean(description=get_field_description(field, registry), required=field.required)


@convert_mongoengine_field.register(mongoengine.DecimalField)
@convert_mongoengine_field.register(mongoengine.FloatField)
def convert_field_to_float(field, registry=None):
return Float(description=get_field_description(field, registry), required=field.required)
return graphene.Float(description=get_field_description(field, registry), required=field.required)


@convert_mongoengine_field.register(mongoengine.DateTimeField)
def convert_field_to_datetime(field, registry=None):
return DateTime(description=get_field_description(field, registry), required=field.required)
return graphene.DateTime(description=get_field_description(field, registry), required=field.required)


@convert_mongoengine_field.register(mongoengine.DictField)
Expand All @@ -79,44 +68,44 @@ def convert_field_to_jsonstring(field, registry=None):

@convert_mongoengine_field.register(mongoengine.PointField)
def convert_point_to_field(field, registry=None):
return Field(advanced_types.PointFieldType)
return graphene.Field(advanced_types.PointFieldType)


@convert_mongoengine_field.register(mongoengine.PolygonField)
def convert_polygon_to_field(field, registry=None):
return Field(advanced_types.PolygonFieldType)
return graphene.Field(advanced_types.PolygonFieldType)


@convert_mongoengine_field.register(mongoengine.MultiPolygonField)
def convert_multipolygon_to_field(field, register=None):
return Field(advanced_types.MultiPolygonFieldType)
return graphene.Field(advanced_types.MultiPolygonFieldType)


@convert_mongoengine_field.register(mongoengine.FileField)
def convert_file_to_field(field, registry=None):
return Field(advanced_types.FileFieldType)
return graphene.Field(advanced_types.FileFieldType)


@convert_mongoengine_field.register(mongoengine.ListField)
@convert_mongoengine_field.register(mongoengine.EmbeddedDocumentListField)
def convert_field_to_list(field, registry=None):
base_type = convert_mongoengine_field(field.field, registry=registry)
if isinstance(base_type, (Dynamic)):
if isinstance(base_type, (graphene.Dynamic)):
base_type = base_type.get_type()
if base_type is None:
return
base_type = base_type._type

if is_node(base_type):
if graphene.is_node(base_type):
return base_type._meta.connection_field_class(base_type)

# Non-relationship field
relations = (mongoengine.ReferenceField, mongoengine.EmbeddedDocumentField)
if not isinstance(base_type, (List, NonNull)) \
if not isinstance(base_type, (graphene.List, graphene.NonNull)) \
and not isinstance(field.field, relations):
base_type = type(base_type)

return List(base_type, description=get_field_description(field, registry), required=field.required)
return graphene.List(base_type, description=get_field_description(field, registry), required=field.required)


@convert_mongoengine_field.register(mongoengine.GenericReferenceField)
Expand All @@ -143,8 +132,8 @@ def convert_field_to_union(field, registry=None):
str(uuid.uuid1()).replace('-', '')
)
Meta = type('Meta', (object, ), {'types': tuple(_types)})
_union = type(name, (Union, ), {'Meta': Meta})
return Field(_union)
_union = type(name, (graphene.Union, ), {'Meta': Meta})
return graphene.Field(_union)


@convert_mongoengine_field.register(mongoengine.EmbeddedDocumentField)
Expand All @@ -156,9 +145,9 @@ def dynamic_type():
_type = registry.get_type_for_model(model)
if not _type:
return None
return Field(_type, description=get_field_description(field, registry))
return graphene.Field(_type, description=get_field_description(field, registry))

return Dynamic(dynamic_type)
return graphene.Dynamic(dynamic_type)


@convert_mongoengine_field.register(mongoengine.LazyReferenceField)
Expand All @@ -173,6 +162,6 @@ def dynamic_type():
_type = registry.get_type_for_model(model)
if not _type:
return None
return Field(_type, resolver=lazy_resolver, description=get_field_description(field, registry))
return graphene.Field(_type, resolver=lazy_resolver, description=get_field_description(field, registry))

return Dynamic(dynamic_type)
return graphene.Dynamic(dynamic_type)
3 changes: 2 additions & 1 deletion graphene_mongo/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

gridfs.enable_gridfs_integration()
mongoengine.connect('graphene-mongo-test', host='mongomock://localhost', alias='default')
# mongoengine.connect('graphene-mongo-test', host='mongodb://192.168.15.91/graphene-mongo-dev')
# mongoengine.connect('graphene-mongo-test', host='mongodb://localhost/graphene-mongo-dev')


class Publisher(mongoengine.Document):
Expand Down Expand Up @@ -32,6 +32,7 @@ class Editor(mongoengine.Document):
metadata = mongoengine.MapField(field=mongoengine.StringField(), help_text="Arbitrary metadata.")
company = mongoengine.LazyReferenceField(Publisher)
avatar = mongoengine.FileField()
seq = mongoengine.SequenceField()


class Article(mongoengine.Document):
Expand Down
4 changes: 4 additions & 0 deletions graphene_mongo/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def test_sould_long_convert_int():
assert_conversion(mongoengine.LongField, graphene.Int)


def test_sould_sequence_convert_field():
assert_conversion(mongoengine.SequenceField, graphene.Int)


def test_should_object_id_convert_id():
assert_conversion(mongoengine.ObjectIdField, graphene.ID)

Expand Down
2 changes: 1 addition & 1 deletion graphene_mongo/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_reporter_field_args():
def test_editor_field_args():
field = MongoengineConnectionField(nodes.EditorNode)

field_args = {'id', 'first_name', 'last_name', 'metadata'}
field_args = {'id', 'first_name', 'last_name', 'metadata', 'seq'}
assert set(field.field_args.keys()) == field_args


Expand Down

0 comments on commit baed65b

Please sign in to comment.