How to write filtering query with graphql?

1.7k Views Asked by At

Currently we are using graphql/graphql-ruby library. I have wrote few queries and mutations as per our requirement.

I have the below use case, where i am not sure how to implement it,

  1. I have already an query/endpoint named allManagers which return all manager details.

  2. Today i have got requirement to implement another query to return all the managers based on the region filter.

I have 2 options to handle this scenario.

  1. Create an optional argument for region , and inside the query i need to check if the region is passed then filter based on region.

  2. Use something like https://www.howtographql.com/graphql-ruby/7-filtering/ .

Which approach is the correct one ?

1

There are 1 best solutions below

0
On

Looks like you can accomplish this with either approach. #2 looks a bit more complicated, but maybe is more extensible if you end up adding a ton of different types of filters?

are you going to be asked to select multiple regions? or negative regions (any region except north america?) - those are the types of questions you want to be thinking about when choosing an approach.

Sounds like a good conversation to have with a coworker :)

I'd probably opt to start with a simple approach and then change it out for a more complex one when the simple solution isn't solving all of my needs any more.