0
0
GraphQLquery~30 mins

Why databases back GraphQL - See It in Action

Choose your learning style9 modes available
Why Databases Back GraphQL
📖 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.