|
1 | 1 |
|
2 | 2 | from graphene import Field, Int, Interface, ObjectType
|
3 | 3 | from graphene.relay import Node, is_node
|
| 4 | +import six |
4 | 5 |
|
5 | 6 | from ..registry import Registry
|
6 |
| -from ..types import SQLAlchemyObjectType |
| 7 | +from ..types import SQLAlchemyObjectType, SQLAlchemyObjectTypeMeta |
7 | 8 | from .models import Article, Reporter
|
8 | 9 |
|
9 | 10 | registry = Registry()
|
@@ -74,3 +75,46 @@ def test_object_type():
|
74 | 75 | assert issubclass(Human, ObjectType)
|
75 | 76 | assert list(Human._meta.fields.keys()) == ['id', 'headline', 'reporter_id', 'reporter', 'pub_date']
|
76 | 77 | assert is_node(Human)
|
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +# Test Custom SQLAlchemyObjectType Implementation |
| 82 | +class CustomSQLAlchemyObjectType(six.with_metaclass(SQLAlchemyObjectTypeMeta, ObjectType)): |
| 83 | + @classmethod |
| 84 | + def is_type_of(cls, root, context, info): |
| 85 | + return SQLAlchemyObjectType.is_type_of(root, context, info) |
| 86 | + |
| 87 | + @classmethod |
| 88 | + def get_query(cls, context): |
| 89 | + return SQLAlchemyObjectType.get_query(context) |
| 90 | + |
| 91 | + @classmethod |
| 92 | + def get_node(cls, id, context, info): |
| 93 | + return SQLAlchemyObjectType.get_node(id, context, info) |
| 94 | + |
| 95 | + @classmethod |
| 96 | + def resolve_id(self, args, context, info): |
| 97 | + graphene_type = info.parent_type.graphene_type |
| 98 | + if is_node(graphene_type): |
| 99 | + return self.__mapper__.primary_key_from_instance(self)[0] |
| 100 | + return getattr(self, graphene_type._meta.id, None) |
| 101 | + |
| 102 | + |
| 103 | +class CustomCharacter(CustomSQLAlchemyObjectType): |
| 104 | + '''Character description''' |
| 105 | + class Meta: |
| 106 | + model = Reporter |
| 107 | + registry = registry |
| 108 | + |
| 109 | +def test_custom_objecttype_registered(): |
| 110 | + assert issubclass(CustomCharacter, ObjectType) |
| 111 | + assert CustomCharacter._meta.model == Reporter |
| 112 | + assert list( |
| 113 | + CustomCharacter._meta.fields.keys()) == [ |
| 114 | + 'id', |
| 115 | + 'first_name', |
| 116 | + 'last_name', |
| 117 | + 'email', |
| 118 | + 'pets', |
| 119 | + 'articles', |
| 120 | + 'favorite_article'] |
0 commit comments