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

Query with "or" expression #681

Closed
bwanglzu opened this issue Mar 12, 2018 · 4 comments
Closed

Query with "or" expression #681

bwanglzu opened this issue Mar 12, 2018 · 4 comments

Comments

@bwanglzu
Copy link

bwanglzu commented Mar 12, 2018

Hi everyone, maybe it's a stupid question, I'm new to graphene. Is it possible for me to query like this?

query{
  things(type: "type_a" or "type_b"){
    edges{
      node{
        thing_id
      }
    }
  }
}

I mean the type argument could possibly be a String or multiple Strings (or a list).. I figured out that Django has something called django_filter.. But I'm using Flask.

Thanks!

@jkimbo
Copy link
Member

jkimbo commented Mar 17, 2018

@bwanglzu as far as I'm aware there is no such ability in the graphql spec: http://facebook.github.io/graphql/October2016/ Graphene is just a framework to implement a graphql server and so it must conform to the specification.

If you want to allow your client to arbitrarily pass filters to your underlying data fetching method you could model it as a list argument as you suggested:

class Query(graphene.ObjectType):
	things = relay.ConnectionField(ThingConnection, or_filters=graphene.List(graphene.String)

@bwanglzu
Copy link
Author

bwanglzu commented Mar 20, 2018

Thanks! @jkimbo , it works for me now!
The way I do it is to use two queries, not very elegant, but still works...

# backend
things = ConnectionField(ThingConnection, type=graphene.String())
# Graphiql front-end
query{
  things(type: "a"){
    edges{
      node{
        field_a
        field_b
      }
    }
  }
}

And to query (filter) with a list of types:

# graphene backend
things_by_type = ConnectionField(ThingConnection, type=graphene.List(graphene.String))
# Graphiql Front-end
query{
  thingsByType(type: ["type_a", "type_b"]){
    edges{
      node{
        field_a
        field_b
      }
    }
  }
}

And also, it also supports for arguments:

query($type:[String]){
  thingsByType(type:$type){
    edges{
      node{
        field_a
        field_b
      }
    }
  }
}

need a parameter in Query Variables field:

{
  "type": ["type_a", "type_b"]
}

@jkimbo
Copy link
Member

jkimbo commented Mar 20, 2018

Great, glad I could help. Generally with graphql there is not a big overhead in having multiple ways to query the data since it's up the client to decide how to query. And it's usually better to be explicit with naming so what you've come up with looks great.

@bwanglzu
Copy link
Author

bwanglzu commented Mar 29, 2018

In case you're usingSQLAlchemy, please take a look at the latest comment here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants