graphene-sqlalchemy icon indicating copy to clipboard operation
graphene-sqlalchemy copied to clipboard

get_query method assumes query is an attribute, fails when its a callable

Open sebastiandev opened this issue 7 years ago • 0 comments

The get_query methods from both SQLAlchemyConnectionField and SQLAlchemyObjectType assume that the Base model has an attribute query but if this is a property it will fail.

I have made this changes to fix it in SQLAlchemyConnectionField:

class SQLAlchemyConnectionField(Connection):
    @classmethod
    def get_query(cls, model, info, **kwargs):
        q = get_query(model, info.context)
        return q() if callable(q) else q

But is probably better to do it in the utils module:

def get_query(model, context):
    query = getattr(model, 'query', None)
    if not query:
        session = get_session(context)
        if not session:
            raise Exception('A query in the model Base or a session in the schema is required for querying.\n'
                            'Read more http://graphene-python.org/docs/sqlalchemy/tips/#querying')
        query = session.query(model)
    else:
        query = query() if callable(query) else query
return query

sebastiandev avatar Nov 13 '17 20:11 sebastiandev