From ebcc1f046d526e0e1dbd10bc53a2c5861cb929a3 Mon Sep 17 00:00:00 2001 From: Markus Padourek Date: Fri, 16 Sep 2016 16:48:27 +0100 Subject: [PATCH] Fixed global registry related errors. --- .../graphene_sqlalchemy/converter.py | 2 + .../graphene_sqlalchemy/tests/test_query.py | 56 ++++++++++++++++++- .../graphene_sqlalchemy/types.py | 1 + graphene/relay/connection.py | 12 +++- setup.py | 2 +- 5 files changed, 67 insertions(+), 6 deletions(-) diff --git a/graphene-sqlalchemy/graphene_sqlalchemy/converter.py b/graphene-sqlalchemy/graphene_sqlalchemy/converter.py index 6839a26f..15ef1570 100644 --- a/graphene-sqlalchemy/graphene_sqlalchemy/converter.py +++ b/graphene-sqlalchemy/graphene_sqlalchemy/converter.py @@ -21,6 +21,7 @@ except ImportError: def convert_sqlalchemy_relationship(relationship, registry, connections, type_name): direction = relationship.direction model = relationship.mapper.entity + print(registry) def dynamic_type(): _type = registry.get_type_for_model(model) @@ -34,6 +35,7 @@ def convert_sqlalchemy_relationship(relationship, registry, connections, type_na try: connection_type = connections[relationship.key] except KeyError: + print(_type) raise KeyError("No Connection provided for relationship {} on type {}. Specify it in its Meta " "class on the 'connections' dict.".format(relationship.key, type_name)) return SQLAlchemyConnectionField(connection_type) diff --git a/graphene-sqlalchemy/graphene_sqlalchemy/tests/test_query.py b/graphene-sqlalchemy/graphene_sqlalchemy/tests/test_query.py index a6caf135..32e84adb 100644 --- a/graphene-sqlalchemy/graphene_sqlalchemy/tests/test_query.py +++ b/graphene-sqlalchemy/graphene_sqlalchemy/tests/test_query.py @@ -7,7 +7,7 @@ from graphene.relay import Node, Connection from ..types import SQLAlchemyObjectType from ..fields import SQLAlchemyConnectionField -from .models import Article, Base, Editor, Reporter +from .models import Article, Base, Editor, Reporter, Pet db = create_engine('sqlite:///test_sqlalchemy.sqlite3') @@ -46,10 +46,34 @@ def setup_fixtures(session): def test_should_query_well(session): setup_fixtures(session) + class ArticleType(SQLAlchemyObjectType): + + class Meta: + model = Article + + ArticleTypeConnection = Connection.for_type(ArticleType) + + ReporterNodeConnection = graphene.Dynamic(lambda: Connection.for_type(ReporterType)) + + class A(SQLAlchemyObjectType): + class Meta: + model = Pet + connections = { + 'reporters': ReporterNodeConnection, + 'articles': ArticleTypeConnection, + } + interfaces = (Node, ) + + AConnection = Connection.for_type(A) + class ReporterType(SQLAlchemyObjectType): class Meta: model = Reporter + connections = { + 'pets': AConnection, + 'articles': AConnection, + } class Query(graphene.ObjectType): reporter = graphene.Field(ReporterType) @@ -106,12 +130,25 @@ def test_should_node(session): ArticleNodeConnection = Connection.for_type(ArticleNode) + ReporterNodeConnection = graphene.Dynamic(lambda: Connection.for_type(ReporterNode)) + + class A(SQLAlchemyObjectType): + class Meta: + model = Pet + connections = { + 'reporters': ReporterNodeConnection + } + interfaces = (Node, ) + + AConnection = Connection.for_type(A) + class ReporterNode(SQLAlchemyObjectType): class Meta: model = Reporter connections = { - 'articles': ArticleNodeConnection + 'articles': ArticleNodeConnection, + 'pets': AConnection, } interfaces = (Node, ) @@ -264,12 +301,25 @@ def test_should_mutate_well(session): ArticleNodeConnection = Connection.for_type(ArticleNode) + ReporterNodeConnection = graphene.Dynamic(lambda: Connection.for_type(ReporterNode)) + + class A(SQLAlchemyObjectType): + class Meta: + model = Pet + connections = { + 'reporters': ReporterNodeConnection + } + interfaces = (Node, ) + + AConnection = Connection.for_type(A) + class ReporterNode(SQLAlchemyObjectType): class Meta: model = Reporter connections = { - 'articles': ArticleNodeConnection + 'articles': ArticleNodeConnection, + 'pets': AConnection, } interfaces = (Node, ) diff --git a/graphene-sqlalchemy/graphene_sqlalchemy/types.py b/graphene-sqlalchemy/graphene_sqlalchemy/types.py index 18a3e52c..e9057d82 100644 --- a/graphene-sqlalchemy/graphene_sqlalchemy/types.py +++ b/graphene-sqlalchemy/graphene_sqlalchemy/types.py @@ -24,6 +24,7 @@ def construct_fields(options, type_name): inspected_model = sqlalchemyinspect(options.model) fields = OrderedDict() + print('options in construct_fields', options) for name, column in inspected_model.columns.items(): is_not_in_only = only_fields and name not in only_fields diff --git a/graphene/relay/connection.py b/graphene/relay/connection.py index 235a4f68..fc5c5133 100644 --- a/graphene/relay/connection.py +++ b/graphene/relay/connection.py @@ -7,7 +7,7 @@ import six from graphql_relay import connection_from_list -from ..types import Boolean, Int, List, String, AbstractType +from ..types import Boolean, Int, List, String, AbstractType, Dynamic from ..types.field import Field from ..types.objecttype import ObjectType, ObjectTypeMeta from ..types.options import Options @@ -123,7 +123,7 @@ def is_connection(gql_type): class IterableConnectionField(Field): def __init__(self, gql_type, *args, **kwargs): - assert is_connection(gql_type), ( + assert is_connection(gql_type) or isinstance(gql_type, Dynamic), ( 'The provided type "{}" for this ConnectionField has to be a Connection as defined by the Relay' ' spec.'.format(gql_type) ) @@ -137,6 +137,14 @@ class IterableConnectionField(Field): **kwargs ) + @property + def type(self): + gql_type = super(IterableConnectionField, self).type + if isinstance(gql_type, Dynamic): + return gql_type.get_type() + else: + return gql_type + @staticmethod def connection_resolver(resolver, connection, root, args, context, info): resolved = Promise.resolve(resolver(root, args, context, info)) diff --git a/setup.py b/setup.py index dfd82742..b11a90bc 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ else: # machinery. builtins.__SETUP__ = True -version = __import__('graphene').get_version() +version = "1.0.beta-1" class PyTest(TestCommand):