API Fundamentals
/Beginner
GraphQL Queries
Definition
The GraphQL equivalent of a REST `GET` request. Queries are used by the client to request specific data from the server without modifying anything.
Explain Like I'm New
A Query is you asking the GraphQL server a very specific question. 'Please give me User 1. I only want their first name. And get their top 3 friends. I only want the friends' profile pictures.'
Real World Example
A React component fetching data to render a profile card. It writes a Query string, sends it via Apollo Client, and gets exactly the JSON needed for the UI.
Common Use Cases
- •Fetching data
- •Deeply nested data retrieval
Terminal Output
bash / terminal
# A complex GraphQL Query featuring arguments and nesting
query GetDashboardData($userId: ID!) {
# Fetch the specific user
user(id: $userId) {
name
email
# Nesting: Fetch their recent orders
orders(limit: 5, sort: "DESC") {
orderId
totalAmount
# Deep Nesting: Fetch the specific items in the order
items {
productName
price
}
}
}
}
Interview Questions
basic
- What REST method is conceptually identical to a GraphQL Query?
intermediate
- What is a GraphQL 'Resolver'?