Skip to content

Adding SQLAlchemyInputObjectType #213

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Added new Class SQLAlchemyInputObjectType
New Class which lets you give a model in a the meta class as input fields.
  • Loading branch information
Rafik-Belkadi authored May 14, 2019
commit ea91ee256b8336777f3468f64e4b5136ec40caf2
66 changes: 66 additions & 0 deletions graphene_sqlalchemy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,69 @@ def enum_for_field(cls, field_name):
sort_enum = classmethod(sort_enum_for_object_type)

sort_argument = classmethod(sort_argument_for_object_type)

def construct_fields_for_input(
obj_type, model, registry, connection_field_factory,only_fields, exclude_fields
):
inspected_model = sqlalchemyinspect(model)

fields = OrderedDict()

for name, column in inspected_model.columns.items():
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 exclude this field in exclude_fields
continue
converted_column = convert_sqlalchemy_column(column, registry)
fields[name] = converted_column

for name, composite in inspected_model.composites.items():
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 exclude this field in exclude_fields
continue
converted_composite = convert_sqlalchemy_composite(composite, 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 exclude this field in exclude_fields
continue
converted_hybrid_property = convert_sqlalchemy_hybrid_method(hybrid_item)

fields[name] = converted_hybrid_property



return fields



class SQLAlchemyInputObjectType(graphene.InputObjectType):
@classmethod
def __init_subclass_with_meta__(cls, model=None, registry=None, skip_registry=False,only_fields=(), exclude_fields=(),
connection=None,_meta=None,connection_field_factory=default_connection_field_factory,
use_connection=None, interfaces=(), id=None, **options):

if not registry:
registry = get_global_registry()


sqla_fields = yank_fields_from_attrs(
construct_fields_for_input(model=model, registry=registry,only_fields=only_fields, exclude_fields=exclude_fields,
connection_field_factory=connection_field_factory,obj_type=cls),
_as=graphene.Field,
)