Field selection lets you choose exactly which pieces of data you want from a database. This helps you get only what you need, making your data faster and easier to use.
When you want to get just the name and email of users, not all their details.
When you need only the title and author of books in a library database.
When you want to load a list of products with only their price and name for a quick display.
When you want to reduce data transfer by asking for fewer fields in a query.
When you want to avoid showing sensitive information like passwords or private data.
Syntax
GraphQL
query {
entityName {
field1
field2
...
}
}
Replace entityName with the name of the data you want.
List only the fields you want inside the curly braces.
Examples
This gets the name and email fields of the user entity.
GraphQL
query {
user {
name
email
}
}
This fetches the title and author fields from book.
GraphQL
query {
book {
title
author
}
}
This gets the id and price of each product.
GraphQL
query {
product {
id
price
}
}
Sample Program
This query asks for the id and name fields of all users.
GraphQL
query {
user {
id
name
}
}
OutputSuccess
Important Notes
You can select as many or as few fields as you want.
If you don't select any fields, the query will be invalid.
Field selection helps reduce the amount of data sent over the network.
Summary
Field selection lets you pick exactly what data you want.
This makes your queries faster and your apps lighter.
Always select only the fields you need.
Practice
(1/5)
1. What does field selection in GraphQL allow you to do?
easy
A. Encrypt data before sending it
B. Automatically update the database schema
C. Choose only the data fields you want to receive
D. Create new database tables
Solution
Step 1: Understand the purpose of field selection
Field selection lets you specify exactly which fields you want in your query response.
Step 2: Compare options with the concept
Only Choose only the data fields you want to receive describes choosing specific data fields, which matches field selection.
Final Answer:
Choose only the data fields you want to receive -> Option C
Quick Check:
Field selection = Choose specific fields [OK]
Hint: Field selection means picking only needed data fields [OK]
Common Mistakes:
Confusing field selection with schema changes
Thinking it encrypts data
Assuming it creates database tables
2. Which of the following is the correct GraphQL syntax to select the fields id and name from a user?
easy
A. { user: id, name }
B. { user(id, name) }
C. query user { id, name }
D. { user { id, name } }
Solution
Step 1: Recall GraphQL field selection syntax
Fields are selected inside curly braces after the type name, separated by commas or spaces.
Step 2: Check each option's syntax
{ user { id, name } } correctly uses braces and field names inside user. Others have syntax errors or wrong structure.
Final Answer:
{ user { id, name } } -> Option D
Quick Check:
Correct field selection syntax = { user { id, name } } [OK]
Hint: Use braces to nest fields inside the type name [OK]
Hint: Nested fields return nested objects in response [OK]
Common Mistakes:
Expecting author as a string instead of object
Missing requested fields in response
Ignoring nested field structure
4. Identify the error in this GraphQL query:
{ user { id name, } }
medium
A. Trailing comma after last field causes syntax error
B. Missing parentheses after user
C. Fields must be quoted strings
D. Curly braces are not allowed inside queries
Solution
Step 1: Check field list syntax
GraphQL does not allow a comma after the last field in a selection set.
Step 2: Verify other syntax rules
No parentheses needed after user, fields are unquoted, and braces are required.
Final Answer:
Trailing comma after last field causes syntax error -> Option A
Quick Check:
No trailing commas in field lists [OK]
Hint: No trailing commas after last field in selection [OK]
Common Mistakes:
Adding commas after last field
Adding unnecessary parentheses
Quoting field names
5. You want to fetch a list of products with only their id and price, but each product also has a nested manufacturer with name and location. How do you write a GraphQL query to select only id, price, and the manufacturer's name (not location)?
hard
A. { products { id price manufacturer { name location } } }
B. { products { id price manufacturer { name } } }
C. { products { id price manufacturer name } }
D. { products { id price, manufacturer: { name } } }
Solution
Step 1: Identify required fields
We need id, price, and nested manufacturer with only name.
Step 2: Write correct nested field selection
{ products { id price manufacturer { name } } } correctly nests name inside manufacturer and excludes location.
Step 3: Check other options for errors
{ products { id price manufacturer { name location } } } includes unwanted location. { products { id price manufacturer name } } incorrectly places name outside manufacturer. { products { id price, manufacturer: { name } } } has invalid colon syntax.
Final Answer:
{ products { id price manufacturer { name } } } -> Option B
Quick Check:
Select only needed nested fields [OK]
Hint: Nest only needed fields inside parent fields [OK]
Common Mistakes:
Selecting unwanted nested fields
Placing nested fields outside their parent
Using colon instead of braces for nesting
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.