Skip to content

Create SQLAlchemyConnectionField through a factory method #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions graphene_sqlalchemy/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from graphene.relay import is_node
from graphene.types.json import JSONString

from .fields import SQLAlchemyConnectionField
from .fields import createConnectionField

try:
from sqlalchemy_utils import ChoiceType, JSONType, ScalarListType
Expand Down Expand Up @@ -36,7 +36,7 @@ def dynamic_type():
elif (direction == interfaces.ONETOMANY or
direction == interfaces.MANYTOMANY):
if is_node(_type):
return SQLAlchemyConnectionField(_type)
return createConnectionField(_type)
return Field(List(_type))

return Dynamic(dynamic_type)
Expand Down
17 changes: 17 additions & 0 deletions graphene_sqlalchemy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,20 @@ def connection_resolver(cls, resolver, connection, model, root, args, context, i

def get_resolver(self, parent_resolver):
return partial(self.connection_resolver, parent_resolver, self.type, self.model)


__connectionFactory = SQLAlchemyConnectionField


def createConnectionField(_type):
return __connectionFactory(_type)


def registerConnectionFieldFactory(factoryMethod):
global __connectionFactory
__connectionFactory = factoryMethod


def unregisterConnectionFieldFactory():
global __connectionFactory
__connectionFactory = SQLAlchemyConnectionField
28 changes: 28 additions & 0 deletions graphene_sqlalchemy/tests/test_connectionfactory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from graphene_sqlalchemy.fields import SQLAlchemyConnectionField, registerConnectionFieldFactory, unregisterConnectionFieldFactory
import graphene

def test_register():
class LXConnectionField(SQLAlchemyConnectionField):
@classmethod
def _applyQueryArgs(cls, model, q, args):
return q

@classmethod
def connection_resolver(cls, resolver, connection, model, root, args, context, info):

def LXResolver(root, args, context, info):
iterable = resolver(root, args, context, info)
if iterable is None:
iterable = cls.get_query(model, context, info, args)

# We accept always a query here. All LX-queries can be filtered and sorted
iterable = cls._applyQueryArgs(model, iterable, args)
return iterable

return SQLAlchemyConnectionField.connection_resolver(LXResolver, connection, model, root, args, context, info)

def createLXConnectionField(table):
return LXConnectionField(table, filter=table.filter(), order_by=graphene.List(of_type=table.order_by))

registerConnectionFieldFactory(createLXConnectionField)
unregisterConnectionFieldFactory()