Skip to content

Added sqlalchemy utils type TSVectory #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions graphene_sqlalchemy/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .fields import SQLAlchemyConnectionField

try:
from sqlalchemy_utils import ChoiceType, JSONType, ScalarListType
from sqlalchemy_utils import ChoiceType, JSONType, ScalarListType, TSVectorType
except ImportError:
class ChoiceType(object):
pass
Expand Down Expand Up @@ -48,7 +48,10 @@ def dynamic_type():
return Field(List(_type))

return Dynamic(dynamic_type)


def convert_sqlalchemy_hybrid_method(hybrid_item):
return String(description=getattr(hybrid_item, '__doc__', None),
required=False)

def convert_sqlalchemy_composite(composite, registry):
converter = registry.get_converter_for_composite(composite.composite_class)
Expand Down Expand Up @@ -97,6 +100,7 @@ def convert_sqlalchemy_type(type, column, registry=None):
@convert_sqlalchemy_type.register(types.Enum)
@convert_sqlalchemy_type.register(postgresql.ENUM)
@convert_sqlalchemy_type.register(postgresql.UUID)
@convert_sqlalchemy_type.register(TSVectorType)
def convert_column_to_string(type, column, registry=None):
return String(description=get_column_doc(column),
required=not(is_column_nullable(column)))
Expand Down
22 changes: 21 additions & 1 deletion graphene_sqlalchemy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import six
from sqlalchemy.inspection import inspect as sqlalchemyinspect
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm.exc import NoResultFound

from graphene import Field, ObjectType
Expand All @@ -13,7 +14,8 @@

from .converter import (convert_sqlalchemy_column,
convert_sqlalchemy_composite,
convert_sqlalchemy_relationship)
convert_sqlalchemy_relationship,
convert_sqlalchemy_hybrid_method)
from .registry import Registry, get_global_registry
from .utils import get_query, is_mapped

Expand Down Expand Up @@ -47,6 +49,24 @@ def construct_fields(options):
converted_composite = convert_sqlalchemy_composite(composite, options.registry)
fields[name] = converted_composite

for hybrid_item in inspected_model.all_orm_descriptors:

if type(hybrid_item) == hybrid_property:
name = hybrid_item.__name__

is_not_in_only = only_fields and name not in only_fields
is_already_created = name in options.fields
is_excluded = name in exclude_fields or is_already_created

if is_not_in_only or is_excluded:
# We skip this field if we specify only_fields and is not
# in there. Or when we excldue this field in exclude_fields

continue
converted_hybrid_property = convert_sqlalchemy_hybrid_method(
hybrid_item)
fields[name] = converted_hybrid_property

# Get all the columns for the relationships on the model
for relationship in inspected_model.relationships:
is_not_in_only = only_fields and relationship.key not in only_fields
Expand Down