Improved interfaces in objecttypes

This commit is contained in:
Syrus Akbary 2016-06-03 21:28:29 -07:00
parent 33d4f44f04
commit 58dbfefc15
4 changed files with 14 additions and 15 deletions

View File

@ -31,7 +31,7 @@ def test_node_no_get_node():
assert "MyNode.get_node method is required by the Node interface." == str(excinfo.value)
def test_node_no_get_node():
def test_node_no_get_node_with_meta():
with pytest.raises(AssertionError) as excinfo:
class MyNode(ObjectType):
class Meta:

View File

@ -162,7 +162,12 @@ class GrapheneObjectType(GrapheneFieldsType, GraphQLObjectType):
self._interfaces = None
# We clear the cached fields as could be inherited from interfaces
self._field_map = None
self._provided_interfaces.append(get_graphql_type(interface))
graphql_type = get_graphql_type(interface)
if isinstance(graphql_type, GrapheneInterfaceType):
graphql_type.graphene_type.implements(self.graphene_type)
self._provided_interfaces.append(graphql_type)
class GrapheneInterfaceType(GrapheneFieldsType, GraphQLInterfaceType):

View File

@ -40,9 +40,8 @@ class Interface(six.with_metaclass(InterfaceTypeMeta)):
@classmethod
def implements(cls, object_type):
'''
We use this function for customizing what the @implements function do
for each implementation.
For example, if we want to check that the class have some required things
We use this function for customizing when a ObjectType have this class as Interface
For example, if we want to check that the ObjectType have some required things
in it like Node.get_node
'''
object_type._meta.graphql_type.add_interface(cls)
pass

View File

@ -29,21 +29,16 @@ class ObjectTypeMeta(ClassTypeMeta):
name=cls._meta.name or cls.__name__,
description=cls._meta.description,
fields=FieldMap(cls, bases=filter(None, inherited_types)),
interfaces=map(get_graphql_type, cls._meta.interfaces),
interfaces=[],
)
for interface in cls._meta.interfaces:
cls._meta.graphql_type.add_interface(interface)
def implements(*interfaces):
from ..utils.get_graphql_type import get_graphql_type
def wrap_class(cls):
for i in interfaces:
graphql_type = get_graphql_type(i)
if isinstance(graphql_type, GrapheneInterfaceType):
graphql_type.graphene_type.implements(cls)
else:
# We add the interface in the regular way
cls._meta.graphql_type.add_interface(graphql_type)
cls._meta.graphql_type.add_interface(i)
return cls
return wrap_class