Using a GraphCool backend, is there a way to have conditional filter in a query?
let's say I have a query like this:
query ($first: Int, $skip: Int, $favorited: Boolean) {
allPhotos (
first: $first
skip: $skip
filter: {
favorited: $favorited
}
)
{
id
url
title
favorited
}
}
//variables: { "first": 10, "skip", "favorited": true }
The query above would either:
1) Fetch only photos that are favorited.
2) Fetch only photos that are not favorited.
My problem is I want to be able to either:
1) query photos that are ONLY either favorited OR not favorited.
2) query photos regardless of whether or not they're favorited.
How do I conditionally include filters? Can I? I'm doing something with react-apollo in Javascript and I could figure out a solution with code, but I was wondering if there was a way to do it in graphql land.
GraphQL does not have semantics for filtering, pagination, or ordering out of the box, instead it is up to the API designer to add these to the GraphQL schema as they deem necessary and relevant for the requirements of the application.
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.
Implementing Search Functionality on the Client To implement search via the client, we'll be using the data. fetchMore function from React Apollo. We'll pass a function to our component, onSearch , that will pass in the search query. onSearch will call props.
The goal is to find an item (or a list of items) that matches some sort of criteria. For the rest of this post, we’ll use the word filter instead of search. For each filtering example, we’ll take a look at the originating schema, how to write a query within it, and a resolver implementation. As a reminder, GraphQL is data source-agnostic.
With this new field, we can query for the list — optionally filtering it down using the Genre argument. Here’s what a query might look like: Let’s say you wanted to get a very specific set of items and you already knew their ids. We could adjust our GraphQL schema to look more like this: type Query { album(id: ID!):
In contrast, when you write and use a GraphQL query, the application can get exactly the required fields. That’s one of the main advantages. After all, you don’t want to spend more resources than you actually need to. Let’s dive into a simple example to demonstrate how filtering actually works on the server side.
When using the auto-completion (with GraphiQl or Postman) the system propose the possible filter criteria. Technically the filter represent a "where" in an query (ERQL / SQL) The current filter will limit the application that have the name strictly equals to the element given in the double quotes. See below for all string filters options.
GraphQL variables don't necessarily have to be scalars -- they can be input types as well. So your query could look like this:
query ($first: Int, $skip: Int, $filter: PhotoFilter) {
allPhotos (
first: $first
skip: $skip
filter: $filter
)
{
#requested fields
}
}
The variables you pass along with your query will now include an Object (i.e. { favorited: true }
) for filter
instead of a boolean for favorited
.
To cover all three of your scenarios, send either:
{ favorited: true }
{ favorited: false }
{}
(no filters)Edit: I think the name of the input type in this case would be PhotoFilter
but you may want to double check your schema to be sure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With