📖 Scenario: You are working on a simple online bookstore. Customers want to see only the book titles and authors, not all the details like price or stock. You will learn how to write queries that ask for just the data you need.
🎯 Goal: Build a GraphQL query that requests only the title and author fields from a list of books. This helps to get just the information needed without extra data.
📋 What You'll Learn
Create a GraphQL query named GetBooks
Request the title and author fields for each book
Do not request any other fields like price or stock
💡 Why This Matters
🌍 Real World
In real apps, requesting only the data you need saves time and bandwidth, making apps faster and easier to use.
💼 Career
Knowing how to write precise queries is important for backend and frontend developers working with APIs and databases.
Progress0 / 4 steps
1
Set up the basic query structure
Write the start of a GraphQL query named GetBooks that will ask for books data. Begin with the query GetBooks { line.
GraphQL
Hint
Start your query with query GetBooks { and inside it, add books { } to get book data.
2
Add the title field to the query
Inside the books field, request the title field exactly as title.
GraphQL
Hint
Inside books { }, add the line title to get the book titles.
3
Add the author field to the query
Add the author field inside the books selection, below title, to get the author names.
GraphQL
Hint
After title, add author to get the authors of the books.
4
Complete the query without extra fields
Make sure your query only requests title and author fields inside books. Do not add any other fields like price or stock.
GraphQL
Hint
Check your query only has title and author inside books.
Practice
(1/5)
1. Why does a GraphQL query request only specific fields instead of all data?
easy
A. To get only the data needed and avoid extra work
B. Because GraphQL cannot fetch all data at once
C. To make the query syntax simpler
D. Because databases only store partial data
Solution
Step 1: Understand GraphQL query purpose
GraphQL lets you ask for exactly the data you want, no more.
Step 2: Reason about efficiency
Requesting only needed fields saves time and resources by avoiding extra data transfer.
Final Answer:
To get only the data needed and avoid extra work -> Option A
Quick Check:
Specific data request = Efficiency [OK]
Hint: Remember: ask only what you need to save resources [OK]
Common Mistakes:
Thinking GraphQL cannot fetch all data
Believing syntax is simpler by requesting all fields
Assuming databases store only partial data
2. Which of the following is the correct GraphQL query syntax to request only the name and age fields of a user?
easy
A. { user name age }
B. { user: name, age }
C. query user { name, age }
D. { user { name age } }
Solution
Step 1: Recall GraphQL query structure
A query requests a field (like user) and inside braces lists requested subfields.
Step 2: Check each option's syntax
{ user { name age } } correctly nests name and age inside user. Others have syntax errors or wrong structure.
Final Answer:
{ user { name age } } -> Option D
Quick Check:
Correct nesting = { user { name age } } [OK]
Hint: Fields must be inside braces after the parent field [OK]
Common Mistakes:
Omitting braces around fields
Using commas inside GraphQL queries
Incorrect alias or colon usage
3. Given this GraphQL query:
{ book { title author { name } } }
What data will the query return?
medium
A. Only the book titles
B. All book and author details
C. Book titles and author names
D. Only author names
Solution
Step 1: Analyze requested fields
The query asks for title of book and inside author it asks for name.
Step 2: Determine returned data
It returns book titles plus the names of their authors only, no other details.
Final Answer:
Book titles and author names -> Option C
Quick Check:
Requested fields = title + author name [OK]
Hint: Look inside braces to see exactly what fields are requested [OK]
Common Mistakes:
Assuming all author details are returned
Thinking only book titles come back
Confusing nested fields with flat fields
4. Identify the error in this GraphQL query:
{ user { id, name, email } }
medium
A. Missing query keyword
B. Commas between fields are not allowed
C. Braces are misplaced
D. Field names must be capitalized
Solution
Step 1: Check GraphQL field list syntax
GraphQL fields inside braces should be space-separated without commas.
Step 2: Identify the error
The commas between id, name, and email cause a syntax error.
Final Answer:
Commas between fields are not allowed -> Option B
Quick Check:
No commas in field lists [OK]
Hint: Separate fields by spaces, not commas [OK]
Common Mistakes:
Using commas between fields
Adding unnecessary keywords
Capitalizing field names incorrectly
5. You want to fetch a list of users with their id, name, and only their email if it exists. Which GraphQL query correctly requests this conditional data?
hard
A. query($showEmail: Boolean!) { users { id name email @include(if: $showEmail) } }
B. { users { id name email @include(if: true) } }
C. { users { id name email @skip(if: false) } }
D. { users { id name email } }
Solution
Step 1: Understand conditional field inclusion
GraphQL uses directives like @include with variables to conditionally request fields.
Step 2: Evaluate options
query($showEmail: Boolean!) { users { id name email @include(if: $showEmail) } } uses a variable $showEmail to include email only if true, which matches the requirement.
Final Answer:
query($showEmail: Boolean!) { users { id name email @include(if: $showEmail) } } -> Option A
Quick Check:
Use @include with variable for conditional fields [OK]
Hint: Use @include with variables to request fields conditionally [OK]
Common Mistakes:
Always requesting email without condition
Using @include with fixed true/false instead of variables
Confusing @skip and @include directives
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.