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.
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?
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.
What is 'overfetching' in the context of API responses for mobile apps?
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.
How does error handling typically differ between REST and GraphQL APIs when a mobile app receives a bad request?
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.
Which statement accurately describes how versioning is commonly handled in REST and GraphQL for mobile APIs?
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.
For a mobile app requiring frequent updates to the screen with tailored data needs, which API style is usually more efficient and why?
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.
Which HTTP methods are generally used in REST compared to GraphQL for mobile API requests?
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.
How does schema flexibility differ between REST and GraphQL in mobile APIs when adding new fields to data?
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.
Which approach describes how endpoints are typically structured in GraphQL versus REST for mobile APIs?
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.
If a mobile app needs information about a user and all their posts in one request, how do REST and GraphQL generally handle this?
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.
What is a typical structural difference in API responses between REST and GraphQL for mobile applications?
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.