Skip to content

Commit 7003e60

Browse files
authored
Merge branch 'master' into batch-sql-4
2 parents 34f617a + 7a48d3d commit 7003e60

File tree

14 files changed

+47
-76
lines changed

14 files changed

+47
-76
lines changed

Diff for: docs/examples.rst

+2-12
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,12 @@ Search all Models with Union
1313
interfaces = (relay.Node,)
1414
1515
16-
class BookConnection(relay.Connection):
17-
class Meta:
18-
node = Book
19-
20-
2116
class Author(SQLAlchemyObjectType):
2217
class Meta:
2318
model = AuthorModel
2419
interfaces = (relay.Node,)
2520
2621
27-
class AuthorConnection(relay.Connection):
28-
class Meta:
29-
node = Author
30-
31-
3222
class SearchResult(graphene.Union):
3323
class Meta:
3424
types = (Book, Author)
@@ -39,8 +29,8 @@ Search all Models with Union
3929
search = graphene.List(SearchResult, q=graphene.String()) # List field for search results
4030
4131
# Normal Fields
42-
all_books = SQLAlchemyConnectionField(BookConnection)
43-
all_authors = SQLAlchemyConnectionField(AuthorConnection)
32+
all_books = SQLAlchemyConnectionField(Book.connection)
33+
all_authors = SQLAlchemyConnectionField(Author.connection)
4434
4535
def resolve_search(self, info, **args):
4636
q = args.get("q") # Search query

Diff for: docs/tips.rst

+1-6
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,8 @@ Given the model
5050
model = Pet
5151
5252
53-
class PetConnection(Connection):
54-
class Meta:
55-
node = PetNode
56-
57-
5853
class Query(ObjectType):
59-
allPets = SQLAlchemyConnectionField(PetConnection)
54+
allPets = SQLAlchemyConnectionField(PetNode.connection)
6055
6156
some of the allowed queries are
6257

Diff for: docs/tutorial.rst

+2-12
Original file line numberDiff line numberDiff line change
@@ -102,28 +102,18 @@ Create ``flask_sqlalchemy/schema.py`` and type the following:
102102
interfaces = (relay.Node, )
103103
104104
105-
class DepartmentConnection(relay.Connection):
106-
class Meta:
107-
node = Department
108-
109-
110105
class Employee(SQLAlchemyObjectType):
111106
class Meta:
112107
model = EmployeeModel
113108
interfaces = (relay.Node, )
114109
115110
116-
class EmployeeConnection(relay.Connection):
117-
class Meta:
118-
node = Employee
119-
120-
121111
class Query(graphene.ObjectType):
122112
node = relay.Node.Field()
123113
# Allows sorting over multiple columns, by default over the primary key
124-
all_employees = SQLAlchemyConnectionField(EmployeeConnection)
114+
all_employees = SQLAlchemyConnectionField(Employee.connection)
125115
# Disable sorting over this field
126-
all_departments = SQLAlchemyConnectionField(DepartmentConnection, sort=None)
116+
all_departments = SQLAlchemyConnectionField(Department.connection, sort=None)
127117
128118
schema = graphene.Schema(query=Query)
129119

Diff for: examples/flask_sqlalchemy/schema.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ class Query(graphene.ObjectType):
2929
node = relay.Node.Field()
3030
# Allow only single column sorting
3131
all_employees = SQLAlchemyConnectionField(
32-
Employee, sort=Employee.sort_argument())
32+
Employee.connection, sort=Employee.sort_argument())
3333
# Allows sorting over multiple columns, by default over the primary key
34-
all_roles = SQLAlchemyConnectionField(Role)
34+
all_roles = SQLAlchemyConnectionField(Role.connection)
3535
# Disable sorting over this field
36-
all_departments = SQLAlchemyConnectionField(Department, sort=None)
36+
all_departments = SQLAlchemyConnectionField(Department.connection, sort=None)
3737

3838

39-
schema = graphene.Schema(query=Query, types=[Department, Employee, Role])
39+
schema = graphene.Schema(query=Query)

Diff for: examples/nameko_sqlalchemy/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ Now the following command will setup the database, and start the server:
4646

4747
```bash
4848
./run.sh
49-
5049
```
5150

5251
Now head on over to postman and send POST request to:

Diff for: examples/nameko_sqlalchemy/database.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def init_db():
1414
# import all modules here that might define models so that
1515
# they will be registered properly on the metadata. Otherwise
1616
# you will have to import them first before calling init_db()
17-
from .models import Department, Employee, Role
17+
from models import Department, Employee, Role
1818
Base.metadata.drop_all(bind=engine)
1919
Base.metadata.create_all(bind=engine)
2020

