Bird
Raised Fist0
GraphQLquery~20 mins

GraphQL vs REST comparison - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
GraphQL vs REST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Key difference in data fetching between GraphQL and REST
Which statement best describes how GraphQL differs from REST in fetching data?
ABoth GraphQL and REST require multiple endpoints to fetch different data types.
BREST allows clients to request exactly the data they need in a single query, while GraphQL requires multiple endpoints.
CGraphQL allows clients to request exactly the data they need in a single query, while REST often requires multiple endpoints.
DGraphQL does not support querying data, only mutations, unlike REST.
Attempts:
2 left
💡 Hint
Think about how many requests are needed to get related data in each approach.
🧠 Conceptual
intermediate
2:00remaining
Handling versioning in GraphQL vs REST
How is API versioning typically handled differently in GraphQL compared to REST?
AREST uses explicit version numbers in the URL, while GraphQL encourages evolving the schema without versioning.
BGraphQL uses explicit version numbers in the URL, while REST evolves the schema without versions.
CBoth GraphQL and REST require version numbers in the URL for every change.
DNeither GraphQL nor REST support versioning.
Attempts:
2 left
💡 Hint
Consider how clients adapt to changes in each API style.
❓ query_result
advanced
2:00remaining
GraphQL query result shape
Given this GraphQL query:
{ user(id: "1") { name, posts { title } } }

