Bird
Raised Fist0
GraphQLquery~30 mins

Filtering arguments in GraphQL - Mini Project: Build & Apply

Choose your learning style10 modes available
Filtering Arguments in GraphQL Queries
📖 Scenario: You are building a simple GraphQL API for a bookstore. The bookstore wants to allow users to query books and filter them by genre and minimum rating.
🎯 Goal: Create a GraphQL query that uses filtering arguments genre and minRating to return only books matching those filters.
📋 What You'll Learn
Define a GraphQL query called books that accepts two optional arguments: genre (String) and minRating (Float).
The query should return a list of books with fields title, author, genre, and rating.
Filter the books so that only those matching the genre argument and having a rating greater than or equal to minRating are returned.
If no arguments are provided, return all books.
💡 Why This Matters
🌍 Real World
Filtering arguments in GraphQL queries are commonly used in real-world APIs to allow clients to request only the data they need, improving efficiency and user experience.
💼 Career
Understanding how to implement filtering in GraphQL queries is essential for backend developers working with modern APIs, enabling them to build flexible and performant data services.
Progress0 / 4 steps
1
Define the Book type and sample data
Create a GraphQL type called Book with fields title (String), author (String), genre (String), and rating (Float). Also, define a sample list of books called books with exactly these entries: { title: "The Hobbit", author: "J.R.R. Tolkien", genre: "Fantasy", rating: 4.8 }, { title: "1984", author: "George Orwell", genre: "Dystopian", rating: 4.6 }, { title: "Clean Code", author: "Robert C. Martin", genre: "Programming", rating: 4.7 }.
GraphQL
Hint

Define the Book type with the four fields. Then create a constant array books with the exact book objects.

2
Add filtering arguments to the books query
Add a GraphQL query called books that accepts two optional arguments: genre of type String and minRating of type Float. The query should return a list of Book objects.
GraphQL
Hint

Define the Query type with a books field that takes genre and minRating as optional arguments and returns a list of Book.

3
Implement resolver logic to filter books
Write a resolver function for the books query that filters the books list. Use the arguments genre and minRating to return only books where the genre matches (if provided) and the rating is greater than or equal to minRating (if provided). If no arguments are given, return all books.
GraphQL
Hint

Use books.filter() and check if args.genre is given, then match book.genre. Similarly, check args.minRating against book.rating. Return true only if both conditions pass.

4
Complete the GraphQL schema export
Export the GraphQL schema by combining the typeDefs string with the Book and Query types, and export the resolvers object. Use the exact variable names typeDefs and resolvers.
GraphQL
Hint

Define typeDefs as a template string with the schema, then export both typeDefs and resolvers.

Practice

(1/5)
1.

What is the main purpose of filtering arguments in a GraphQL query?

easy
A. To get only the data you need by specifying conditions
B. To change the structure of the data returned
C. To add new data to the database
D. To delete data from the database

Solution

  1. Step 1: Understand filtering arguments

    Filtering arguments are used to specify conditions that limit the data returned by a query.
  2. Step 2: Identify the purpose in the context of data retrieval

    They help get only the data you need, making queries efficient and faster.
  3. Final Answer:

    To get only the data you need by specifying conditions -> Option A
  4. Quick Check:

    Filtering arguments = get needed data [OK]
Hint: Filtering arguments limit data returned by conditions [OK]
Common Mistakes:
  • Thinking filtering changes data structure
  • Confusing filtering with data modification
  • Assuming filtering adds or deletes data
2.

Which of the following is the correct syntax to filter users by age greater than 25 in a GraphQL query?

{
  users(age_gt: 25) {
    id
    name
  }
}
easy
A. { users(age: {gt: 25}) { id name } }
B. { users(age_gt: 25) { id name } }
C. { users(filter: {age: 25}) { id name } }
D. { users(age > 25) { id name } }

Solution

  1. Step 1: Identify correct GraphQL filter syntax

    GraphQL uses argument names like age_gt to filter values greater than 25.
  2. Step 2: Compare options with standard syntax

    { users(age_gt: 25) { id name } } uses age_gt: 25 correctly; others use invalid or incorrect syntax.
  3. Final Answer:

    { users(age_gt: 25) { id name } } -> Option B
  4. Quick Check:

    Correct filter syntax = { users(age_gt: 25) { id name } } [OK]
Hint: Use _gt suffix for 'greater than' filters [OK]
Common Mistakes:
  • Using operators like > inside arguments
  • Passing filter as nested object incorrectly
  • Using equality instead of greater than
3.

Given the query below, what will be the output if the database has users with ages 20, 30, and 40?

{
  users(age_gte: 30) {
    name
  }
}
medium
A. [{"name": "User30"}, {"name": "User40"}]
B. [{"name": "User20"}]
C. [{"name": "User20"}, {"name": "User30"}, {"name": "User40"}]
D. []

Solution

  1. Step 1: Understand the filter age_gte: 30

    This filter returns users with age greater than or equal to 30.
  2. Step 2: Check which users match the condition

    Users with ages 30 and 40 match; user with age 20 does not.
  3. Final Answer:

    [{"name": "User30"}, {"name": "User40"}] -> Option A
  4. Quick Check:

    age_gte 30 returns users 30 and 40 [OK]
Hint: gte means greater than or equal [OK]
Common Mistakes:
  • Including users younger than 30
  • Confusing gte with gt
  • Returning empty list incorrectly
4.

Identify the error in the following GraphQL query that tries to filter products by price less than 100:

{
  products(price_lt 100) {
    id
    name
  }
}
medium
A. Incorrect field name 'products' instead of 'productList'
B. Using wrong filter suffix 'lt' instead of 'lessThan'
C. Missing curly braces around filter argument
D. Missing colon ':' between argument name and value

Solution

  1. Step 1: Check argument syntax in the query

    The argument price_lt 100 is missing a colon ':' between the argument name and value.
  2. Step 2: Confirm correct syntax for filtering

    Correct syntax requires a colon: price_lt: 100.
  3. Final Answer:

    Missing colon ':' between argument name and value -> Option D
  4. Quick Check:

    Arguments need ':' separator [OK]
Hint: Always use ':' between argument and value [OK]
Common Mistakes:
  • Omitting colon ':' after argument name
  • Using wrong filter suffix names
  • Confusing field names
5.

You want to query a list of books published after 2010 and with more than 300 pages. Which of the following GraphQL queries correctly applies both filters?

{
  books(published_gt: 2010, pages_gt: 300) {
    title
    author
  }
}
hard
A. { books(filter: {published: {gt: 2010}, pages: {gt: 300}}) { title author } }
B. { books(published: {gt: 2010}, pages: {gt: 300}) { title author } }
C. { books(published_gt: 2010, pages_gt: 300) { title author } }
D. { books(published_gt: 2010 pages_gt: 300) { title author } }

Solution

  1. Step 1: Understand how to apply multiple filters in GraphQL

    Filters are passed as separate arguments separated by commas.
  2. Step 2: Check each option for correct syntax and filter usage

    { books(published_gt: 2010, pages_gt: 300) { title author } } correctly uses commas and proper filter names; others have syntax errors or wrong structure.
  3. Final Answer:

    { books(published_gt: 2010, pages_gt: 300) { title author } } -> Option C
  4. Quick Check:

    Multiple filters use commas and correct suffixes [OK]
Hint: Separate filters with commas and use _gt suffix [OK]
Common Mistakes:
  • Missing commas between arguments
  • Using nested filter objects incorrectly
  • Omitting colons between argument and value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard