📖 Scenario: You are building a simple GraphQL API for a bookstore. The bookstore wants to manage its books and authors. Initially, you will create a basic data structure to hold books and authors. Then, you will add a configuration to filter books by a minimum rating. Next, you will write a GraphQL query to fetch books that meet the rating criteria. Finally, you will complete the schema with a query type that supports this filtering.
🎯 Goal: Build a GraphQL schema that demonstrates how advanced features like arguments and filtering improve flexibility in querying data.
📋 What You'll Learn
Create a list of books with exact fields and values
Add a variable for minimum rating filter
Write a GraphQL query that filters books by minimum rating
Complete the GraphQL schema with a query type supporting the filter
💡 Why This Matters
🌍 Real World
GraphQL APIs often need flexible queries to allow clients to request exactly the data they want with filters and arguments.
💼 Career
Understanding how to use arguments and define flexible schemas is essential for backend developers working with GraphQL to build scalable and maintainable APIs.
Progress0 / 4 steps
1
DATA SETUP: Create a list of books
Create a variable called books that is a list of dictionaries. Each dictionary should have these exact keys and values: {'id': 1, 'title': 'GraphQL Basics', 'author': 'Alice', 'rating': 4.5}, {'id': 2, 'title': 'Advanced GraphQL', 'author': 'Bob', 'rating': 4.8}, and {'id': 3, 'title': 'GraphQL in Practice', 'author': 'Charlie', 'rating': 4.2}.
GraphQL
Hint
Use a list with dictionaries exactly as shown.
2
CONFIGURATION: Add a minimum rating filter
Create a variable called min_rating and set it to 4.5.
GraphQL
Hint
Just assign 4.5 to min_rating.
3
CORE LOGIC: Write a GraphQL query with rating filter
Write a GraphQL query string called query that fetches books with a rating greater than or equal to min_rating. The query should be named GetBooks and accept a variable $minRating of type Float. It should request id, title, author, and rating fields for books filtered by rating_gte: $minRating.
GraphQL
Hint
Use triple quotes for the query string and include the variable and filter exactly as shown.
4
COMPLETION: Define the GraphQL schema with query type
Define a GraphQL schema string called schema that includes a Book type with fields id (Int), title (String), author (String), and rating (Float). Also define a Query type with a books field that accepts an optional argument rating_gte of type Float and returns a list of Book. Assign the full schema SDL string to schema.
GraphQL
Hint
Write the schema SDL string exactly with Book and Query types as shown.
Practice
(1/5)
1. What is one main benefit of using advanced features in GraphQL queries?
easy
A. You can request only the data you need, reducing data transfer.
B. It makes the server slower by processing more data.
C. You must always request all fields in a query.
D. It prevents you from filtering data.
Solution
Step 1: Understand GraphQL query flexibility
GraphQL allows clients to specify exactly which fields they want in a query.
Step 2: Recognize the benefit of selective data fetching
Requesting only needed data reduces the amount of data sent over the network, improving speed and efficiency.
Final Answer:
You can request only the data you need, reducing data transfer. -> Option A
Quick Check:
Selective data fetching = You can request only the data you need, reducing data transfer. [OK]
Hint: Advanced features let you pick only needed data [OK]
Common Mistakes:
Thinking GraphQL always sends all data
Believing advanced features slow down the server
Assuming filtering is not possible
2. Which of the following is the correct syntax to filter a list of users by age greater than 18 in GraphQL?
easy
A. query { users(where: {age: {gt: 18}}) { id name } }
B. query { users(filter: {age: >18}) { id name } }
C. query { users(age > 18) { id name } }
D. query { users(age_gt: 18) { id name } }
Solution
Step 1: Identify correct GraphQL filter syntax
GraphQL commonly uses nested input objects for filtering, like where: {field: {operator: value}}.
Step 2: Match the syntax to the options
query { users(where: {age: {gt: 18}}) { id name } } uses where: {age: {gt: 18}}, which is the standard way to filter for age greater than 18.
Final Answer:
query { users(where: {age: {gt: 18}}) { id name } } -> Option A
Quick Check:
Filter syntax with nested objects = query { users(where: {age: {gt: 18}}) { id name } } [OK]
Hint: Use nested objects for filters like where: {field: {operator: value}} [OK]
Common Mistakes:
Using invalid operators directly in arguments
Incorrect filter key names
Missing nested structure for conditions
3. Given the GraphQL query:
query { books(filter: {rating_gte: 4}) { title author { name } } }
What will the query return?
medium
A. An error because nested author fields are not allowed.
B. All books with rating less than 4 and their authors' names.
C. Only the titles of all books regardless of rating.
D. Books with rating greater than or equal to 4 and their authors' names.
Solution
Step 1: Understand the filter condition
The filter rating_gte: 4 means rating greater than or equal to 4.
Step 2: Analyze requested fields
The query asks for book titles and nested author names, which is valid and returns those fields.
Final Answer:
Books with rating greater than or equal to 4 and their authors' names. -> Option D
Quick Check:
Filter rating_gte=4 + nested author = Books with rating greater than or equal to 4 and their authors' names. [OK]
Hint: gte means greater or equal; nested fields are allowed [OK]
Common Mistakes:
Confusing gte with less than
Thinking nested fields cause errors
Ignoring filter conditions
4. Identify the error in this GraphQL query:
query { users(filter: {age: {gt: 20}}) { id name posts { title } } }
medium
A. The filter syntax is incorrect; 'gt' should be 'greaterThan'.
B. Nested fields like posts are not allowed inside users.
C. The query is valid and will return users older than 20 with their posts' titles.
D. The filter key should be 'where' instead of 'filter'.
Solution
Step 1: Check filter syntax
Using filter: {age: {gt: 20}} is a common and valid way to filter users older than 20.
Step 2: Verify nested fields
Requesting nested posts { title } inside users is allowed to fetch related data.
Final Answer:
The query is valid and will return users older than 20 with their posts' titles. -> Option C
Quick Check:
Valid filter and nested fields = The query is valid and will return users older than 20 with their posts' titles. [OK]
Hint: Check if filter and nested fields follow schema rules [OK]
Common Mistakes:
Assuming 'gt' is invalid
Thinking nested fields are forbidden
Confusing filter and where keywords
5. You want to fetch a list of products with their categories, but only products priced above 50 and categories that are active. Which GraphQL query structure best uses advanced features for this?
hard
A. query { products(filter: {price_gt: 50}) { name category { name active } } }
B. query { products(filter: {price_gt: 50, category: {active: true}}) { name category { name } } }
C. query { products { name category(filter: {active: true}) { name } } }
D. query { products(filter: {price_gt: 50}) { name category(filter: {active: true}) { name } } }
Solution
Step 1: Understand filtering on related fields
To filter products by price and also filter categories by active status, the filter must include nested conditions.
Step 2: Analyze options for nested filtering
query { products(filter: {price_gt: 50, category: {active: true}}) { name category { name } } } uses filter: {price_gt: 50, category: {active: true}}, which applies filters on both product and its category correctly.
Final Answer:
query { products(filter: {price_gt: 50, category: {active: true}}) { name category { name } } } -> Option B
Quick Check:
Nested filters for related fields = query { products(filter: {price_gt: 50, category: {active: true}}) { name category { name } } } [OK]
Hint: Use nested filters inside main filter for related data [OK]
Common Mistakes:
Filtering related fields outside main filter
Applying filters only on one level
Ignoring nested filter syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.