Skip to content
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

How to query by argument? #16

Closed
steinsag opened this issue Nov 2, 2016 · 10 comments
Closed

How to query by argument? #16

steinsag opened this issue Nov 2, 2016 · 10 comments

Comments

@steinsag
Copy link

steinsag commented Nov 2, 2016

I was wondering if querying a schema via specific arguments is supposed to work out of the box or if anything special must be done to make it work?

In case of the flask example, I was expecting the following to be a valid query:

{
    role(roleId: 2) {
        roleId
        name
    }
}

But I only get

Unknown argument "roleId" on field "role" of type "Query".

So how would I need to extend the example so that I could search employees by their name or retrieve a role via the ID? Or is my query just wrong?

@cauyeung
Copy link

cauyeung commented Nov 2, 2016

This worked for me following the Graphene docs. I think the Django Graphene project has some automatic filter support but I couldn't find the parallel implemented for this. You explicity define the arguments, in my case "pk". They then appear in your args in your resolve func where you can manually filter.

class Query(graphene.ObjectType):
  project = graphene.Field(ProjectType, pk=graphene.Int())

  def resolve_project(self, args, context, info):
    query = ProjectType.get_query(context)
    return query.get(args.get('pk'))

query = '''
  query {
    project(pk:1) {
      name
    }
  }
'''

@steinsag
Copy link
Author

steinsag commented Nov 4, 2016

Mmh, ok, I assumed that this kind of automatic filtering is part of the SQL Alchemy / Graphene extension. I really would like to see that implemented, but I doubt I could figure out how to extend the library to do so :-(

@jkbbwr
Copy link

jkbbwr commented Nov 15, 2016

This seems rather limiting if this isn't supported

@dervos
Copy link

dervos commented Nov 22, 2016

@steinsag You will need to update your Query in schema.py to:

class Query(graphene.ObjectType):
    node = relay.Node.Field()
    all_employees = SQLAlchemyConnectionField(Employee)
    all_roles = SQLAlchemyConnectionField(Role)
    role = relay.Node.Field(Role)

This will allow you to select roles by id with:

query Role($id: ID!){
  role(id: $id) {
    id
    name
  }
}

@kvey
Copy link
Contributor

kvey commented Dec 8, 2016

  node(id: $roleId) {
    id
    ... on Role {
      id,
      name
    }
  }
}

Will work without additionally adding role = relay.Node.Field(Role). I'm not sure about the performance impact of this versus directly defining the Field.

@aminghadersohi
Copy link

+1 It would be great to have this.

@aminghadersohi
Copy link

So I get that you can query with the relay ID. But I want to query by something else. Like name. How do i do that?

@aminghadersohi
Copy link

I figured out how to do filtering for any field. I posted a possible solution here: #27

@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

7 participants