0
0
GraphQLquery~30 mins

Filtering arguments in GraphQL - Mini Project: Build & Apply

Choose your learning style9 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.