Skip to content

Commit

Permalink
Merge branch 'master' into batch-sql-4
Browse files Browse the repository at this point in the history
  • Loading branch information
jnak authored Feb 12, 2020
2 parents 34f617a + 7a48d3d commit 7003e60
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 76 deletions.
14 changes: 2 additions & 12 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,12 @@ Search all Models with Union
interfaces = (relay.Node,)
class BookConnection(relay.Connection):
class Meta:
node = Book
class Author(SQLAlchemyObjectType):
class Meta:
model = AuthorModel
interfaces = (relay.Node,)
class AuthorConnection(relay.Connection):
class Meta:
node = Author
class SearchResult(graphene.Union):
class Meta:
types = (Book, Author)
Expand All @@ -39,8 +29,8 @@ Search all Models with Union
search = graphene.List(SearchResult, q=graphene.String()) # List field for search results
# Normal Fields
all_books = SQLAlchemyConnectionField(BookConnection)
all_authors = SQLAlchemyConnectionField(AuthorConnection)
all_books = SQLAlchemyConnectionField(Book.connection)
all_authors = SQLAlchemyConnectionField(Author.connection)
def resolve_search(self, info, **args):
q = args.get("q") # Search query
Expand Down
7 changes: 1 addition & 6 deletions docs/tips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,8 @@ Given the model
model = Pet
class PetConnection(Connection):
class Meta:
node = PetNode
class Query(ObjectType):
allPets = SQLAlchemyConnectionField(PetConnection)
allPets = SQLAlchemyConnectionField(PetNode.connection)
some of the allowed queries are

Expand Down
14 changes: 2 additions & 12 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,18 @@ Create ``flask_sqlalchemy/schema.py`` and type the following:
interfaces = (relay.Node, )
class DepartmentConnection(relay.Connection):
class Meta:
node = Department
class Employee(SQLAlchemyObjectType):
class Meta:
model = EmployeeModel
interfaces = (relay.Node, )
class EmployeeConnection(relay.Connection):
class Meta:
node = Employee
class Query(graphene.ObjectType):
node = relay.Node.Field()
# Allows sorting over multiple columns, by default over the primary key
all_employees = SQLAlchemyConnectionField(EmployeeConnection)
all_employees = SQLAlchemyConnectionField(Employee.connection)
# Disable sorting over this field
all_departments = SQLAlchemyConnectionField(DepartmentConnection, sort=None)
all_departments = SQLAlchemyConnectionField(Department.connection, sort=None)
schema = graphene.Schema(query=Query)
Expand Down
8 changes: 4 additions & 4 deletions examples/flask_sqlalchemy/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class Query(graphene.ObjectType):
node = relay.Node.Field()
# Allow only single column sorting
all_employees = SQLAlchemyConnectionField(
Employee, sort=Employee.sort_argument())
Employee.connection, sort=Employee.sort_argument())
# Allows sorting over multiple columns, by default over the primary key
all_roles = SQLAlchemyConnectionField(Role)
all_roles = SQLAlchemyConnectionField(Role.connection)
# Disable sorting over this field
all_departments = SQLAlchemyConnectionField(Department, sort=None)
all_departments = SQLAlchemyConnectionField(Department.connection, sort=None)


schema = graphene.Schema(query=Query, types=[Department, Employee, Role])
schema = graphene.Schema(query=Query)
1 change: 0 additions & 1 deletion examples/nameko_sqlalchemy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ Now the following command will setup the database, and start the server:

```bash
./run.sh

```

Now head on over to postman and send POST request to:
Expand Down
2 changes: 1 addition & 1 deletion examples/nameko_sqlalchemy/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def init_db():
# import all modules here that might define models so that
# they will be registered properly on the metadata. Otherwise
# you will have to import them first before calling init_db()
from .models import Department, Employee, Role
from models import Department, Employee, Role
Base.metadata.drop_all(bind=engine)
Base.metadata.create_all(bind=engine)

