-
Notifications
You must be signed in to change notification settings - Fork 227
Flexible arguments in SQLAlchemyConnectionField and custom query to filter against arguments #137
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
Comments
I have been implementing something similar myself and have one suggested change to the implementation here. The code in It made implementing this externally a lot cleaner for me
|
A builtin way to support declaring extra inputs for the This is a much less complete solution, but just for an idea about what people are doing to work around it. But does what I need it to 99% of the time. class RoleInput(graphene.InputObjectType):
role = String()
class FilteredConnectionField(SQLAlchemyConnectionField):
def __init__(self, type, input_type, *args, **kwargs):
fields = {name: field.type() for name, field in input_type._meta.fields.items()}
kwargs.update(fields)
super().__init__(type, *args, **kwargs)
@classmethod
def get_query(cls, model, info, sort=None, **args):
query = super().get_query(model, info, sort=sort, **args)
omitted = ('first', 'last', 'hasPreviousPage', 'hasNextPage', 'startCursor', 'endCursor')
for name, val in args.items():
if name in omitted: continue
col = getattr(model, name, None)
if col:
query = query.filter(col == val)
return query
class Query(graphene.ObjectType):
users = FilteredConnectionField(Users, RoleInput) |
What about graphene-sqlalchemy-filter? That seems to achieve what you need, no? |
As a developer, a feedback - the learning curve of graphene-sqlalchemy increases so much when so many new libraries ,and then their evermore classes, involved. |
How do I use this on a Schema Object which uses relay for its interface, like this one -
|
Thanks to you I extended it into something more versatile for filter fields in Flask / graphQL / sqlalchemy :
Then for the query :
and here PostObject is simply :
I hope this class helps others. |
I finally found the way to do this in the right way. Simply add graphene.Argument to kwargs of SQLAlchemyConnectionField like below.
We can accept the status argument in Query.
You can also accept Hope this helps. |
Following up on @shota's solution, if you consistently need the same arguments, consider including a default among class MySQLAlchemyConnectionField(SQLAlchemyConnectionField):
def __init__(self, *args, **kwargs):
super(*args, status=graphene.Argument(graphene.String), **kwargs)
class Query(graphene.ObjectType):
users = MySQLAlchemyConnectionField(User.connection)
def resolve_users(self, info=None, sort=None, status=None):
return fetch_users(status=status) |
I am closing all old issues&PRs related to filtering. The discussion will continue in #347 (WIP). A proposal for the implementation of filters is currently being worked on and will be posted there once it is ready. |
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. |
Hi maintainers,
I have this situation that I need more args to filter the SQLAlchemyConnectionField. Currently from graphene, it gives us (before, after, first, last). My objective is to add additional args for each Model included.
Here be dragons.
Models and theirs attributes:
Job: name, blocks
Block: name, job
Note: 1 Job has 0 or more Blocks
Arguments:
jobs: name, before, after, first, last
blocks: before, after,first,last
Notes:
Apologies for long explanation, I really hope this illustrates the situation I am facing and the proposal from me to add two additional filters/hooks into the SQLAlchemyConnectionField. I am sure this proposal will be greatly accepted by community, since from my perspective this is immediate update that I require to do after installing your module.
I am happy to make the PR changes including the test to the master, just let me know whether the hook/filter naming convention is good, the arguments in the def is good and also the way that I approach to my problem is acceptable. I am open to any discussion.
Thanks!!
The text was updated successfully, but these errors were encountered: