📖 Scenario: You are working as a junior developer in a company that uses different types of APIs. Your manager wants you to create a simple comparison of three popular API styles: REST, SOAP, and GraphQL. This will help the team understand their differences clearly.
🎯 Goal: Create a small program that stores key features of REST, SOAP, and GraphQL in a dictionary, then filters and displays the features of the API style chosen by the user.
📋 What You'll Learn
Create a dictionary called api_features with keys 'REST', 'SOAP', and 'GraphQL', each holding a list of 3 features as strings.
Create a variable called selected_api and set it to one of the keys: 'REST', 'SOAP', or 'GraphQL'.
Use a for loop with variables feature to iterate over the list of features for the selected_api key in api_features.
Print each feature on a new line.
💡 Why This Matters
🌍 Real World
Understanding different API styles helps developers choose the right one for their projects and communicate clearly with teams.
💼 Career
Many software jobs require knowledge of REST, SOAP, and GraphQL to build or maintain web services and APIs.
Progress0 / 4 steps
1
Create the API features dictionary
Create a dictionary called api_features with these exact keys and values: 'REST' with features 'Uses HTTP methods', 'Stateless', 'JSON format'; 'SOAP' with features 'Uses XML', 'Stateful or stateless', 'Built-in error handling'; 'GraphQL' with features 'Single endpoint', 'Client specifies data', 'Strongly typed schema'.
Rest API
Hint
Use curly braces {} to create a dictionary. Each key is a string and its value is a list of strings.
2
Set the selected API type
Create a variable called selected_api and set it to the string 'REST'.
Rest API
Hint
Assign the string 'REST' to the variable selected_api.
3
Loop through features of the selected API
Use a for loop with variable feature to iterate over api_features[selected_api].
Rest API
Hint
Use a for loop to go through each feature in the list for the selected API.
4
Print the features of the selected API
Print each feature inside the for loop to display the features of the selected_api.
Rest API
Hint
Use print(feature) inside the loop to show each feature on its own line.
Practice
(1/5)
1. Which API style allows clients to request exactly the data they need in a single query?
easy
A. FTP
B. REST
C. SOAP
D. GraphQL
Solution
Step 1: Understand GraphQL's data fetching
GraphQL lets clients specify exactly which fields they want in a query, reducing over-fetching.
Step 2: Compare with REST and SOAP
REST uses fixed endpoints and HTTP methods, SOAP uses strict XML messages, neither allows flexible data selection like GraphQL.
Final Answer:
GraphQL -> Option D
Quick Check:
Exact data request = GraphQL [OK]
Hint: GraphQL = precise data queries in one request [OK]
Common Mistakes:
Confusing REST with GraphQL's flexible queries
Thinking SOAP supports flexible data selection
Choosing unrelated protocols like FTP
2. Which of the following is the correct HTTP method typically used by REST APIs to update a resource?
easy
A. PUT
B. POST
C. GET
D. CONNECT
Solution
Step 1: Recall REST HTTP methods
REST commonly uses PUT to update or replace a resource at a URL.
Step 2: Differentiate from other methods
GET retrieves data, POST creates new resources, CONNECT is for network tunnels, so only PUT updates.
Final Answer:
PUT -> Option A
Quick Check:
Update resource = PUT [OK]
Hint: PUT method updates resources in REST [OK]
Common Mistakes:
Using GET to update data
Confusing POST with update instead of create
Selecting non-REST HTTP methods like CONNECT
3. Given the following GraphQL query, what data will the client receive?
{ user(id: "123") { name email } }
medium
A. {"user": {"name": "Alice"}}
B. {"user": {"name": "Alice", "email": "alice@example.com"}}
C. {"user": {"id": "123", "name": "Alice"}}
D. {"user": {"email": "alice@example.com"}}
Solution
Step 1: Analyze the GraphQL query fields
The query requests the user's name and email fields for user with id "123".
Step 2: Determine expected response
The response includes both requested fields inside the user object, matching {"user": {"name": "Alice", "email": "alice@example.com"}}.
Final Answer:
{"user": {"name": "Alice", "email": "alice@example.com"}} -> Option B
Quick Check:
Requested fields = name and email [OK]
Hint: GraphQL returns only requested fields in response [OK]
Common Mistakes:
Expecting all user fields returned
Missing one of the requested fields
Confusing id field presence
4. You have a SOAP API call that fails due to incorrect message format. What is the most likely cause?
medium
A. Using GraphQL query syntax in the request
B. Missing HTTP GET method in the request
C. Using JSON instead of XML in the request body
D. Not specifying the resource URL correctly
Solution
Step 1: Understand SOAP message format requirements
SOAP requires XML formatted messages strictly following its schema.
Step 2: Identify common format errors
Sending JSON instead of XML breaks the SOAP message format, causing failure.
Final Answer:
Using JSON instead of XML in the request body -> Option C
Quick Check:
SOAP needs XML, not JSON [OK]
Hint: SOAP messages must be XML formatted [OK]
Common Mistakes:
Using JSON payloads with SOAP
Confusing HTTP methods for SOAP
Mixing GraphQL syntax with SOAP
5. You need to design an API for a mobile app that requires minimal data usage and flexible queries. Which API style is best and why?
hard
A. GraphQL, because it lets clients request exactly the data they need
B. REST, because it uses simple URLs and HTTP methods
C. SOAP, because it uses strict XML for secure communication
D. FTP, because it transfers files efficiently
Solution
Step 1: Identify requirements for minimal data and flexible queries
The app needs to reduce data usage and allow clients to specify data fields.
Step 2: Match API styles to requirements
GraphQL allows clients to request only needed data in one query, minimizing data transfer and providing flexibility.
Final Answer:
GraphQL, because it lets clients request exactly the data they need -> Option A
Quick Check:
Minimal data + flexible queries = GraphQL [OK]
Hint: Choose GraphQL for flexible, minimal data queries [OK]
Common Mistakes:
Choosing REST without flexible data selection
Selecting SOAP despite heavy XML overhead
Confusing FTP with API styles
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.