Skip to content

Commit dd6a7c7

Browse files
committed
Merge branch 'master' of github.com:graphql-python/graphene-sqlalchemy
2 parents d1f1d21 + 4955043 commit dd6a7c7

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Query(graphene.ObjectType):
5151
query = User.get_query(context) # SQLAlchemy query
5252
return query.all()
5353

54-
schema = graphene.Schema(query=QueryRoot)
54+
schema = graphene.Schema(query=Query)
5555
```
5656

5757
Then you can simply query the schema:

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ following:
5858
query = User.get_query(context) # SQLAlchemy query
5959
return query.all()
6060
61-
schema = graphene.Schema(query=QueryRoot)
61+
schema = graphene.Schema(query=Query)
6262
6363
Then you can simply query the schema:
6464

graphene_sqlalchemy/converter.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@
1111
from .fields import SQLAlchemyConnectionField
1212

1313
try:
14-
from sqlalchemy_utils import ChoiceType, ScalarListType
14+
from sqlalchemy_utils import ChoiceType, JSONType, ScalarListType
1515
except ImportError:
1616
class ChoiceType(object):
1717
pass
1818

1919
class ScalarListType(object):
2020
pass
2121

22+
class JSONType(object):
23+
pass
24+
2225

2326
def convert_sqlalchemy_relationship(relationship, registry):
2427
direction = relationship.direction
@@ -87,7 +90,8 @@ def convert_sqlalchemy_type(type, column, registry=None):
8790
@convert_sqlalchemy_type.register(postgresql.ENUM)
8891
@convert_sqlalchemy_type.register(postgresql.UUID)
8992
def convert_column_to_string(type, column, registry=None):
90-
return String(description=column.doc, required=not(column.nullable))
93+
return String(description=getattr(column, 'doc', None),
94+
required=not(getattr(column, 'nullable', True)))
9195

9296

9397
@convert_sqlalchemy_type.register(types.SmallInteger)
@@ -133,3 +137,8 @@ def convert_postgres_array_to_list(type, column, registry=None):
133137
@convert_sqlalchemy_type.register(postgresql.JSONB)
134138
def convert_json_to_string(type, column, registry=None):
135139
return JSONString(description=column.doc, required=not(column.nullable))
140+
141+
142+
@convert_sqlalchemy_type.register(JSONType)
143+
def convert_json_type_to_string(type, column, registry=None):
144+
return JSONString(description=column.doc, required=not(column.nullable))

graphene_sqlalchemy/fields.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ class SQLAlchemyConnectionField(ConnectionField):
1515
def model(self):
1616
return self.type._meta.node._meta.model
1717

18-
@staticmethod
19-
def connection_resolver(resolver, connection, model, root, args, context, info):
18+
@classmethod
19+
def get_query(cls, model, context, info, args):
20+
return get_query(model, context)
21+
22+
@classmethod
23+
def connection_resolver(cls, resolver, connection, model, root, args, context, info):
2024
iterable = resolver(root, args, context, info)
2125
if iterable is None:
22-
iterable = get_query(model, context)
26+
iterable = cls.get_query(model, context, info, args)
2327
if isinstance(iterable, Query):
2428
_len = iterable.count()
2529
else:

graphene_sqlalchemy/tests/test_converter.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from py.test import raises
2-
from sqlalchemy import Column, Table, types
2+
from sqlalchemy import Column, Table, case, types
33
from sqlalchemy.dialects import postgresql
44
from sqlalchemy.ext.declarative import declarative_base
55
from sqlalchemy.orm import composite
6-
from sqlalchemy_utils import ChoiceType, ScalarListType
6+
from sqlalchemy.sql.elements import Label
7+
from sqlalchemy_utils import ChoiceType, JSONType, ScalarListType
78

89
import graphene
910
from graphene.relay import Node
@@ -106,6 +107,12 @@ def test_should_numeric_convert_float():
106107
assert_column_conversion(types.Numeric(), graphene.Float)
107108

108109

110+
def test_should_label_convert_string():
111+
label = Label('label_test', case([], else_="foo"), type_=types.Unicode())
112+
graphene_type = convert_sqlalchemy_column(label)
113+
assert isinstance(graphene_type, graphene.String)
114+
115+
109116
def test_should_choice_convert_enum():
110117
TYPES = [
111118
(u'es', u'Spanish'),
@@ -127,6 +134,10 @@ def test_should_scalar_list_convert_list():
127134
assert_column_conversion(ScalarListType(), graphene.List)
128135

129136

137+
def test_should_jsontype_convert_jsonstring():
138+
assert_column_conversion(JSONType(), JSONString)
139+
140+
130141
def test_should_manytomany_convert_connectionorlist():
131142
registry = Registry()
132143
dynamic_field = convert_sqlalchemy_relationship(Reporter.pets.property, registry)

0 commit comments

Comments
 (0)