常见问题
新手入门

新手入门

¥Getting Started

我为什么要使用 GraphQL?

¥Why should I use GraphQL?

这取决于你的用例,但一般来说,GraphQL 有一些突出的关键功能。例如,GraphQL 使你能够:

¥It depends on your use case, but in general, GraphQL has a few key features that stand out. For example, GraphQL enables you to:

我们的主页 概述了使用 GraphQL 的更多理由。

¥Our homepage outlines even more reasons to use GraphQL.

你可以尝试 GraphQL,而无需重写整个应用。例如,从封装现有 REST 调用的单个 HTTP 请求开始。你的 GraphQL 结构业务字段模型 可以逐渐扩展。我们建议首先关注一个用例,并仅构建所需的结构部分。

¥You can try out GraphQL without rewriting your entire application. For instance, starting with a single HTTP request that wraps an existing REST call. Your GraphQL schema and business domain model can expand gradually. We recommend focusing on one use case at first and only building the part of the schema needed for that.

GraphQL 会取代 REST 吗?

¥Does GraphQL replace REST?

不,不一定。它们都处理 API,并且可以从业务角度 服务于类似目的。GraphQL 通常被认为是 REST 的替代品,但并不是绝对的替代品。

¥No, not necessarily. They both handle APIs and can serve similar purposes from a business perspective. GraphQL is often considered an alternative to REST, but it’s not a definitive replacement.

GraphQL 和 REST 实际上可以在你的堆栈中共存。例如,你可以在 GraphQL 服务器 后面抽象 REST API。这可以通过使用 根解析器 将 REST 端点屏蔽为 GraphQL 端点来实现。

¥GraphQL and REST can actually co-exist in your stack. For example, you can abstract REST APIs behind a GraphQL server. This can be done by masking your REST endpoint into a GraphQL endpoint using root resolvers.

有关 GraphQL 与 REST 的比较,请查看 如何使用 GraphQL

¥For an opinionated perspective on how GraphQL compares to REST, check out How To GraphQL.

GraphQL 是一种像 SQL 一样的数据库语言吗?

¥Is GraphQL a database language like SQL?

不,但这是一个常见的误解。

¥No, but this is a common misconception.

GraphQL 是一种通常用于远程客户端-服务器通信的规范。与 SQL 不同,GraphQL 与用于检索数据和保存更改的数据源无关。访问和操作数据是通过名为 resolvers 的任意函数执行的。GraphQL 协调并聚合来自这些解析器函数的数据,然后将结果返回给客户端。通常,这些解析器功能应委托给负责与各种底层数据源通信的 业务逻辑层。这些数据源可以是远程 API、数据库、本地缓存 以及你的编程语言可以访问的几乎任何其他东西。

¥GraphQL is a specification typically used for remote client-server communications. Unlike SQL, GraphQL is agnostic to the data source(s) used to retrieve data and persist changes. Accessing and manipulating data is performed with arbitrary functions called resolvers. GraphQL coordinates and aggregates the data from these resolver functions, then returns the result to the client. Generally, these resolver functions should delegate to a business logic layer responsible for communicating with the various underlying data sources. These data sources could be remote APIs, databases, local cache, and nearly anything else your programming language can access.

有关如何让 GraphQL 与数据库交互的更多信息,请查看我们的 解析器文档

¥For more information on how to get GraphQL to interact with your database, check out our documentation on resolvers.

我如何学习 GraphQL?

¥How can I learn GraphQL?

有许多资源可帮助你学习 GraphQL,包括此网站。在 我们的文档 中,你将找到一系列文章,解释基本 GraphQL 概念及其工作原理。我们的 社区页面 充满了可供参考的资源和可供加入的群组。

¥There are many resources available to help you learn GraphQL, including this website. In our documentation, you’ll find a series of articles that explain essential GraphQL concepts and how they work. Our Community page is full of resources to reference and groups to join.

有关更多实用指南,请访问 如何使用 GraphQL 全栈教程网站。我们还与 edX 合作开设了一门免费在线课程 探索 GraphQL:API 的查询语言

¥For more practical guides, visit the How to GraphQL fullstack tutorial website. We also have a free online course with edX, Exploring GraphQL: A Query Language for APIs.

在开始学习之旅之前,请确保你了解 什么是 API 以及客户端和服务器之间的通信通常是如何进行的。

¥Before you start your learning journey, make sure you know what an API is and how communication generally works between client and server.

GraphQL 仅适用于 React 或 JavaScript 开发者吗?

¥Is GraphQL only for React or JavaScript developers?

不,一点也不。GraphQL 是一种规范 可以是 以任何语言实现。我们的 代码页 包含许多不同编程语言的库列表,可帮助你实现这一点。

¥No, not at all. GraphQL is a specification that can be implemented in any language. Our Code page contains a long list of libraries in many different programming languages to help with that.

不过,你可以理解为什么你会这么想。GraphQL 是在 React 会议 上引入的,而 GraphQL.js 是迄今为止使用最广泛的实现之一。我们知道这可能会造成混淆,因此我们正在努力改进我们的文档并添加更多未使用 JavaScript 编写的代码示例。

¥It’s understandable why you’d think this, though. GraphQL was introduced at a React conference and GraphQL.js is one of the most widely used implementations to date. We know this can be confusing, so we’re working to improve our documentation and add more code samples that aren’t written in JavaScript.