What is the expected shape of the JSON response?
A{"data": {"name": "Alice", "posts": [{"title": "Post 1"}, {"title": "Post 2"}]}}
B{"data": {"user": {"name": "Alice", "posts": [{"title": "Post 1"}, {"title": "Post 2"}]}}}
C{"user": {"name": "Alice", "posts": [{"title": "Post 1"}, {"title": "Post 2"}]}}
D{"data": {"user": {"name": "Alice"}}}
Attempts:
2 left
💡 Hint
GraphQL responses always wrap data inside a 'data' key.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this GraphQL query
Which option contains a syntax error in the GraphQL query?
GraphQL
query { user(id: "1") { name posts { title } } }
Aquery { user(id: "1") { name, posts { title } } }
B} } } eltit { stsop ,eman { )"1" :di(resu { yreuq
Cquery { user(id: "1") { name posts { title } } }
D} } } eltit { stsop eman { )"1" :di(resu { yreuq
Attempts:
2 left
💡 Hint
Look for missing commas between fields.
❓ optimization
expert
3:00remaining
Optimizing REST API calls compared to GraphQL
You have a REST API with endpoints /users and /posts. To get a user's name and their posts' titles, you must call both endpoints. How does GraphQL optimize this scenario?
AGraphQL duplicates data in each query to avoid multiple calls.
BGraphQL requires separate queries for user and posts, increasing network calls.
CGraphQL caches REST endpoints internally, so no optimization is needed.
DGraphQL allows fetching user name and posts titles in a single query, reducing network calls.
Attempts:
2 left
💡 Hint
Think about how many requests are needed in REST vs GraphQL for related data.

Practice

(1/5)
1. What is a key advantage of GraphQL compared to REST when fetching data?
easy
A. GraphQL does not support querying nested data.
B. GraphQL requires multiple URLs for different data resources.
C. GraphQL uses fixed data shapes for each resource.
D. GraphQL allows fetching exactly the data you want in a single request.

Solution

  1. Step 1: Understand GraphQL data fetching

    GraphQL lets clients specify exactly what data they want in one request, avoiding over-fetching or under-fetching.
  2. Step 2: Compare with REST approach

    REST typically requires multiple URLs and fixed data shapes, which can lead to multiple requests or extra data.
  3. Final Answer:

    GraphQL allows fetching exactly the data you want in a single request. -> Option D
  4. Quick Check:

    GraphQL = precise single-request data fetching [OK]
Hint: GraphQL = one request, exact data [OK]
Common Mistakes:
  • Thinking GraphQL needs multiple URLs
  • Believing GraphQL returns fixed data shapes
  • Assuming GraphQL can't query nested data
2. Which of the following is the correct way to define a GraphQL query to get a user's name and email?
easy
A. { user: { name, email } }
B. query { user { name, email } }
C. SELECT name, email FROM user
D. GET /user/name,email

Solution

  1. Step 1: Identify GraphQL query syntax

    GraphQL queries start with the keyword 'query' followed by the requested fields inside braces.
  2. Step 2: Check each option

    query { user { name, email } } matches GraphQL syntax. GET /user/name,email is REST style URL. SELECT name, email FROM user is SQL. { user: { name, email } } is invalid JSON-like structure.
  3. Final Answer:

    query { user { name, email } } -> Option B
  4. Quick Check:

    GraphQL query syntax = query { user { name, email } } [OK]
Hint: GraphQL queries start with 'query' keyword [OK]
Common Mistakes:
  • Using REST or SQL syntax instead of GraphQL
  • Omitting the 'query' keyword
  • Incorrect braces placement
3. Given this GraphQL query:
query { book(id: "1") { title author { name } } }

What data structure will the response most likely contain?
medium
A. {"data": {"book": {"title": "1984", "author": {"name": "George Orwell"}}}}
B. {"book": {"title": "1984", "author": "George Orwell"}}
C. {"data": {"book": {"title": "1984", "author": "George Orwell"}}}
D. {"data": {"book": {"title": "1984", "author": ["George Orwell"]}}}

Solution

  1. Step 1: Understand GraphQL response format

    GraphQL responses wrap data inside a 'data' object, matching the query structure exactly.
  2. Step 2: Match nested fields

    The query requests 'book' with 'title' and nested 'author' with 'name', so response must reflect nested objects.
  3. Final Answer:

    {"data": {"book": {"title": "1984", "author": {"name": "George Orwell"}}}} -> Option A
  4. Quick Check:

    GraphQL response = nested 'data' object [OK]
Hint: GraphQL responses wrap data inside 'data' key [OK]
Common Mistakes:
  • Omitting the 'data' wrapper
  • Flattening nested objects incorrectly
  • Using arrays for single nested objects
4. Identify the error in this GraphQL query:
query { user(id: 5) { name, email } }
medium
A. The query keyword is missing.
B. The fields 'name' and 'email' should be inside parentheses.
C. The id value should be a string, so it needs quotes.
D. GraphQL does not support querying users by id.

Solution

  1. Step 1: Check argument types in GraphQL

    GraphQL requires string arguments to be quoted. Here, id is likely a string and should be "5".
  2. Step 2: Verify field syntax

    Fields 'name' and 'email' are correctly listed with commas, no parentheses needed.
  3. Final Answer:

    The id value should be a string, so it needs quotes. -> Option C
  4. Quick Check:

    String arguments need quotes in GraphQL [OK]
Hint: Quote string arguments in GraphQL queries [OK]
Common Mistakes:
  • Leaving string arguments unquoted
  • Adding parentheses around fields
  • Assuming query keyword is optional
5. You want to design an API for a mobile app that needs flexible data fetching with minimal requests and efficient caching. Which approach is best and why?
hard
A. Use GraphQL because it allows clients to request exactly what they need in one request.
B. Use REST because it is simpler and caches well with fixed URLs.
C. Use REST because it supports nested queries better than GraphQL.
D. Use GraphQL because it requires multiple requests for different data.

Solution

  1. Step 1: Analyze requirements for flexible data fetching and minimal requests

    GraphQL excels at letting clients specify exactly what data they want, reducing multiple requests.
  2. Step 2: Consider caching and efficiency

    While REST is simpler and caches well, GraphQL's single-request flexibility suits mobile apps needing efficient data transfer.
  3. Final Answer:

    Use GraphQL because it allows clients to request exactly what they need in one request. -> Option A
  4. Quick Check:

    Flexible, minimal requests = GraphQL [OK]
Hint: Flexible single-request fetching = GraphQL [OK]
Common Mistakes:
  • Choosing REST for flexibility over GraphQL
  • Thinking GraphQL needs multiple requests
  • Assuming REST handles nested queries better
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard