# 您是否优化了 API 的性能

API 是应用程序通过所谓的端点向内部和第三方应用程序公开数据的通信通道。在设计和构建 API 时，我们需要一个合理的协议来启用服务器和第三方请求之间的通信。 Representational State Transfer（REST）是一个成熟的逻辑选择：它定义了开发人员遵循的一组约束，以便以高性能，可靠和可扩展的方式访问内容。符合 REST 约束的 Web 服务称为 RESTful Web 服务。

与良好的 HTTP 请求一样，当从 API 检索数据时，服务器响应中的任何延迟都将传播到最终用户，从而延迟呈现。当资源想要从 API 检索某些数据时，它将需要从相应的端点请求数据。从多个资源中呈现数据的组件（例如，在每个注释中包含注释和作者照片的文章）可能需要多次往返服务器以在呈现之前获取所有数据。此外，通过 REST 返回的数据量通常大于呈现该组件所需的数据量。

如果许多资源需要来自 API 的数据，API 可能会成为性能瓶颈。 GraphQL 为这些问题提供了高性能的解决方案。本身，GraphQL 是 API 的查询语言，也是服务器端运行时，它使用您为数据定义的类型系统来执行查询。与 REST 不同，GraphQL 可以在单个请求中检索所有数据，并且响应将完全符合要求，而不会像 REST 通常那样过度或欠读数据。

此外，由于 GraphQL 使用模式（描述数据结构的元数据），它已经可以将数据组织到首选结构中，因此，例如，使用 GraphQL，我们可以删除用于处理状态管理的 JavaScript 代码，生成更简洁的应用程序代码，在客户端上运行得更快。

如果你想开始使用 GraphQL，Eric Baer 发表了两篇关于你真正 Smashing 杂志的精彩文章：GraphQL 入门：为什么我们需要一种新的 API 和一个 GraphQL 入门：API 设计的演变（感谢提示，Leonardo ！）。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://shenjunhong.gitbook.io/blog/yi-wen/yi-nin-shi-fou-you-hua-le-api-de-xing-neng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
