Skip to content

Commit

Permalink
Added description to relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Dougherty committed Apr 6, 2019
1 parent 6a96d37 commit 8168aaa
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
7 changes: 4 additions & 3 deletions graphene_sqlalchemy/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions graphene_sqlalchemy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions graphene_sqlalchemy/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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([
Expand Down
4 changes: 4 additions & 0 deletions graphene_sqlalchemy/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand Down

0 comments on commit 8168aaa

Please sign in to comment.