Skip to content

Commit f916d03

Browse files
committed
Merge branch 'master' into features/2.0
# Conflicts: # graphene_sqlalchemy/types.py
2 parents 82267d3 + d3f8e08 commit f916d03

File tree

4 files changed

+84
-5
lines changed

4 files changed

+84
-5
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,38 @@ query = '''
6868
result = schema.execute(query, context_value={'session': db_session})
6969
```
7070

71+
You may also subclass SQLAlchemyObjectType by providing `abstract = True` in
72+
your subclasses Meta:
73+
```python
74+
from graphene_sqlalchemy import SQLAlchemyObjectType
75+
76+
class ActiveSQLAlchemyObjectType(SQLAlchemyObjectType):
77+
class Meta:
78+
abstract = True
79+
80+
@classmethod
81+
def get_node(cls, id, context, info):
82+
return cls.get_query(context).\
83+
filter(and_(
84+
cls._meta.model.deleted_at==None,
85+
cls._meta.model.id==id,
86+
)).\
87+
first()
88+
89+
class User(ActiveSQLAlchemyObjectType):
90+
class Meta:
91+
model = UserModel
92+
93+
class Query(graphene.ObjectType):
94+
users = graphene.List(User)
95+
96+
def resolve_users(self, args, context, info):
97+
query = User.get_query(context) # SQLAlchemy query
98+
return query.all()
99+
100+
schema = graphene.Schema(query=Query)
101+
```
102+
71103
To learn more check out the following [examples](examples/):
72104

73105
* **Full example**: [Flask SQLAlchemy example](examples/flask_sqlalchemy)

graphene_sqlalchemy/registry.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ def __init__(self):
66
self._registry_composites = {}
77

88
def register(self, cls):
9-
from .types import SQLAlchemyObjectType
10-
assert issubclass(
11-
cls, SQLAlchemyObjectType), 'Only SQLAlchemyObjectType can be registered, received "{}"'.format(
12-
cls.__name__)
9+
from .types import SQLAlchemyObjectTypeMeta
10+
assert issubclass(type(cls), SQLAlchemyObjectTypeMeta), (
11+
'Only classes of type SQLAlchemyObjectTypeMeta can be registered, ',
12+
'received "{}"'
13+
).format(cls.__name__)
1314
assert cls._meta.registry == self, 'Registry for a Model have to match.'
1415
# assert self.get_type_for_model(cls._meta.model) in [None, cls], (
1516
# 'SQLAlchemy model "{}" already associated with '

graphene_sqlalchemy/tests/test_types.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11

22
from graphene import Field, Int, Interface, ObjectType
33
from graphene.relay import Node, is_node
4+
import six
45

56
from ..registry import Registry
6-
from ..types import SQLAlchemyObjectType
7+
from ..types import SQLAlchemyObjectType, SQLAlchemyObjectTypeMeta
78
from .models import Article, Reporter
89

910
registry = Registry()
@@ -87,3 +88,31 @@ class Meta:
8788
assert issubclass(Human, ObjectType)
8889
assert list(Human._meta.fields.keys()) == ['id', 'headline', 'pub_date', 'reporter_id', 'reporter']
8990
assert is_node(Human)
91+
92+
93+
94+
# Test Custom SQLAlchemyObjectType Implementation
95+
class CustomSQLAlchemyObjectType(SQLAlchemyObjectType):
96+
class Meta:
97+
abstract = True
98+
99+
100+
class CustomCharacter(CustomSQLAlchemyObjectType):
101+
'''Character description'''
102+
class Meta:
103+
model = Reporter
104+
registry = registry
105+
106+
107+
def test_custom_objecttype_registered():
108+
assert issubclass(CustomCharacter, ObjectType)
109+
assert CustomCharacter._meta.model == Reporter
110+
assert list(
111+
CustomCharacter._meta.fields.keys()) == [
112+
'id',
113+
'first_name',
114+
'last_name',
115+
'email',
116+
'pets',
117+
'articles',
118+
'favorite_article']

setup.cfg

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,20 @@ omit = */tests/*
77

88
[isort]
99
known_first_party=graphene,graphene_sqlalchemy
10+
11+
[tool:pytest]
12+
testpaths = graphene_sqlalchemy/
13+
addopts =
14+
-s
15+
; --cov graphene-sqlalchemy
16+
norecursedirs =
17+
__pycache__
18+
*.egg-info
19+
.cache
20+
.git
21+
.tox
22+
appdir
23+
docs
24+
filterwarnings =
25+
error
26+
ignore::DeprecationWarning

0 commit comments

Comments
 (0)