graphql-python / graphene-sqlalchemy

Graphene SQLAlchemy integration
http://docs.graphene-python.org/projects/sqlalchemy/en/latest/
MIT License
975 stars 225 forks source link

Post processing relationship result with SQLAlchemyConnectionField #341

Closed ego closed 2 years ago

ego commented 2 years ago

Hi there!

Maybe someone asked this question, sorry. But how to do post processing or override relationship result with SQLAlchemyConnectionField?

https://github.com/graphql-python/graphene-sqlalchemy/blob/master/examples/flask_sqlalchemy/schema.py#L36

class Query(graphene.ObjectType):
     ....
    all_departments = SQLAlchemyConnectionField(Department.connection, sort=None)

    def resolve_all_departments(root, info, *args, **kwargs):
          # Like result = super().resolve_all_departments()  ?
          # Do post processing result / add some additional filters to query.
          # return result
erikwrede commented 2 years ago

Hey there, for filtering in graphene-sqlalchemy<3.0, take a look at graphene-sqlalchemy-filter or look in the closed and open issues, there are plenty of suggestions, you can pick the one that fits your needs best. One way you can get the query of an SQLAlchemyConnectionField is this:

class Query(graphene.ObjectType):

   all_departments = SQLAlchemyConnectionField(Department.connection, sort=None)

   def resolve_all_departments(parent, info, sort=None, **kwargs):
           query = SQLALchemyConnectionField.get_query(Department._meta.model, info, sort)
          #apply additional modificaitons to your query
          return query.all()
ego commented 2 years ago

Thanks for your time and reply.

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related topics referencing this issue.