From 517c9ba3be2efee10f3f9cb965d67e1a20755094 Mon Sep 17 00:00:00 2001 From: abawchen Date: Thu, 20 Jun 2019 10:11:06 +0800 Subject: [PATCH] feat: Support SequenceField --- graphene_mongo/converter.py | 57 +++++++++++--------------- graphene_mongo/tests/test_converter.py | 4 ++ 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/graphene_mongo/converter.py b/graphene_mongo/converter.py index 247ecea2..33f47e8a 100644 --- a/graphene_mongo/converter.py +++ b/graphene_mongo/converter.py @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/graphene_mongo/tests/test_converter.py b/graphene_mongo/tests/test_converter.py index 754aa8a1..03ba6db7 100644 --- a/graphene_mongo/tests/test_converter.py +++ b/graphene_mongo/tests/test_converter.py @@ -50,6 +50,10 @@ def test_sould_long_convert_int(): assert_conversion(mongoengine.LongField, graphene.Int) +def test_sould_sequence_convert_int(): + assert_conversion(mongoengine.SequenceField, graphene.Int) + + def test_should_object_id_convert_id(): assert_conversion(mongoengine.ObjectIdField, graphene.ID)