mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-22 17:46:57 +03:00
Added uselist from a relationship for One to One mapping to a Field. Fixed #246
This commit is contained in:
parent
6f99372287
commit
ed12231ee8
|
@ -23,7 +23,7 @@ def convert_sqlalchemy_relationship(relationship, registry):
|
|||
_type = registry.get_type_for_model(model)
|
||||
if not _type:
|
||||
return None
|
||||
if direction == interfaces.MANYTOONE:
|
||||
if (direction == interfaces.MANYTOONE or not relationship.uselist):
|
||||
return Field(_type)
|
||||
elif (direction == interfaces.ONETOMANY or
|
||||
direction == interfaces.MANYTOMANY):
|
||||
|
|
|
@ -32,6 +32,7 @@ class Reporter(Base):
|
|||
email = Column(String())
|
||||
pets = relationship('Pet', secondary=association_table, backref='reporters')
|
||||
articles = relationship('Article', backref='reporter')
|
||||
favorite_article = relationship("Article", uselist=False)
|
||||
|
||||
|
||||
class Article(Base):
|
||||
|
|
|
@ -171,6 +171,19 @@ def test_should_manytoone_convert_connectionorlist_connection():
|
|||
assert graphene_type.type == A
|
||||
|
||||
|
||||
def test_should_onetoone_convert_field():
|
||||
class A(SQLAlchemyObjectType):
|
||||
class Meta:
|
||||
model = Article
|
||||
interfaces = (Node, )
|
||||
|
||||
dynamic_field = convert_sqlalchemy_relationship(Reporter.favorite_article.property, A._meta.registry)
|
||||
assert isinstance(dynamic_field, graphene.Dynamic)
|
||||
graphene_type = dynamic_field.get_type()
|
||||
assert isinstance(graphene_type, graphene.Field)
|
||||
assert graphene_type.type == A
|
||||
|
||||
|
||||
def test_should_postgresql_uuid_convert():
|
||||
assert_column_conversion(postgresql.UUID(), graphene.String)
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ def test_should_map_fields_correctly():
|
|||
model = Reporter
|
||||
registry = Registry()
|
||||
|
||||
assert list(ReporterType2._meta.fields.keys()) == ['id', 'first_name', 'last_name', 'email', 'pets', 'articles']
|
||||
assert list(ReporterType2._meta.fields.keys()) == ['id', 'first_name', 'last_name', 'email', 'pets', 'articles', 'favorite_article']
|
||||
|
||||
|
||||
def test_should_map_only_few_fields():
|
||||
|
|
|
@ -49,7 +49,7 @@ def test_sqlalchemy_interface():
|
|||
def test_objecttype_registered():
|
||||
assert issubclass(Character, ObjectType)
|
||||
assert Character._meta.model == Reporter
|
||||
assert list(Character._meta.fields.keys()) == ['id', 'first_name', 'last_name', 'email', 'pets', 'articles']
|
||||
assert list(Character._meta.fields.keys()) == ['id', 'first_name', 'last_name', 'email', 'pets', 'articles', 'favorite_article']
|
||||
|
||||
|
||||
# def test_sqlalchemynode_idfield():
|
||||
|
|
Loading…
Reference in New Issue
Block a user