Expand Down
15 changes: 6 additions & 9 deletions examples/nameko_sqlalchemy/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,28 @@


class Department(SQLAlchemyObjectType):

class Meta:
model = DepartmentModel
interfaces = (relay.Node, )
interfaces = (relay.Node,)


class Employee(SQLAlchemyObjectType):

class Meta:
model = EmployeeModel
interfaces = (relay.Node, )
interfaces = (relay.Node,)


class Role(SQLAlchemyObjectType):

class Meta:
model = RoleModel
interfaces = (relay.Node, )
interfaces = (relay.Node,)


class Query(graphene.ObjectType):
node = relay.Node.Field()
all_employees = SQLAlchemyConnectionField(Employee)
all_roles = SQLAlchemyConnectionField(Role)
all_employees = SQLAlchemyConnectionField(Employee.connection)
all_roles = SQLAlchemyConnectionField(Role.connection)
role = graphene.Field(Role)


schema = graphene.Schema(query=Query, types=[Department, Employee, Role])
schema = graphene.Schema(query=Query)
6 changes: 3 additions & 3 deletions graphene_sqlalchemy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def type(self):
assert issubclass(_type, SQLAlchemyObjectType), (
"SQLALchemyConnectionField only accepts SQLAlchemyObjectType types, not {}"
).format(_type.__name__)
assert _type._meta.connection, "The type {} doesn't have a connection".format(
assert _type.connection, "The type {} doesn't have a connection".format(
_type.__name__
)
return _type._meta.connection
return _type.connection

@property
def model(self):
Expand Down Expand Up @@ -115,7 +115,7 @@ def get_resolver(self, parent_resolver):
def from_relationship(cls, relationship, registry, **field_kwargs):
model = relationship.mapper.entity
model_type = registry.get_type_for_model(model)
return cls(model_type._meta.connection, resolver=get_batch_resolver(relationship), **field_kwargs)
return cls(model_type.connection, resolver=get_batch_resolver(relationship), **field_kwargs)


def default_connection_field_factory(relationship, registry, **field_kwargs):
Expand Down
8 changes: 4 additions & 4 deletions graphene_sqlalchemy/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def resolver(_obj, _info):
return Promise.resolve([])

result = UnsortedSQLAlchemyConnectionField.connection_resolver(
resolver, Pet._meta.connection, Pet, None, None
resolver, Pet.connection, Pet, None, None
)
assert isinstance(result, Promise)

Expand All @@ -51,18 +51,18 @@ def test_type_assert_object_has_connection():


def test_sort_added_by_default():
field = SQLAlchemyConnectionField(Pet._meta.connection)
field = SQLAlchemyConnectionField(Pet.connection)
assert "sort" in field.args
assert field.args["sort"] == Pet.sort_argument()


def test_sort_can_be_removed():
field = SQLAlchemyConnectionField(Pet._meta.connection, sort=None)
field = SQLAlchemyConnectionField(Pet.connection, sort=None)
assert "sort" not in field.args


def test_custom_sort():
field = SQLAlchemyConnectionField(Pet._meta.connection, sort=Editor.sort_argument())
field = SQLAlchemyConnectionField(Pet.connection, sort=Editor.sort_argument())
assert field.args["sort"] == Editor.sort_argument()


Expand Down
14 changes: 3 additions & 11 deletions graphene_sqlalchemy/tests/test_query.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import graphene
from graphene.relay import Connection, Node
from graphene.relay import Node

from ..converter import convert_sqlalchemy_composite
from ..fields import SQLAlchemyConnectionField
Expand Down Expand Up @@ -96,14 +96,10 @@ class Meta:
model = Article
interfaces = (Node,)

class ArticleConnection(Connection):
class Meta:
node = ArticleNode

class Query(graphene.ObjectType):
node = Node.Field()
reporter = graphene.Field(ReporterNode)
all_articles = SQLAlchemyConnectionField(ArticleConnection)
all_articles = SQLAlchemyConnectionField(ArticleNode.connection)

def resolve_reporter(self, _info):
return session.query(Reporter).first()
Expand Down Expand Up @@ -230,13 +226,9 @@ class Meta:
model = Editor
interfaces = (Node,)

class EditorConnection(Connection):
class Meta:
node = EditorNode

class Query(graphene.ObjectType):
node = Node.Field()
all_editors = SQLAlchemyConnectionField(EditorConnection)
all_editors = SQLAlchemyConnectionField(EditorNode.connection)

query = """
query {
Expand Down
20 changes: 8 additions & 12 deletions graphene_sqlalchemy/tests/test_sort_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sqlalchemy as sa

from graphene import Argument, Enum, List, ObjectType, Schema
from graphene.relay import Connection, Node
from graphene.relay import Node

from ..fields import SQLAlchemyConnectionField
from ..types import SQLAlchemyObjectType
Expand Down Expand Up @@ -249,22 +249,18 @@ class Meta:
model = Pet
interfaces = (Node,)

class PetConnection(Connection):
class Meta:
node = PetNode

class Query(ObjectType):
defaultSort = SQLAlchemyConnectionField(PetConnection)
nameSort = SQLAlchemyConnectionField(PetConnection)
multipleSort = SQLAlchemyConnectionField(PetConnection)
descSort = SQLAlchemyConnectionField(PetConnection)
defaultSort = SQLAlchemyConnectionField(PetNode.connection)
nameSort = SQLAlchemyConnectionField(PetNode.connection)
multipleSort = SQLAlchemyConnectionField(PetNode.connection)
descSort = SQLAlchemyConnectionField(PetNode.connection)
singleColumnSort = SQLAlchemyConnectionField(
PetConnection, sort=Argument(PetNode.sort_enum())
PetNode.connection, sort=Argument(PetNode.sort_enum())
)
noDefaultSort = SQLAlchemyConnectionField(
PetConnection, sort=PetNode.sort_argument(has_default=False)
PetNode.connection, sort=PetNode.sort_argument(has_default=False)
)
noSort = SQLAlchemyConnectionField(PetConnection, sort=None)
noSort = SQLAlchemyConnectionField(PetNode.connection, sort=None)

query = """
query sortTest {
Expand Down
10 changes: 10 additions & 0 deletions graphene_sqlalchemy/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from graphene import (Dynamic, Field, GlobalID, Int, List, Node, NonNull,
ObjectType, Schema, String)
from graphene.relay import Connection

from ..converter import convert_sqlalchemy_composite
from ..fields import (SQLAlchemyConnectionField,
Expand Down Expand Up @@ -46,6 +47,15 @@ class Meta:
assert reporter == reporter_node


def test_connection():
class ReporterType(SQLAlchemyObjectType):
class Meta:
model = Reporter
interfaces = (Node,)

assert issubclass(ReporterType.connection, Connection)


def test_sqlalchemy_default_fields():
@convert_sqlalchemy_composite.register(CompositeFullName)
def convert_composite_class(composite, registry):
Expand Down
2 changes: 2 additions & 0 deletions graphene_sqlalchemy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ def __init_subclass_with_meta__(
_meta.connection = connection
_meta.id = id or "id"

cls.connection = connection # Public way to get the connection

super(SQLAlchemyObjectType, cls).__init_subclass_with_meta__(
_meta=_meta, interfaces=interfaces, **options
)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
requirements = [
# To keep things simple, we only support newer versions of Graphene
"graphene>=2.1.3,<3",
"promise>=2.1",
"promise>=2.3",
# Tests fail with 1.0.19
"SQLAlchemy>=1.2,<2",
"six>=1.10.0,<2",
Expand Down

0 comments on commit 7003e60

Please sign in to comment.