REST vs GraphQL: Essential Concepts for API-Driven Mobile Apps Quiz

Explore the fundamental differences between REST and GraphQL APIs for mobile app development. This beginner-level quiz covers use cases, data handling, performance, and essential terminology to help users understand optimal API strategies for mobile applications.

  1. Key Difference in Data Fetching

    Which statement correctly describes how REST and GraphQL differ in fetching data for a mobile app that needs a user's name and email in one screen?

    1. REST can only fetch the name, while GraphQL can only fetch the email.
    2. REST may require multiple endpoints, while GraphQL can fetch both from a single request.
    3. REST can fetch multiple resources in one call, but GraphQL requires separate queries.
    4. Both REST and GraphQL cannot fetch multiple fields in one request.

    Explanation: GraphQL enables the client to specify exactly what data is needed in a single query, making it easy to fetch multiple fields such as name and email at once. REST often organizes data in separate endpoints (like /users/name and /users/email), possibly requiring multiple requests. The second option is incorrect because it's the opposite of what occurs; GraphQL combines queries while REST may separate them. The third option is false since both can return multiple fields in a response. The last statement is entirely wrong, as both methods can fetch various attributes.

  2. Overfetching in API Responses

    What is 'overfetching' in the context of API responses for mobile apps?

    1. Receiving more data than needed in a single API response.
    2. Fetching data from unauthorized endpoints.
    3. Receiving less data than requested.
    4. Requesting the same endpoint multiple times by mistake.

    Explanation: Overfetching refers to the scenario where an API, often with REST, returns more information than needed by the app, resulting in inefficient data transfer. This commonly occurs with fixed endpoints returning complete objects. Receiving less data is called 'underfetching.' Fetching from unauthorized endpoints is a security issue, not overfetching. Accidentally requesting the same endpoint several times refers to redundant calls, not related to overfetching.

  3. Error Handling Approach

    How does error handling typically differ between REST and GraphQL APIs when a mobile app receives a bad request?

    1. GraphQL uses status codes, while REST does not handle errors.
    2. REST and GraphQL both ignore bad requests.
    3. REST sends HTTP status codes, while GraphQL returns errors in the response body.
    4. Both REST and GraphQL use only HTTP status codes.

    Explanation: REST commonly signals errors with HTTP status codes such as 400 or 404. In contrast, GraphQL uses a successful HTTP status code and provides errors inside the response body, describing issues with the request. The second option is incorrect, as GraphQL typically encapsulates errors. The third is incorrect because REST does handle errors via status codes, and GraphQL does not rely solely on status codes. Both APIs acknowledging errors makes the fourth option incorrect.

  4. Versioning Strategies

    Which statement accurately describes how versioning is commonly handled in REST and GraphQL for mobile APIs?

    1. Versioning is automatic in both REST and GraphQL.
    2. REST often uses different URLs for versions, while GraphQL avoids versioning by evolving the schema.
    3. REST never needs versioning, and GraphQL always uses versioned endpoints.
    4. Both REST and GraphQL require endpoint versioning for every change.

    Explanation: REST typically manages changes by creating new endpoints or URLs with version numbers (such as /v1/ or /v2/). GraphQL aims to avoid explicit versioning by evolving the schema, adding or deprecating fields without breaking existing queries. The second and third choices are inaccurate because REST may need versioning, and GraphQL generally does not need to version endpoints for every change. The last option is incorrect, as versioning requires intentional design in both methods.

  5. Best Use Case Scenario

    For a mobile app requiring frequent updates to the screen with tailored data needs, which API style is usually more efficient and why?

    1. REST, as it automatically aggregates all endpoints.
    2. GraphQL, because clients request exactly the data needed.
    3. GraphQL, since it restricts data access more than REST.
    4. REST, since it always provides the smallest response.

    Explanation: GraphQL allows the client to specify precisely what information is needed, reducing unnecessary data and optimizing performance for frequently updated screens. REST responses might include more data than required (overfetching), making them less efficient in such use cases. The third choice is incorrect since data restriction depends on schema and permissions, not API type. REST does not automatically aggregate data across endpoints, making the last option wrong.

  6. Request Types and Methods

    Which HTTP methods are generally used in REST compared to GraphQL for mobile API requests?

    1. REST uses PATCH exclusively; GraphQL uses PUT exclusively.
    2. GraphQL uses GET for data fetch and DELETE for removal, while REST only uses POST.
    3. REST uses multiple methods like GET, POST, PUT, DELETE, while GraphQL commonly uses POST.
    4. REST and GraphQL both only use the GET method.

    Explanation: REST utilizes a variety of standard HTTP verbs corresponding to CRUD operations—GET, POST, PUT, DELETE, and sometimes PATCH. GraphQL most commonly uses the POST method for queries and mutations via a single endpoint, though it may support others. The second and third answers misstate the general usage and standard practices. The fourth option is incorrect as neither REST nor GraphQL relies exclusively on PATCH or PUT.

  7. Schema Flexibility

    How does schema flexibility differ between REST and GraphQL in mobile APIs when adding new fields to data?

    1. REST and GraphQL both require versioning for each new field.
    2. GraphQL cannot handle changes in schema without new endpoints.
    3. REST automatically merges new fields into all existing endpoints.
    4. GraphQL's schema allows new fields to be added without breaking existing queries, unlike typical REST endpoints.

    Explanation: GraphQL's design allows developers to add new fields to the schema without affecting existing queries, so clients can ignore or use new fields as needed. REST endpoints may require updating or versioning if their response format changes, possibly impacting clients. The second statement is incorrect; REST doesn’t merge fields automatically. GraphQL can accommodate schema changes without creating new endpoints, unlike the third option. Both APIs do not require versioning for every field addition, making the last answer incorrect.

  8. Single Endpoint Approach

    Which approach describes how endpoints are typically structured in GraphQL versus REST for mobile APIs?

    1. GraphQL uses a single endpoint for all operations, while REST uses separate endpoints for resources.
    2. REST and GraphQL both merge all endpoints into a master endpoint.
    3. GraphQL requires one endpoint per data type, similar to REST.
    4. Both GraphQL and REST require multiple endpoints for each resource.

    Explanation: GraphQL is usually accessed through a single endpoint where specific data queries are submitted. REST, conversely, defines separate endpoints for each resource or action, resulting in multiple URLs. The second and third options misunderstand the endpoint structure for GraphQL. The last choice is inaccurate because neither approach necessarily uses a 'master endpoint' that merges all others.

  9. Handling Nested Data Requests

    If a mobile app needs information about a user and all their posts in one request, how do REST and GraphQL generally handle this?

    1. REST and GraphQL both prevent fetching nested data in a single call.
    2. GraphQL can fetch nested related data in one query, while REST may require multiple requests.
    3. Both REST and GraphQL require separate calls for each related data item.
    4. REST combines all nested data automatically in one endpoint, while GraphQL cannot.

    Explanation: GraphQL is designed to handle complex and nested queries in a single request, enabling clients to efficiently get related data like user details and their posts. REST might require multiple requests to different endpoints to collect all related information, leading to increased network usage. The second and last options are incorrect, as REST does not combine nested data unless specifically designed to do so. The third answer ignores GraphQL's core advantage: retrieving nested data in one call.

  10. API Response Structure

    What is a typical structural difference in API responses between REST and GraphQL for mobile applications?

    1. REST responses are structured around fixed data models, while GraphQL responses match the fields requested by the client.
    2. REST and GraphQL both require exact fields to be specified in every call.
    3. GraphQL responses follow a fixed format unrelated to the query.
    4. Both REST and GraphQL always return all possible related data.

    Explanation: REST returns pre-defined data structures based on how endpoints are designed, often resulting in responses with all attributes of a resource, whether all are needed or not. GraphQL, in contrast, tailors the response format specifically to the fields requested by the client, minimizing unnecessary data transfer. The second option is incorrect, as only GraphQL can limit output to what is requested. The third is incorrect because GraphQL shapes responses by the query, not a fixed format. The last choice is misleading since REST does not require field-level specification in calls.