GraphQL and gRPC

Other ways to talk to an API · Part of Architecture

Key idea

Practice

8 questions · about 4 min

Explained step by step

GraphQL

GraphQL is a query language and a server-side runtime for APIs that gives the client exactly the data it asks for and nothing more. Facebook created it and open-sourced it in 2015. In GraphQL, the basic unit is the query. Also, fields can be added or deprecated without breaking existing queries. It's like a buffet where you take only what you want, instead of a fixed 5-course menu.

GraphQL eliminates over-fetching, has a strongly defined schema, generates code and optimizes what travels. But it shifts complexity to the server, caching gets hard (each query is different), versioning is unclear and it suffers the N+1 problem: fetching 1 list and then 1 query per item. It's good for cutting an app's bandwidth, rapid prototyping and graph-shaped data. It's like a buffet: you eat exactly what you want, but the kitchen works harder.

Resolver

GraphQL has 3 key concepts. The schema describes everything the client can ask for when it connects. The query is the client's request, with fields and arguments; if it changes data, it's called a mutation. The GraphQL resolver is the set of functions that builds each field's answer: it's what handles the query. It's like a restaurant: the menu is the schema, your order is the query and the cook is the resolver.

gRPC

gRPC is a modern, open-source, high-performance framework for remote procedure calls (RPC): calling a function running on another machine as if it were local. It connects services within and across data centers, with support for load balancing, tracing, health checks and authentication. It's like phoning a colleague to do a sum: you say «add 2 and 3» and they answer «5», without you seeing how.

gRPC is lightweight, very fast, generates code and allows bidirectional streaming: client and server send data at the same time over the same connection. But it's newer than REST and GraphQL, browsers barely support it, it's harder to learn and people can't read its messages. It fits real time, communication between microservices, low latency and high volume, and teams with many languages. It's like a freight train: super fast between stations, but it doesn't drop you at your door.

Protocol buffers

gRPC uses protocol buffers to describe services and messages. It's a language-neutral way to serialize data (turn it into bytes to send it), like JSON but smaller and faster, and it generates code for each language. A .proto file defines the service, its functions and the shape of each message. It's like sending a form with numbered boxes instead of a handwritten letter: it takes less space and reads faster.

REST

Comparing: REST has low coupling, great caching and easy versioning, but needs many calls. GraphQL needs few calls and generates code, but is more complex and caching is custom. gRPC has the best performance and generates code, but couples more, is hard to discover and hard to version. None is the best: it depends on the case. It's like choosing between car, bike and train: each wins on a different trip.

API

Which API technology is best? None. There's no silver bullet: each has advantages and disadvantages. Users only care about using your API consistently, so focus on the business and the requirements when designing it. Many systems use 2 or 3 at once. It's like the tools in a toolbox: the hammer isn't better than the screwdriver, it depends on the screw.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

Other ways to talk to an API