Diff for: examples/nameko_sqlalchemy/schema.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,28 @@
88

99

1010
class Department(SQLAlchemyObjectType):
11-
1211
class Meta:
1312
model = DepartmentModel
14-
interfaces = (relay.Node, )
13+
interfaces = (relay.Node,)
1514

1615

1716
class Employee(SQLAlchemyObjectType):
18-
1917
class Meta:
2018
model = EmployeeModel
21-
interfaces = (relay.Node, )
19+
interfaces = (relay.Node,)
2220

2321

2422
class Role(SQLAlchemyObjectType):
25-
2623
class Meta:
2724
model = RoleModel
28-
interfaces = (relay.Node, )
25+
interfaces = (relay.Node,)
2926

3027

3128
class Query(graphene.ObjectType):
3229
node = relay.Node.Field()
33-
all_employees = SQLAlchemyConnectionField(Employee)
34-
all_roles = SQLAlchemyConnectionField(Role)
30+
all_employees = SQLAlchemyConnectionField(Employee.connection)
31+
all_roles = SQLAlchemyConnectionField(Role.connection)
3532
role = graphene.Field(Role)
3633

3734

38-
schema = graphene.Schema(query=Query, types=[Department, Employee, Role])
35+
schema = graphene.Schema(query=Query)

Diff for: graphene_sqlalchemy/fields.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ def type(self):
2424
assert issubclass(_type, SQLAlchemyObjectType), (
2525
"SQLALchemyConnectionField only accepts SQLAlchemyObjectType types, not {}"
2626
).format(_type.__name__)
27-
assert _type._meta.connection, "The type {} doesn't have a connection".format(
27+
assert _type.connection, "The type {} doesn't have a connection".format(
2828
_type.__name__
2929
)
30-
return _type._meta.connection
30+
return _type.connection
3131

3232
@property
3333
def model(self):
@@ -115,7 +115,7 @@ def get_resolver(self, parent_resolver):
115115
def from_relationship(cls, relationship, registry, **field_kwargs):
116116
model = relationship.mapper.entity
117117
model_type = registry.get_type_for_model(model)
118-
return cls(model_type._meta.connection, resolver=get_batch_resolver(relationship), **field_kwargs)
118+
return cls(model_type.connection, resolver=get_batch_resolver(relationship), **field_kwargs)
119119

120120

121121
def default_connection_field_factory(relationship, registry, **field_kwargs):

Diff for: graphene_sqlalchemy/tests/test_fields.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def resolver(_obj, _info):
3131
return Promise.resolve([])
3232

3333
result = UnsortedSQLAlchemyConnectionField.connection_resolver(
34-
resolver, Pet._meta.connection, Pet, None, None
34+
resolver, Pet.connection, Pet, None, None
3535
)
3636
assert isinstance(result, Promise)
3737

@@ -51,18 +51,18 @@ def test_type_assert_object_has_connection():
5151

5252

5353
def test_sort_added_by_default():
54-
field = SQLAlchemyConnectionField(Pet._meta.connection)
54+
field = SQLAlchemyConnectionField(Pet.connection)
5555
assert "sort" in field.args
5656
assert field.args["sort"] == Pet.sort_argument()
5757

5858

5959
def test_sort_can_be_removed():
60-
field = SQLAlchemyConnectionField(Pet._meta.connection, sort=None)
60+
field = SQLAlchemyConnectionField(Pet.connection, sort=None)
6161
assert "sort" not in field.args
6262

6363

6464
def test_custom_sort():
65-
field = SQLAlchemyConnectionField(Pet._meta.connection, sort=Editor.sort_argument())
65+
field = SQLAlchemyConnectionField(Pet.connection, sort=Editor.sort_argument())
6666
assert field.args["sort"] == Editor.sort_argument()
6767

6868

Diff for: graphene_sqlalchemy/tests/test_query.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import graphene
2-
from graphene.relay import Connection, Node
2+
from graphene.relay import Node
33

44
from ..converter import convert_sqlalchemy_composite
55
from ..fields import SQLAlchemyConnectionField
@@ -96,14 +96,10 @@ class Meta:
9696
model = Article
9797
interfaces = (Node,)
9898

99-
class ArticleConnection(Connection):
100-
class Meta:
101-
node = ArticleNode
102-
10399
class Query(graphene.ObjectType):
104100
node = Node.Field()
105101
reporter = graphene.Field(ReporterNode)
106-
all_articles = SQLAlchemyConnectionField(ArticleConnection)
102+
all_articles = SQLAlchemyConnectionField(ArticleNode.connection)
107103

108104
def resolve_reporter(self, _info):
109105
return session.query(Reporter).first()
@@ -230,13 +226,9 @@ class Meta:
230226
model = Editor
231227
interfaces = (Node,)
232228

233-
class EditorConnection(Connection):
234-
class Meta:
235-
node = EditorNode
236-
237229
class Query(graphene.ObjectType):
238230
node = Node.Field()
239-
all_editors = SQLAlchemyConnectionField(EditorConnection)
231+
all_editors = SQLAlchemyConnectionField(EditorNode.connection)
240232

241233
query = """
242234
query {

Diff for: graphene_sqlalchemy/tests/test_sort_enums.py

+8-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sqlalchemy as sa
33

44
from graphene import Argument, Enum, List, ObjectType, Schema
5-
from graphene.relay import Connection, Node
5+
from graphene.relay import Node
66

77
from ..fields import SQLAlchemyConnectionField
88
from ..types import SQLAlchemyObjectType
@@ -249,22 +249,18 @@ class Meta:
249249
model = Pet
250250
interfaces = (Node,)
251251

252-
class PetConnection(Connection):
253-
class Meta:
254-
node = PetNode
255-
256252
class Query(ObjectType):
257-
defaultSort = SQLAlchemyConnectionField(PetConnection)
258-
nameSort = SQLAlchemyConnectionField(PetConnection)
259-
multipleSort = SQLAlchemyConnectionField(PetConnection)
260-
descSort = SQLAlchemyConnectionField(PetConnection)
253+
defaultSort = SQLAlchemyConnectionField(PetNode.connection)
254+
nameSort = SQLAlchemyConnectionField(PetNode.connection)
255+
multipleSort = SQLAlchemyConnectionField(PetNode.connection)
256+
descSort = SQLAlchemyConnectionField(PetNode.connection)
261257
singleColumnSort = SQLAlchemyConnectionField(
262-
PetConnection, sort=Argument(PetNode.sort_enum())
258+
PetNode.connection, sort=Argument(PetNode.sort_enum())
263259
)
264260
noDefaultSort = SQLAlchemyConnectionField(
265-
PetConnection, sort=PetNode.sort_argument(has_default=False)
261+
PetNode.connection, sort=PetNode.sort_argument(has_default=False)
266262
)
267-
noSort = SQLAlchemyConnectionField(PetConnection, sort=None)
263+
noSort = SQLAlchemyConnectionField(PetNode.connection, sort=None)
268264

269265
query = """
270266
query sortTest {

Diff for: graphene_sqlalchemy/tests/test_types.py

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from graphene import (Dynamic, Field, GlobalID, Int, List, Node, NonNull,
66
ObjectType, Schema, String)
7+
from graphene.relay import Connection
78

89
from ..converter import convert_sqlalchemy_composite
910
from ..fields import (SQLAlchemyConnectionField,
@@ -46,6 +47,15 @@ class Meta:
4647
assert reporter == reporter_node
4748

4849

50+
def test_connection():
51+
class ReporterType(SQLAlchemyObjectType):
52+
class Meta:
53+
model = Reporter
54+
interfaces = (Node,)
55+
56+
assert issubclass(ReporterType.connection, Connection)
57+
58+
4959
def test_sqlalchemy_default_fields():
5060
@convert_sqlalchemy_composite.register(CompositeFullName)
5161
def convert_composite_class(composite, registry):

Diff for: graphene_sqlalchemy/types.py

+2
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ def __init_subclass_with_meta__(
269269
_meta.connection = connection
270270
_meta.id = id or "id"
271271

272+
cls.connection = connection # Public way to get the connection
273+
272274
super(SQLAlchemyObjectType, cls).__init_subclass_with_meta__(
273275
_meta=_meta, interfaces=interfaces, **options
274276
)

Diff for: setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
requirements = [
1515
# To keep things simple, we only support newer versions of Graphene
1616
"graphene>=2.1.3,<3",
17-
"promise>=2.1",
17+
"promise>=2.3",
1818
# Tests fail with 1.0.19
1919
"SQLAlchemy>=1.2,<2",
2020
"six>=1.10.0,<2",

0 commit comments

Comments
 (0)