Skip to content

How to filter the child of the main query? #110

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

Closed
afr-dt opened this issue Jan 31, 2018 · 8 comments
Closed

How to filter the child of the main query? #110

afr-dt opened this issue Jan 31, 2018 · 8 comments

Comments

@afr-dt
Copy link

afr-dt commented Jan 31, 2018

I'm a relative newcomer to Graphene, I'm trying to make a query like this
Query example

{
   user(username: "Jon") { 
     name
     last_lame
     username
     posts(in_draft : true) { 
       title
       text
       in_draft
       update_at      
     }
   }
}

Filter the posts that are in draft. Is this possible?

Query:

class Query(graphene.ObjectType):

    user = graphene.Field(lambda: User, username=graphene.String())
    def resolve_user(self, info, username):
       query = User.get_query(info)
       return query.filter(UserModel.username == username).first()


   posts = graphene.List(lambda: Post, in_draft=graphene.Boolean())
   def resolve_posts(self, info, in_draft):
      query = Post.get_query(info)
      return query.filter(PostModel.in_draft == in_draft).all()
@flewellyn
Copy link

I also want to do these things, and have found that it does not work properly. Graphene complains that the parameters are not found on the nested type.

@adifahmi
Copy link

Hi, any update on this?
I also need to achieve same thing

@afr-dt
Copy link
Author

afr-dt commented Feb 23, 2018

Hi @adifahmi you can try to do the following your objects of type SQLAlchemyObjectType

class Post(SQLAlchemyObjectType):
    class Meta:
        model = PostModel
        # interfaces = (relay.Node, )

In this case I'm getting the resolving of posts within the object user

class User(SQLAlchemyObjectType):

    class Meta:
        model = UserModel
        interfaces = (relay.Node, )

    posts = graphene.List(lambda: Post, in_draft=graphene.Boolean())
    def resolve_posts(self, info, in_draft):
      query = Post.get_query(info)
      return query.filter(PostModel.in_draft == in_draft).all()

And in the query just call the resolver of the user

class Query(graphene.ObjectType):

    user = graphene.Field(lambda: User, username=graphene.String())
    def resolve_user(self, info, username):
       query = User.get_query(info)
       return query.filter(UserModel.username == username).first()

@afr-dt afr-dt closed this as completed Feb 23, 2018
@afr-dt afr-dt reopened this Feb 23, 2018
@adifahmi
Copy link

adifahmi commented Feb 23, 2018

Hi @aleks-fr thanks for the code

@somada141
Copy link
Contributor

Hi @aleks-fr, quick q re the code you provided above. While defining the posts under User allows one to nest them the posts wont' necessarily belong to the resolved user.

Is there a way to communicate the arguments of resolve_user to the nested resolve_posts so that posts can be limited to that user only?

@viktor992
Copy link

@somada141, it seems that self is the resolved User, so you could use PostModel.user_id = self.id to filter the posts.

class User(SQLAlchemyObjectType):

    class Meta:
        model = UserModel
        interfaces = (relay.Node, )

    posts = graphene.List(lambda: Post, in_draft=graphene.Boolean())
    def resolve_posts(self, info, in_draft):
      query = Post.get_query(info)
      return query.filter(PostModel.user_id = self.id, PostModel.in_draft == in_draft).all()

@erikwrede
Copy link
Member

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.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants