Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL "not equal" operator?

Tags:

graphql

I have a GraphQl API for listing a bunch of items, and I can query it perfectly etc. But now I'd like to query for a subset of that list where one property can have 'all possible values except one specific one'.

For example, I want to query something like this:

{
  items(status: !"Unwanted"){
    id
  }
}

That exclamation mark obviously doesn't work, but it illustrates what I am after. Can't find any information about this online.

Does anybody know of a way to do this? I would really hate having to enumerate all possible wanted values instead of just excluding the one unwanted value. This would be really bad design and is not scalable.

Thanks.

like image 498
batjko Avatar asked Apr 08 '16 11:04

batjko


People also ask

How do you use condition in GraphQL?

Every GraphQL search filter can use and , or , and not operators. GraphQL syntax uses infix notation, so: “a and b” is a, and: { b } , “a or b or c” is a, or: { b, or: c } , and “not” is a prefix ( not: ). The and operator is implicit for a single filter object, if the fields don't overlap.

What is GraphiQL tool?

What is GraphiQL? GraphiQL is the GraphQL integrated development environment (IDE). It's a powerful (and all-around awesome) tool you'll use often while building Gatsby websites. You can access it when your site's development server is running—normally at http://localhost:8000/___graphql .

Which function used in GraphQL schema performs operations and returns data?

A resolver is a function that's responsible for populating the data for a single field in your schema. It can populate that data in any way you define, such as by fetching data from a back-end database or a third-party API.


2 Answers

Use ne :

{
  items(filter: {status: {ne: "Unwanted"}}){
    id
  }
}
like image 57
kangaswad Avatar answered Sep 20 '22 13:09

kangaswad


If you can define the schema (implement the server) then you can add a second argument like statusExcept to the items field. Then in the resolve method check if status or statusExcept was set and deliver the items according to that.

It the server API is fixed there is afaik nothing that you can do except getting everything and filter on the client.

like image 28
Matthias247 Avatar answered Sep 21 '22 13:09

Matthias247