Merge pull request #313 from BossGrand/feature/allow_abstract_connections

Allow DjangoObjectType to use an Abstract Connection Class
This commit is contained in:
Syrus Akbary 2017-11-13 02:23:46 -08:00 committed by GitHub
commit 94e2a5c49f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 3 deletions

View File

@ -1,6 +1,6 @@
from mock import patch from mock import patch
from graphene import Interface, ObjectType, Schema from graphene import Interface, ObjectType, Schema, Connection, String
from graphene.relay import Node from graphene.relay import Node
from .. import registry from .. import registry
@ -17,11 +17,23 @@ class Reporter(DjangoObjectType):
model = ReporterModel model = ReporterModel
class ArticleConnection(Connection):
'''Article Connection'''
test = String()
def resolve_test():
return 'test'
class Meta:
abstract = True
class Article(DjangoObjectType): class Article(DjangoObjectType):
'''Article description''' '''Article description'''
class Meta: class Meta:
model = ArticleModel model = ArticleModel
interfaces = (Node, ) interfaces = (Node, )
connection_class = ArticleConnection
class RootQuery(ObjectType): class RootQuery(ObjectType):
@ -74,6 +86,7 @@ type Article implements Node {
type ArticleConnection { type ArticleConnection {
pageInfo: PageInfo! pageInfo: PageInfo!
edges: [ArticleEdge]! edges: [ArticleEdge]!
test: String
} }
type ArticleEdge { type ArticleEdge {

View File

@ -45,7 +45,7 @@ class DjangoObjectType(ObjectType):
@classmethod @classmethod
def __init_subclass_with_meta__(cls, model=None, registry=None, skip_registry=False, def __init_subclass_with_meta__(cls, model=None, registry=None, skip_registry=False,
only_fields=(), exclude_fields=(), filter_fields=None, connection=None, only_fields=(), exclude_fields=(), filter_fields=None, connection=None,
use_connection=None, interfaces=(), **options): connection_class=None, use_connection=None, interfaces=(), **options):
assert is_valid_django_model(model), ( assert is_valid_django_model(model), (
'You need to pass a valid Django Model in {}.Meta, received "{}".' 'You need to pass a valid Django Model in {}.Meta, received "{}".'
).format(cls.__name__, model) ).format(cls.__name__, model)
@ -71,7 +71,11 @@ class DjangoObjectType(ObjectType):
if use_connection and not connection: if use_connection and not connection:
# We create the connection automatically # We create the connection automatically
connection = Connection.create_type('{}Connection'.format(cls.__name__), node=cls) if not connection_class:
connection_class = Connection
connection = connection_class.create_type(
'{}Connection'.format(cls.__name__), node=cls)
if connection is not None: if connection is not None:
assert issubclass(connection, Connection), ( assert issubclass(connection, Connection), (