A GraphQL query starts with the keyword query (optional), followed by curly braces { } containing the fields you want to fetch.
Click to reveal answer
beginner
How do you request specific fields in a GraphQL query?
Inside the curly braces, list the exact field names you want to get from the server. For example, { name age } fetches only the name and age fields.
Click to reveal answer
beginner
Can you explain what happens if you omit the query keyword in a GraphQL request?
The query keyword is optional. If omitted, the server treats the request as a query by default.
Click to reveal answer
intermediate
What is the purpose of nested fields in a GraphQL query?
Nested fields let you fetch related data in one request. For example, <code>{ user { name email } }</code> fetches the user's <code>name</code> and <code>email</code> inside the <code>user</code> object.
Click to reveal answer
intermediate
How do you name a GraphQL query and why?
You can name a query by writing query MyQueryName { ... }. Naming helps with debugging and caching.
Click to reveal answer
What keyword starts a GraphQL query?
Aselect
Bget
Cfetch
Dquery
✗ Incorrect
GraphQL queries start with the keyword query, although it is optional.
How do you specify which fields to retrieve in a GraphQL query?
ABy listing them inside curly braces
BBy using a WHERE clause
CBy using SELECT *
DBy writing them after a FROM keyword
✗ Incorrect
Fields are listed inside curly braces { } to specify what data to fetch.
Is the query keyword mandatory in GraphQL queries?
AOnly for mutations
BYes, always required
CNo, it is optional
DOnly for subscriptions
✗ Incorrect
The query keyword is optional; the server assumes a query if omitted.
What does this GraphQL query fetch? { user { name email } }
AUser's name and email
BAll users
CUser's ID only
DUser's password
✗ Incorrect
It fetches the name and email fields inside the user object.
Why would you name a GraphQL query?
ATo encrypt the query
BTo help with debugging and caching
CTo make it run faster
DTo avoid writing fields
✗ Incorrect
Naming queries helps tools identify and cache them, and makes debugging easier.
Describe the basic syntax of a GraphQL query and how you select fields.
Think about how you ask for specific data from a friend.
You got /4 concepts.
Explain why naming a GraphQL query can be useful.
Consider how names help organize and find things later.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a GraphQL query?
easy
A. To request specific data by naming fields inside curly braces
B. To update data on the server
C. To delete data from the database
D. To create new database tables
Solution
Step 1: Understand GraphQL query basics
A GraphQL query is designed to ask for specific data by naming the fields you want inside curly braces.
Step 2: Differentiate query from other operations
Updating, deleting, or creating data are done by mutations, not queries.
Final Answer:
To request specific data by naming fields inside curly braces -> Option A
Quick Check:
GraphQL query = request specific data [OK]
Hint: Queries ask for data by naming fields in braces [OK]
Common Mistakes:
Confusing queries with mutations
Thinking queries modify data
Assuming queries create or delete data
2. Which of the following is the correct syntax for a simple GraphQL query to get a user's name and email?
easy
A. query { user { name, email } }
B. query user { name, email }
C. { user: name, email }
D. query { user: name, email }
Solution
Step 1: Identify correct GraphQL query structure
A GraphQL query starts with optional 'query' keyword, then curly braces containing the root field and requested subfields separated by commas or spaces.
Step 2: Check each option's syntax
'query { user { name, email } }' correctly uses the 'query' keyword, outer braces, nested 'user' braces for fields. 'query user { name, email }' misses opening brace after 'query'; '{ user: name, email }' lacks 'query' and uses invalid colon; 'query { user: name, email }' uses invalid colon after 'user'.
Hint: Use curly braces to nest fields correctly [OK]
Common Mistakes:
Omitting curly braces around nested fields
Using colons incorrectly inside queries
Placing fields outside braces
3. Given the GraphQL query:
query { book { title author { name } } }
What data will this query return?
medium
A. Only the book's title
B. Only the author's name
C. The book's title and the author's name
D. The book's title and author as a string
Solution
Step 1: Analyze the query structure
The query asks for 'book' field with subfields 'title' and 'author'. The 'author' field itself has a subfield 'name'.
Step 2: Understand nested data retrieval
This means the query returns the book's title and inside author, only the name field.
Final Answer:
The book's title and the author's name -> Option C
Quick Check:
Nested fields return nested data = The book's title and the author's name [OK]
Hint: Nested braces mean nested data fields [OK]
Common Mistakes:
Thinking only top-level fields are returned
Assuming author returns full object, not just name
Confusing syntax with JSON output
4. Identify the error in this GraphQL query:
query { user { id name, } }
medium
A. Query keyword 'query' is not allowed
B. Missing parentheses after 'user'
C. Curly braces are missing around 'id' and 'name'
D. Trailing comma after 'name' is invalid
Solution
Step 1: Review field list syntax inside braces
In GraphQL, fields inside braces are separated by spaces or commas, but trailing commas are not allowed.
Step 2: Check the given query
The query has a trailing comma after 'name', which causes a syntax error.
Final Answer:
Trailing comma after 'name' is invalid -> Option D
Quick Check:
No trailing commas in field lists [OK]
Hint: Avoid trailing commas inside field lists [OK]
Common Mistakes:
Adding commas after last field
Adding parentheses unnecessarily
Removing required curly braces
5. You want to fetch a list of users with their id, name, and each user's posts with post title and comments count. Which GraphQL query correctly expresses this?
hard
A. query { users { id name posts { title commentsCount, } } }
C. query { users { id name posts { title, commentsCount } } }
D. query { users { id name posts { title commentsCount } } }
Solution
Step 1: Understand nested fields and syntax
The query must request 'users' with subfields 'id', 'name', and nested 'posts' with 'title' and 'commentsCount'. In single-line GraphQL queries, commas are required to separate field names, and trailing commas are invalid.
Step 2: Evaluate syntax correctness
'query { users { id, name, posts { title, commentsCount } } }' correctly separates all fields with commas at every level without trailing commas. The others have errors: missing commas between some fields combined with trailing comma; missing commas between top-level fields; or missing all commas between fields.