Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter greater than in GraphQL

Tags:

graphql

type Person {
    firstName: String!,
    lastName: String!,
    age: Int!
}

How to query all people what are older than 18?

like image 307
Romper Avatar asked Aug 14 '17 12:08

Romper


3 Answers

It probably depends on the backend you are using but e.g. in graph.cool you could something like this:

query {
  allPersons(filter: {
    age_gt: 18
  }) {
    firstName
    lastName
  }
}
like image 173
sinned Avatar answered Oct 26 '22 09:10

sinned


If you are using Prisma as backend, you could use the greater than operator (_gt), like so:

query {
  persons(where: {age_gt: 18}) {
    firstName
    lastName
    age
  }
}

You can also use other operators like:

  • _gt (greater than)
  • _lt (less than)
  • _gte (greater than or equal to)
  • _lte (less than or equal to)
  • _in (equal to)
  • _not_in (not equal to)

They are compatible with any data types like Integer, Float, Double, Text, Boolean, Date, etc.

like image 29
abranhe Avatar answered Oct 26 '22 07:10

abranhe


Hasura

query {
  article(
    where: {rating: {_gte: 4}}
  ) {
    id
    title
    rating
  }
}

Prisma

query {
  posts(where: {
    AND: [{
      title_in: ["My biggest Adventure", "My latest Hobbies"]
    }]
  }) {
    id
    title
  }
}

Advice Do not use graphql directly/independently ,use it with above orm's

like image 37
vijay Avatar answered Oct 26 '22 09:10

vijay