API Fundamentals Course
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'?

Flash Cards

Question

Which REST method?

Click to reveal answer
Answer

`GET` (Both are used for reading data without changing server state).

Question

What is a Resolver?

Click to reveal answer
Answer

While the Client writes the Query, the Server must write a 'Resolver'. A Resolver is just a backend function that connects the GraphQL schema to the actual database (e.g., instructing the server how to actually fetch the User data when requested).