📖 Scenario: You are building a simple GraphQL API for a bookstore. The data about books is stored in a list of dictionaries. You want to connect this data source to your GraphQL schema so clients can query book information.
🎯 Goal: Create a GraphQL schema that integrates a data source of books and allows querying the list of books with their titles and authors.
📋 What You'll Learn
Create a list called books with three books, each having id, title, and author fields.
Define a GraphQL type Book with fields id, title, and author.
Create a query type Query with a field books that returns the list of books.
Connect the books field resolver to return the books data source.
💡 Why This Matters
🌍 Real World
GraphQL APIs often need to connect to data sources like databases or in-memory lists to serve client queries.
💼 Career
Understanding how to integrate data sources with GraphQL is essential for backend developers building APIs.
Progress0 / 4 steps
1
Create the data source list
Create a list called books with these exact entries: {"id": "1", "title": "The Great Gatsby", "author": "F. Scott Fitzgerald"}, {"id": "2", "title": "1984", "author": "George Orwell"}, and {"id": "3", "title": "To Kill a Mockingbird", "author": "Harper Lee"}.
GraphQL
Hint
Use a Python list of dictionaries to store the book data exactly as shown.
2
Define the GraphQL Book type
Define a GraphQL type called Book with fields id of type ID!, title of type String!, and author of type String!.
GraphQL
Hint
Use GraphQL SDL syntax to define the Book type with the required fields and types.
3
Add the Query type with books field
Add a GraphQL Query type with a field books that returns a list of Book objects, using the syntax books: [Book!]!.
GraphQL
Hint
Extend the GraphQL schema by adding a Query type with a books field that returns a non-null list of non-null Book objects.
4
Connect the books field resolver
Create a resolver dictionary called resolvers with a Query key. Inside Query, add a books function that returns the books list.
GraphQL
Hint
Define a resolvers dictionary with a Query key. The books resolver should be a function returning the books list.
Practice
(1/5)
1. What is the main purpose of data source integration in GraphQL?
easy
A. To style the GraphQL interface for better user experience
B. To encrypt data sent over the network
C. To create new GraphQL schema types automatically
D. To connect GraphQL queries to the actual data storage or services
Solution
Step 1: Understand what data source integration means
It means linking GraphQL queries to where data is stored or fetched from, like databases or APIs.
Step 2: Identify the main goal of this integration
The goal is to let GraphQL get data from these sources when a query runs.
Final Answer:
To connect GraphQL queries to the actual data storage or services -> Option D
Quick Check:
Data source integration = connect queries to data [OK]
Hint: Think: GraphQL needs to reach data somewhere [OK]
Common Mistakes:
Confusing integration with UI styling
Thinking it creates schema types automatically
Assuming it handles data encryption
2. Which of the following is the correct way to define a resolver that fetches data from a REST API in GraphQL?
easy
A. const resolver = () => fetch('https://api.example.com/data').then(res => res.json());
B. const resolver = fetch('https://api.example.com/data').json();
C. const resolver = () => fetch('https://api.example.com/data').json();
D. const resolver = () => fetch('https://api.example.com/data').then(res => res.text());
Solution
Step 1: Check how fetch works in JavaScript
fetch returns a promise; to get JSON, you call .then(res => res.json()).
Step 2: Verify the resolver syntax
const resolver = () => fetch('https://api.example.com/data').then(res => res.json()); correctly returns a function that fetches and parses JSON asynchronously.
A. The resolver should be async and await the db query
B. The query string is incorrect SQL syntax
C. The resolver should return a promise, not data
D. The resolver is missing a try-catch block
Solution
Step 1: Understand db.query behavior
db.query usually returns a promise, so it must be awaited in async functions.
Step 2: Check resolver function signature
The resolver is not async and does not await the promise, so it returns a pending promise instead of data.
Final Answer:
The resolver should be async and await the db query -> Option A
Quick Check:
Async + await needed for promises [OK]
Hint: If db query returns promise, resolver must be async and await it [OK]
Common Mistakes:
Ignoring async/await for promises
Assuming SQL syntax is wrong without checking
Thinking try-catch is mandatory for correctness
5. You want to integrate two data sources in a GraphQL resolver: a REST API and a SQL database. Which approach correctly combines data from both sources before returning it?