support neomodel in metaclass

This commit is contained in:
Mardanov Timur Rustemovich 2018-11-19 21:24:46 +03:00
parent be2e7111b7
commit 1c1e0cce15
3 changed files with 18 additions and 12 deletions

View File

@ -8,7 +8,11 @@ from graphene.types.utils import yank_fields_from_attrs
from .converter import convert_django_field_with_choices from .converter import convert_django_field_with_choices
from .registry import Registry, get_global_registry 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): def construct_fields(model, registry, only_fields, exclude_fields):
@ -57,8 +61,8 @@ class DjangoObjectType(ObjectType):
_meta=None, _meta=None,
**options **options
): ):
assert is_valid_django_model(model), ( assert is_valid_neomodel_model(model), (
'You need to pass a valid Django Model in {}.Meta, received "{}".' 'You need to pass a valid Neomodel Model in {}.Meta, received "{}".'
).format(cls.__name__, model) ).format(cls.__name__, model)
if not registry: if not registry:
@ -82,7 +86,6 @@ class DjangoObjectType(ObjectType):
) )
if use_connection and not connection: if use_connection and not connection:
# We create the connection automatically
if not connection_class: if not connection_class:
connection_class = Connection connection_class = Connection
@ -112,7 +115,7 @@ class DjangoObjectType(ObjectType):
registry.register(cls) registry.register(cls)
def resolve_id(self, info): def resolve_id(self, info):
return self.pk return self.uid
@classmethod @classmethod
def is_type_of(cls, root, info): def is_type_of(cls, root, info):
@ -121,7 +124,7 @@ class DjangoObjectType(ObjectType):
root = root._wrapped root = root._wrapped
if isinstance(root, cls): if isinstance(root, cls):
return True 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)) raise Exception(('Received incompatible instance "{}".').format(root))
model = root._meta.model._meta.concrete_model model = root._meta.model._meta.concrete_model
@ -130,6 +133,6 @@ class DjangoObjectType(ObjectType):
@classmethod @classmethod
def get_node(cls, info, id): def get_node(cls, info, id):
try: try:
return cls._meta.model.objects.get(pk=id) return cls._meta.model.nodes.get(uid=id)
except cls._meta.model.DoesNotExist: except DoesNotExist:
return None return None

View File

@ -1,7 +1,10 @@
import inspect import inspect
from django.db import models from django.db import models
from neomodel import NodeSet from neomodel import (
NodeSet,
StructuredNode,
)
# from graphene.utils import LazyList # from graphene.utils import LazyList
@ -55,8 +58,8 @@ def get_model_fields(model):
return all_fields return all_fields
def is_valid_django_model(model): def is_valid_neomodel_model(model):
return inspect.isclass(model) and issubclass(model, models.Model) return inspect.isclass(model) and issubclass(model, StructuredNode)
def import_single_dispatch(): def import_single_dispatch():

View File

@ -12,7 +12,7 @@ with open("graphene_django/__init__.py", "rb") as f:
rest_framework_require = ["djangorestframework>=3.6.3"] rest_framework_require = ["djangorestframework>=3.6.3"]
neomodel_require = ["neomodel>=3.3.0", ] neomodel_require = [] #["neomodel>=3.3.0", ]
tests_require = [ tests_require = [
"pytest>=3.6.3", "pytest>=3.6.3",