📖 Scenario: You are building a simple GraphQL API for a bookstore. The data about books is stored in a database. You want to understand how the database supports the GraphQL queries by providing the data.
🎯 Goal: Create a basic GraphQL schema and simulate how the database data is structured and accessed to back the GraphQL queries.
📋 What You'll Learn
Create a list of books with exact titles and authors
Add a configuration variable for filtering books by minimum year
Write a GraphQL query resolver that returns books filtered by the minimum year
Complete the GraphQL schema with the query type and resolver
💡 Why This Matters
🌍 Real World
GraphQL APIs rely on databases to store and retrieve data efficiently. Understanding this connection helps build better APIs.
💼 Career
Backend developers often write GraphQL resolvers that fetch data from databases. This project shows the basic flow from data storage to API response.
Progress0 / 4 steps
1
DATA SETUP: Create the initial data structure
Create a list called books with these exact entries: {"title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "year": 1925}, {"title": "1984", "author": "George Orwell", "year": 1949}, and {"title": "To Kill a Mockingbird", "author": "Harper Lee", "year": 1960}.
GraphQL
Hint
Use a list of dictionaries to store the books with their title, author, and year.
2
CONFIGURATION: Add a filter variable
Create a variable called min_year and set it to 1930 to filter books published from this year onward.
GraphQL
Hint
This variable will help filter the books by their publication year.
3
CORE LOGIC: Filter books by minimum year
Create a list called filtered_books that contains only the books from books where the year is greater than or equal to min_year.
GraphQL
Hint
Use a list comprehension to select books with year >= min_year.
4
COMPLETION: Define GraphQL schema and resolver
Define a GraphQL type Query with a field books that returns a list of books. Implement a resolver function called resolve_books that returns the filtered_books list.
GraphQL
Hint
Define the GraphQL schema string and a resolver function that returns the filtered books.
Practice
(1/5)
1. Why do GraphQL APIs usually connect to databases?
easy
A. To speed up the internet connection
B. To make the app's user interface colorful
C. To store and organize the data that apps request
D. To create animations in the app
Solution
Step 1: Understand GraphQL's role
GraphQL lets apps ask for specific data they need.
Step 2: Identify why databases are needed
Databases store and organize data safely for GraphQL to fetch.
Final Answer:
To store and organize the data that apps request -> Option C
Quick Check:
GraphQL needs data storage = To store and organize the data that apps request [OK]
Hint: GraphQL fetches data; databases keep data safe [OK]
Common Mistakes:
Thinking GraphQL creates UI effects
Confusing data storage with internet speed
Assuming GraphQL handles animations
2. Which of the following is the correct way to describe how GraphQL queries data from a database?
easy
A. GraphQL sends a query to the database asking for specific fields
B. GraphQL directly edits the database tables without queries
C. GraphQL stores data inside the app without using a database
D. GraphQL only works with files, not databases
Solution
Step 1: Understand GraphQL query behavior
GraphQL sends queries specifying exactly which data fields it wants.
Step 2: Check database interaction
Databases respond to these queries by returning requested data.
Final Answer:
GraphQL sends a query to the database asking for specific fields -> Option A
Quick Check:
GraphQL queries database fields = GraphQL sends a query to the database asking for specific fields [OK]
3. Given this GraphQL query: { user(id: 1) { name, email } } What does the database return?
medium
A. User with id 1's name and email only
B. All users with their names and emails
C. An error because the query is incomplete
D. User with id 1's password and email
Solution
Step 1: Analyze the query fields
The query asks for user with id 1, requesting only name and email fields.
Step 2: Understand database response
The database returns data matching id 1 with only requested fields: name and email.
Final Answer:
User with id 1's name and email only -> Option A
Quick Check:
Query requests specific user fields = User with id 1's name and email only [OK]
Hint: Query fields limit data returned from database [OK]
Common Mistakes:
Assuming all users are returned
Expecting password field without request
Thinking query is incomplete and errors
4. A developer wrote this GraphQL query: { product(id: ) { name price } } What is the main issue?
medium
A. GraphQL does not support querying products
B. Query will return all products instead of one
C. Fields name and price are invalid
D. Missing value for id causes syntax error
Solution
Step 1: Check query syntax
The id argument is empty, which is invalid syntax in GraphQL.
Step 2: Understand error impact
This causes a syntax error, so the query won't run or return data.
Final Answer:
Missing value for id causes syntax error -> Option D
Quick Check:
Empty argument value = syntax error [OK]
Hint: Always provide values for required arguments [OK]
Common Mistakes:
Assuming query returns all products
Thinking fields are invalid
Believing GraphQL can't query products
5. An app uses GraphQL to fetch user data from a database. The query requests { user(id: 5) { name, posts { title } } }. Why is the database important here?
hard
A. It creates the GraphQL query automatically
B. It stores user and post data so GraphQL can fetch nested info
C. It styles the user interface with data colors
D. It speeds up the app by skipping data storage
Solution
Step 1: Understand nested query
The query asks for user info and their posts' titles, which is nested data.
Step 2: Role of database
The database holds both user and post data, allowing GraphQL to retrieve nested related data.
Final Answer:
It stores user and post data so GraphQL can fetch nested info -> Option B
Quick Check:
Database stores nested data = It stores user and post data so GraphQL can fetch nested info [OK]
Hint: Database holds nested data GraphQL queries [OK]
Common Mistakes:
Thinking database creates queries
Confusing data storage with UI styling
Assuming database skips storing data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.