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 .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

View File

@ -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():

View File

@ -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",