Skip to main content
GraphQL Basics
CHAPTER 02 Beginner

Understanding APIs and GraphQL Concepts

Updated: May 13, 2026
15 min read

# CHAPTER 2

Understanding APIs and GraphQL Concepts

1. Introduction

Welcome to Chapter 2! Before diving deep into writing complex GraphQL queries and schemas, it is crucial to establish a strong foundational understanding of how web communication works. If you are new to APIs, this chapter will bridge that gap. We will explore the basics of Application Programming Interfaces (APIs), how the client-server architecture operates, and introduce the core terminology specific to the GraphQL ecosystem.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Explain what an API is in the context of web development.
  • Understand the Client-Server architecture and the Request-Response model.
  • Define essential GraphQL terminology (Schema, Query, Mutation, Resolver).
  • Understand how data flows between a client application and a GraphQL server.

3. Beginner-Friendly Explanation

Think of a web application as a busy restaurant.
  • The Client is you, the customer sitting at the table. You want food (data).
  • The Server is the kitchen. It has all the ingredients and cooks the food (the database and backend logic).
  • The API is the menu and the waiter. You can't go into the kitchen and cook yourself. You look at the menu (the API documentation), tell the waiter your order (the Request), and the waiter brings the cooked food back to your table (the Response).

GraphQL changes how you order. Instead of ordering a combo meal where you get fries whether you want them or not (REST), GraphQL lets you give the waiter a precise, customized list of exact ingredients and portions you want.

4. Real-World Examples

  • Mobile Apps: When you open the Instagram app (the Client), it needs to show your feed. It sends a Request over the internet to Instagram's backend (the Server) via an API. The server gathers the photos and likes, and sends them back as a Response.
  • Weather Widgets: A smart mirror showing the weather doesn't calculate meteorological data. It acts as a Client, requesting data from a Weather API Server.

5. Detailed Code Examples

In traditional web communication, a client makes an HTTP request. In GraphQL, this is usually a single POST request to a /graphql endpoint.

Client Request (using PHP cURL):

php
12345678910111213
<?php
$query = &#039;{"query": "query { systemStatus }" }';

$ch = curl_init(&#039;http://api.example.com/graphql');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    &#039;Content-Type: application/json'
]);

$response = curl_exec($ch);
echo $response;
?>

6. Query Examples

A Query in GraphQL is how the client asks for data. It is equivalent to a GET request in REST.
graphql
123456
query {
  getSystemStatus {
    uptime
    version
  }
}

7. Mutation Examples

A Mutation is how the client modifies data on the server.
graphql
123456
mutation {
  rebootSystem {
    success
    message
  }
}

8. Schema Examples

The Schema is the contract between the client and the server. It defines everything that is possible.
graphql
12345678
type SystemInfo {
  uptime: Int
  version: String
}

type Query {
  getSystemStatus: SystemInfo
}

9. Best Practices

  • Understand the Protocol: Remember that GraphQL is simply a specification, usually transmitted over HTTP. Treat it with the same security and network considerations as any other HTTP API.
  • Learn the Terminology: Getting comfortable with terms like *Schema*, *Type*, *Resolver*, and *Field* is essential before writing code.
  • Keep Resolvers Thin: As we will learn later, the functions that fetch your data (Resolvers) should be simple and defer heavy logic to dedicated service classes.

10. Common Mistakes

  • Confusing GraphQL with a Database: A client asking for data does not query the database directly. The client queries the GraphQL server, and the server's code queries the database.
  • Forgetting the HTTP Layer: Beginners sometimes think GraphQL is a magical new internet protocol. It still uses standard HTTP requests and responses (usually POST).

11. Mini Exercises

  1. 1. Define "Client" and "Server" in your own words.
  1. 2. If a user updates their profile picture, would the GraphQL operation be a Query or a Mutation?
  1. 3. If a user views a list of their friends, is that a Query or a Mutation?

12. Coding Challenges

Challenge 1: Write a JSON object representing what you think a GraphQL server might return if a client sends a query asking for the version of an API.

13. MCQs with Answers

Question 1

In the client-server model, what role does the mobile application play?

Question 2

Which GraphQL operation is used to modify data on the server?

Question 3

What is the "Contract" between the client and the server in GraphQL called?

14. Interview Questions

  • Q: Describe the Request-Response model in web architecture.
  • Q: What is the fundamental difference between a Query and a Mutation in GraphQL?
  • Q: What is a Schema in GraphQL and why is it important?

15. FAQs

Q: Can a single application be both a client and a server? A: Yes! A PHP backend might act as a server to a React frontend, but act as a client when it requests data from a third-party payment API.

Q: Why do GraphQL requests usually use the POST HTTP method even for fetching data? A: GraphQL queries can be very large and complex, exceeding the URL length limits of GET requests. POST allows the query to be safely sent in the request body.

16. Summary

In Chapter 2, we reviewed the fundamental architecture of the web: the Client-Server model and the Request-Response cycle. We learned that an API acts as the intermediary. We also introduced crucial GraphQL terminology. A Schema defines what is possible, a Query fetches data, a Mutation changes data, and Resolvers (which we will cover later) do the actual work of getting the data.

17. Next Chapter Recommendation

With a firm grasp on how web communication and basic concepts work, it is time to tackle the most common question in API design. Proceed to Chapter 3: GraphQL vs REST APIs to see a side-by-side comparison of the two dominant API architectures and understand exactly why GraphQL is so revolutionary.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·