From 8168aaa035f128bac99d73bf2f0bdbacea9a4c6a Mon Sep 17 00:00:00 2001 From: Sean Dougherty Date: Mon, 1 Apr 2019 22:05:57 -0400 Subject: [PATCH] Added description to relationships --- graphene_sqlalchemy/converter.py | 7 ++++--- graphene_sqlalchemy/fields.py | 4 ++-- graphene_sqlalchemy/tests/models.py | 8 ++++---- graphene_sqlalchemy/tests/test_converter.py | 4 ++++ 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/graphene_sqlalchemy/converter.py b/graphene_sqlalchemy/converter.py index 72089613..1317745b 100644 --- a/graphene_sqlalchemy/converter.py +++ b/graphene_sqlalchemy/converter.py @@ -25,17 +25,18 @@ def is_column_nullable(column): def convert_sqlalchemy_relationship(relationship, registry): direction = relationship.direction model = relationship.mapper.entity + description = getattr(relationship, "doc", None) def dynamic_type(): _type = registry.get_type_for_model(model) if not _type: return None if direction == interfaces.MANYTOONE or not relationship.uselist: - return Field(_type) + return Field(_type, description=description) elif direction in (interfaces.ONETOMANY, interfaces.MANYTOMANY): if _type._meta.connection: - return createConnectionField(_type._meta.connection) - return Field(List(_type)) + return createConnectionField(_type._meta.connection, description=description) + return Field(List(_type), description=description) return Dynamic(dynamic_type) diff --git a/graphene_sqlalchemy/fields.py b/graphene_sqlalchemy/fields.py index bf3522b4..0a031802 100644 --- a/graphene_sqlalchemy/fields.py +++ b/graphene_sqlalchemy/fields.py @@ -97,8 +97,8 @@ def __init__(self, type, *args, **kwargs): __connectionFactory = UnsortedSQLAlchemyConnectionField -def createConnectionField(_type): - return __connectionFactory(_type) +def createConnectionField(_type, **kwargs): + return __connectionFactory(_type, **kwargs) def registerConnectionFieldFactory(factoryMethod): diff --git a/graphene_sqlalchemy/tests/models.py b/graphene_sqlalchemy/tests/models.py index 3ba23a8a..c5416266 100644 --- a/graphene_sqlalchemy/tests/models.py +++ b/graphene_sqlalchemy/tests/models.py @@ -4,7 +4,7 @@ from sqlalchemy import Column, Date, Enum, ForeignKey, Integer, String, Table from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy.orm import mapper, relationship +from sqlalchemy.orm import backref, mapper, relationship class Hairkind(enum.Enum): @@ -43,9 +43,9 @@ class Reporter(Base): first_name = Column(String(30)) last_name = Column(String(30)) email = Column(String()) - pets = relationship("Pet", secondary=association_table, backref="reporters") - articles = relationship("Article", backref="reporter") - favorite_article = relationship("Article", uselist=False) + pets = relationship("Pet", secondary=association_table, backref="reporters", doc='Pets') + articles = relationship("Article", backref=backref("reporter", doc='Reporter'), doc='Articles') + favorite_article = relationship("Article", uselist=False, doc='Favorite Article') # total = column_property( # select([ diff --git a/graphene_sqlalchemy/tests/test_converter.py b/graphene_sqlalchemy/tests/test_converter.py index c2ec3e49..0ab3362e 100644 --- a/graphene_sqlalchemy/tests/test_converter.py +++ b/graphene_sqlalchemy/tests/test_converter.py @@ -199,6 +199,7 @@ class Meta: assert isinstance(graphene_type, graphene.Field) assert isinstance(graphene_type.type, graphene.List) assert graphene_type.type.of_type == A + assert graphene_type.description == "Pets" def test_should_manytomany_convert_connectionorlist_connection(): @@ -233,6 +234,7 @@ class Meta: graphene_type = dynamic_field.get_type() assert isinstance(graphene_type, graphene.Field) assert graphene_type.type == A + assert graphene_type.description == "Reporter" def test_should_manytoone_convert_connectionorlist_connection(): @@ -248,6 +250,7 @@ class Meta: graphene_type = dynamic_field.get_type() assert isinstance(graphene_type, graphene.Field) assert graphene_type.type == A + assert graphene_type.description == "Reporter" def test_should_onetoone_convert_field(): @@ -263,6 +266,7 @@ class Meta: graphene_type = dynamic_field.get_type() assert isinstance(graphene_type, graphene.Field) assert graphene_type.type == A + assert graphene_type.description == "Favorite Article" def test_should_postgresql_uuid_convert():