mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 20:54:16 +03:00
Refactored is_node into relay utils
This commit is contained in:
parent
340b1ac17d
commit
2ba0a62a6c
|
@ -2,10 +2,16 @@ import inspect
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
from graphene.core.options import Options
|
from graphene.core.options import Options
|
||||||
|
from graphene.core.types import BaseObjectType
|
||||||
|
from graphene.relay.utils import is_node
|
||||||
|
|
||||||
VALID_ATTRS = ('model', 'only_fields')
|
VALID_ATTRS = ('model', 'only_fields')
|
||||||
|
|
||||||
from graphene.relay.types import Node, BaseNode
|
|
||||||
|
def is_base(cls):
|
||||||
|
from graphene.contrib.django.types import DjangoObjectType
|
||||||
|
return DjangoObjectType in cls.__bases__
|
||||||
|
|
||||||
|
|
||||||
class DjangoOptions(Options):
|
class DjangoOptions(Options):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
@ -16,7 +22,7 @@ class DjangoOptions(Options):
|
||||||
|
|
||||||
def contribute_to_class(self, cls, name):
|
def contribute_to_class(self, cls, name):
|
||||||
super(DjangoOptions, self).contribute_to_class(cls, name)
|
super(DjangoOptions, self).contribute_to_class(cls, name)
|
||||||
if cls.__name__ == 'DjangoNode':
|
if not is_node(cls) and not is_base(cls):
|
||||||
return
|
return
|
||||||
if not self.model:
|
if not self.model:
|
||||||
raise Exception('Django ObjectType %s must have a model in the Meta class attr' % cls)
|
raise Exception('Django ObjectType %s must have a model in the Meta class attr' % cls)
|
||||||
|
|
|
@ -8,3 +8,5 @@ import graphene.relay.connections
|
||||||
from graphene.relay.types import (
|
from graphene.relay.types import (
|
||||||
Node
|
Node
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from graphene.relay.utils import is_node
|
||||||
|
|
|
@ -4,11 +4,12 @@ from graphql_relay.node.node import (
|
||||||
|
|
||||||
from graphene import signals
|
from graphene import signals
|
||||||
from graphene.relay.fields import NodeIDField
|
from graphene.relay.fields import NodeIDField
|
||||||
from graphene.relay.types import BaseNode, Node
|
from graphene.relay.utils import is_node
|
||||||
|
|
||||||
|
|
||||||
@signals.class_prepared.connect
|
@signals.class_prepared.connect
|
||||||
def object_type_created(object_type):
|
def object_type_created(object_type):
|
||||||
if issubclass(object_type, BaseNode) and BaseNode not in object_type.__bases__:
|
if is_node(object_type):
|
||||||
type_name = object_type._meta.type_name
|
type_name = object_type._meta.type_name
|
||||||
field = NodeIDField()
|
field = NodeIDField()
|
||||||
object_type.add_to_class('id', field)
|
object_type.add_to_class('id', field)
|
||||||
|
|
|
@ -39,7 +39,8 @@ class BaseNode(object):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def internal_type(cls, schema):
|
def internal_type(cls, schema):
|
||||||
if cls is Node or BaseNode in cls.__bases__:
|
from graphene.relay.utils import is_node_type
|
||||||
|
if is_node_type(cls):
|
||||||
# Return only nodeInterface when is the Node Inerface
|
# Return only nodeInterface when is the Node Inerface
|
||||||
return BaseNode.get_definitions(schema).nodeInterface
|
return BaseNode.get_definitions(schema).nodeInterface
|
||||||
return super(BaseNode, cls).internal_type(schema)
|
return super(BaseNode, cls).internal_type(schema)
|
||||||
|
|
7
graphene/relay/utils.py
Normal file
7
graphene/relay/utils.py
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
from graphene.relay.types import BaseNode
|
||||||
|
|
||||||
|
def is_node(object_type):
|
||||||
|
return issubclass(object_type, BaseNode) and not is_node_type(object_type)
|
||||||
|
|
||||||
|
def is_node_type(object_type):
|
||||||
|
return BaseNode in object_type.__bases__
|
Loading…
Reference in New Issue
Block a user