GraphQL 与 gRPC
探索 GraphQL 这类支持灵活获取数据的替代 API 风格,以及 gRPC 这类支持高性能通信的方案
GraphQL 与 gRPC 是 CoddyKit 上的免费 System Design Basics for Backend Developers 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 System Design Basics for Backend Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 System Design Basics for Backend Developers 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Beyond REST: GraphQL & gRPC
RESTful APIs are widely used, but they aren't always the perfect fit for every scenario.
Sometimes, you need more control over data fetching or require extremely high performance for inter-service communication.
This lesson explores two powerful alternatives: GraphQL and gRPC.
REST's Data Fetching Hurdles
Traditional REST APIs often lead to two common issues:
- Over-fetching: Receiving more data than needed in a single request, wasting bandwidth.
- Under-fetching: Needing to make multiple requests to different endpoints to gather all required data.
This can be inefficient, especially for mobile clients or complex UIs.
GraphQL: Query Exactly What You Need
GraphQL is a query language for your API, and a server-side runtime for executing those queries.
It empowers clients to request precisely the data they need, eliminating over-fetching and under-fetching.
Think of it as filling out a form for your data request, rather than accepting a pre-defined bundle.
Defining Data with GraphQL Schema
Every GraphQL API is built around a schema. This schema defines all the types of data and operations (queries, mutations) available.
It acts as a contract between the client and the server, ensuring clients know exactly what they can ask for.
Here's a simple example:
type User {
id: ID!
name: String!
email: String
posts: [Post]
}
type Post {
id: ID!
title: String!
content: String
author: User!
}Fetching Data with GraphQL Queries
Clients use GraphQL queries to fetch data. They specify the exact fields they want from the available types defined in the schema.
This means a single query can replace multiple REST requests.
See how we only ask for name, email, and post titles:
query GetUserAndPosts {
user(id: "123") {
name
email
posts {
title
}
}
}Changing Data with GraphQL Mutations
Just as queries fetch data, mutations are used to modify data on the server. This includes creating, updating, or deleting records.
Mutations also return data, often the newly created or updated object, confirming the change.
Here's an example to create a new post:
mutation CreatePost($title: String!, $authorId: ID!) {
createPost(title: $title, authorId: $authorId) {
id
title
}
}gRPC: High-Performance RPC
gRPC (Google Remote Procedure Call) is an open-source framework for high-performance communication between services.
Unlike GraphQL, gRPC is more commonly used for internal microservice communication where speed, efficiency, and strict contracts are paramount.
It's built on HTTP/2 for transport and Protocol Buffers for message serialization.
Protocol Buffers: gRPC's IDL
gRPC uses Protocol Buffers (Protobuf) as its Interface Definition Language (IDL) and its underlying message interchange format.
Protobuf defines the structure of your data and the service interfaces in a language-agnostic way.
This definition is then used to generate client and server code in various languages.
syntax = "proto3";
package greeter;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}gRPC: Binary, HTTP/2, Streaming
Key features of gRPC for high performance:
- HTTP/2: Enables multiplexing (multiple requests over one connection) and bidirectional streaming.
- Binary Serialization: Protobuf serializes data into a compact binary format, which is faster and smaller than JSON.
- Code Generation: Automatically generates client and server stub code from
.protodefinitions, simplifying development.
Choosing Between GraphQL & gRPC
When to use which?
- GraphQL: Ideal for flexible, client-facing APIs where clients need to define their data requirements. Excellent for mobile apps and complex UIs.
- gRPC: Best for high-performance, low-latency inter-service communication (e.g., microservices), or when strict contracts and efficient data transfer are critical.
They solve different problems and can even be used together in a larger system!
Quick Check: API Styles
Consider a scenario where you are building a new mobile application with a complex UI that needs to fetch varied data from a backend with minimal network requests. Which API style would be most suitable for the client-server communication?
Recap: Flexible & Fast APIs
In this lesson, we explored two powerful API alternatives: GraphQL and gRPC.
- GraphQL provides a flexible query language, allowing clients to fetch precisely the data they need, tackling over- and under-fetching issues.
- gRPC offers a high-performance, binary communication framework, ideal for efficient inter-service communication using HTTP/2 and Protocol Buffers.
Understanding their strengths helps you choose the right tool for different system design challenges.
常见问题解答
「GraphQL 与 gRPC」课时是免费的吗?
是的 — 「GraphQL 与 gRPC」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 System Design Basics for Backend Developers 课程的其余内容,请升级到 CoddyKit PRO。 System Design Basics for Backend Developers 课程共包含 4 节课。
「GraphQL 与 gRPC」这节课中我会学到什么?
探索 GraphQL 这类支持灵活获取数据的替代 API 风格,以及 gRPC 这类支持高性能通信的方案 你通过在浏览器中直接运行的动手代码来练习 System Design Basics for Backend Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 System Design Basics for Backend Developers 需要有经验吗?
无需任何先前经验。CoddyKit 上的 System Design Basics for Backend Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「GraphQL 与 gRPC」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 System Design Basics for Backend Developers 课中编写并运行代码吗?
能。每节 System Design Basics for Backend Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- RESTful API 设计原则
- GraphQL 与 gRPC
- 消息队列与事件驱动
- API 版本管理与向后兼容