From 1c1e0cce15a80aef43d91db6adb48eb9e773bfca Mon Sep 17 00:00:00 2001 From: Mardanov Timur Rustemovich Date: Mon, 19 Nov 2018 21:24:46 +0300 Subject: [PATCH] support neomodel in metaclass --- graphene_django/types.py | 19 +++++++++++-------- graphene_django/utils.py | 9 ++++++--- setup.py | 2 +- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/graphene_django/types.py b/graphene_django/types.py index aa8b5a3..40f903b 100644 --- a/graphene_django/types.py +++ b/graphene_django/types.py @@ -8,7 +8,11 @@ from graphene.types.utils import yank_fields_from_attrs from .converter import convert_django_field_with_choices from .registry import Registry, get_global_registry -from .utils import DJANGO_FILTER_INSTALLED, get_model_fields, is_valid_django_model +from .utils import DJANGO_FILTER_INSTALLED, get_model_fields, is_valid_neomodel_model + +from neomodel import ( + DoesNotExist, +) def construct_fields(model, registry, only_fields, exclude_fields): @@ -57,8 +61,8 @@ class DjangoObjectType(ObjectType): _meta=None, **options ): - assert is_valid_django_model(model), ( - 'You need to pass a valid Django Model in {}.Meta, received "{}".' + assert is_valid_neomodel_model(model), ( + 'You need to pass a valid Neomodel Model in {}.Meta, received "{}".' ).format(cls.__name__, model) if not registry: @@ -82,7 +86,6 @@ class DjangoObjectType(ObjectType): ) if use_connection and not connection: - # We create the connection automatically if not connection_class: connection_class = Connection @@ -112,7 +115,7 @@ class DjangoObjectType(ObjectType): registry.register(cls) def resolve_id(self, info): - return self.pk + return self.uid @classmethod def is_type_of(cls, root, info): @@ -121,7 +124,7 @@ class DjangoObjectType(ObjectType): root = root._wrapped if isinstance(root, cls): return True - if not is_valid_django_model(type(root)): + if not is_valid_neomodel_model(type(root)): raise Exception(('Received incompatible instance "{}".').format(root)) model = root._meta.model._meta.concrete_model @@ -130,6 +133,6 @@ class DjangoObjectType(ObjectType): @classmethod def get_node(cls, info, id): try: - return cls._meta.model.objects.get(pk=id) - except cls._meta.model.DoesNotExist: + return cls._meta.model.nodes.get(uid=id) + except DoesNotExist: return None diff --git a/graphene_django/utils.py b/graphene_django/utils.py index 9e0f9ec..a35b1b3 100644 --- a/graphene_django/utils.py +++ b/graphene_django/utils.py @@ -1,7 +1,10 @@ import inspect from django.db import models -from neomodel import NodeSet +from neomodel import ( + NodeSet, + StructuredNode, +) # from graphene.utils import LazyList @@ -55,8 +58,8 @@ def get_model_fields(model): return all_fields -def is_valid_django_model(model): - return inspect.isclass(model) and issubclass(model, models.Model) +def is_valid_neomodel_model(model): + return inspect.isclass(model) and issubclass(model, StructuredNode) def import_single_dispatch(): diff --git a/setup.py b/setup.py index e025a6e..d3830fa 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ with open("graphene_django/__init__.py", "rb") as f: rest_framework_require = ["djangorestframework>=3.6.3"] -neomodel_require = ["neomodel>=3.3.0", ] +neomodel_require = [] #["neomodel>=3.3.0", ] tests_require = [ "pytest>=3.6.3",