How to answer
REST exposes many resource endpoints (GET /users/42, GET /users/42/posts) whose response shapes the server decides, and it leans on HTTP itself: verbs, status codes and HTTP caching. GraphQL exposes one endpoint with a typed schema, and the client sends a query describing exactly the fields it needs, often across several related objects in one round trip, which avoids over-fetching and under-fetching. The trade-off is that GraphQL moves complexity elsewhere: HTTP caching mostly doesn't apply (queries are usually POSTs), errors come back as 200 with an errors array, and you typically want code generation and a normalized client cache. For an iOS team, I'd pick REST for simple CRUD or public, cacheable APIs, and GraphQL when many screens need different slices of a rich, connected data model.
Key points
- REST: multiple endpoints, server-defined shapes, HTTP caching and status codes, easy to debug with any tool
- GraphQL: single endpoint, client-defined shapes, strongly typed schema, introspection, subscriptions for real time
- Over-fetching (too many fields) and under-fetching (several round trips) are the REST pain points GraphQL targets
- GraphQL errors: partial data plus
errors, usually with HTTP 200 - Caching: REST uses
URLCacheandETags; GraphQL clients (e.g. Apollo iOS) keep a normalized cache keyed by object ID - Code generation: Apollo iOS generates Swift types from
.graphqlfiles; for REST, swift-openapi-generator does the same from OpenAPI
If they dig deeper
Persisted queries (send a hash, allow GET and CDN caching). Server costs: N+1 resolvers, query complexity limits. Versioning: REST versions endpoints, GraphQL deprecates fields. Pagination: REST offsets/cursors vs GraphQL connections (edges, pageInfo). gRPC as a third option for service-to-service and streaming. A BFF (backend for frontend) as a REST-side answer to over-fetching.
Common pitfalls
Choosing GraphQL for a handful of simple endpoints. Treating HTTP 200 as success in GraphQL and ignoring errors. Expecting URLCache to cache POSTed queries. Hand-writing GraphQL response models that drift from the schema. Fetching huge nested graphs because "the client can ask for anything".
How to use it
The same data, two ways. REST needs two requests; GraphQL asks for exactly these fields in one:
query UserWithPosts($id: ID!) {
user(id: $id) {
name
posts(first: 3) { title }
}
}A minimal GraphQL call with URLSession, without a client library:
import Foundation
struct GraphQLRequest<Variables: Encodable & Sendable>: Encodable, Sendable {
let query: String
let variables: Variables
}
struct GraphQLResponse<Payload: Decodable & Sendable>: Decodable, Sendable {
struct GQLError: Decodable, Sendable { let message: String }
let data: Payload?
let errors: [GQLError]?
}
func graphQL<V: Encodable & Sendable, P: Decodable & Sendable>(
_ query: String, variables: V, endpoint: URL, session: URLSession
) async throws -> P {
var request = URLRequest(url: endpoint)
request.httpMethod = "POST" // one endpoint, always POST here
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try JSONEncoder().encode(GraphQLRequest(query: query, variables: variables))
let (data, http) = try await session.data(for: request)
guard (http as? HTTPURLResponse)?.statusCode == 200 else { throw URLError(.badServerResponse) }
let response = try JSONDecoder().decode(GraphQLResponse<P>.self, from: data)
if let errors = response.errors, !errors.isEmpty { // 200 can still carry errors
throw URLError(.cannotParseResponse, userInfo: [NSLocalizedDescriptionKey: errors[0].message])
}
guard let payload = response.data else { throw URLError(.zeroByteResource) }
return payload
}Best practices
- Choose by data shape and team: REST for simple or cacheable resources, GraphQL for rich, connected models with many client views
- Generate types from the schema or spec instead of hand-writing models
- Handle GraphQL
errorsand partial data explicitly - Plan caching up front (HTTP caching for REST, a normalized cache for GraphQL)
- Limit query depth and cost on the server
Related concepts
- REST API client design, GraphQL client, gRPC on iOS, response caching, pagination, Codable