Simplified Node type

This commit is contained in:
Syrus Akbary 2017-07-12 01:57:15 -07:00
parent 7a6d741531
commit a023aeba62
3 changed files with 18 additions and 18 deletions

View File

@ -1,3 +1,4 @@
from collections import OrderedDict
from functools import partial
import six
@ -58,15 +59,21 @@ class NodeField(Field):
return partial(self.node_type.node_resolver, only_type=get_type(self.field_type))
class Node(Interface):
'''An object with an ID'''
class AbstractNode(Interface):
class Meta:
abstract = True
@classmethod
def __init_subclass_with_meta__(cls, **options):
_meta = InterfaceOptions(cls)
_meta.fields = {
'id': GlobalID(cls, description='The ID of the object.')
}
super(Node, cls).__init_subclass_with_meta__(cls, _meta=_meta, **options)
_meta.fields = OrderedDict(
id=GlobalID(cls, description='The ID of the object.')
)
super(AbstractNode, cls).__init_subclass_with_meta__(_meta=_meta, **options)
class Node(AbstractNode):
'''An object with an ID'''
@classmethod
def Field(cls, *args, **kwargs): # noqa: N802

View File

@ -2,12 +2,12 @@ from collections import OrderedDict
from graphql_relay import to_global_id
from ...types import AbstractType, ObjectType, Schema, String
from ...types import ObjectType, Schema, String
from ..connection import Connection
from ..node import Node
class SharedNodeFields(AbstractType):
class SharedNodeFields(object):
shared = String()
something_else = String()
@ -54,15 +54,6 @@ def test_node_good():
assert 'id' in MyNode._meta.fields
def test_node_get_connection():
connection = MyNode.Connection
assert issubclass(connection, Connection)
def test_node_get_connection_dont_duplicate():
assert MyNode.Connection == MyNode.Connection
def test_node_query():
executed = schema.execute(
'{ node(id:"%s") { ... on MyNode { name } } }' % Node.to_global_id("MyNode", 1)

View File

@ -19,7 +19,9 @@ class Interface(BaseType):
when the field is resolved.
'''
@classmethod
def __init_subclass_with_meta__(cls, _meta=None, **options):
def __init_subclass_with_meta__(cls, abstract=False, _meta=None, **options):
if abstract:
return
if not _meta:
_meta = InterfaceOptions(cls)