GraphQL 来了,REST 尚能饭否
Posted on Wed 10 August 2022 in Journal
概述
REST 凭借着其简单明了的定义,风靡一时,可是在实际应用过程中,REST 略显呆板,并且在实体对象巨大时性能不佳。 于是 GraphQL 应运而生,客户端只需请求它真正需要的数据,这极大简化和限制了请求的数量.
1) A query language for your API
GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.
GraphQL 是一种用于 API 的查询语言,也是一种使用现有数据完成这些查询的运行引擎。 GraphQL 为您的 API 中的数据提供了完整且易于理解的描述,使客户能够准确地询问他们需要什么,仅此而已,使 API 更容易随着时间的推移而发展,并支持强大的开发人员工具。
2) Ask for what you need, get exactly that
Send a GraphQL query to your API and get exactly what you need, nothing more and nothing less. GraphQL queries always return predictable results. Apps using GraphQL are fast and stable because they control the data they get, not the server.
查询你所需要的, 得到的就是那个
向您的 API 发送 GraphQL 查询并准确获取您需要的内容,不多也不少。 GraphQL 查询总是返回可预测的结果。 使用 GraphQL 的应用程序快速且稳定,因为它们控制的是获取的数据,而不是服务器。
3) Get many resources in a single request
GraphQL queries access not just the properties of one resource but also smoothly follow references between them. While typical REST APIs require loading from multiple URLs, GraphQL APIs get all the data your app needs in a single request. Apps using GraphQL can be quick even on slow mobile network connections.
GraphQL 查询不仅可以访问一种资源的属性,还可以平滑地跟踪它们之间的引用。 虽然典型的 REST API 需要从多个 URL 加载,但 GraphQL API 在单个请求中获取您的应用所需的所有数据。 即使在缓慢的移动网络连接上,使用 GraphQL 的应用程序也可以快速运行。
4) Describe what’s possible with a type system
GraphQL APIs are organized in terms of types and fields, not endpoints. Access the full capabilities of your data from a single endpoint. GraphQL uses types to ensure Apps only ask for what’s possible and provide clear and helpful errors. Apps can use types to avoid writing manual parsing code
GraphQL API 是根据类型和字段组织的,而不是端点。 从单个端点来访问你的数据的所有能力。 GraphQL 使用类型来确保应用程序只询问可能的情况并提供清晰且有用的错误。 应用程序可以使用类型来避免编写手动解析代码
5)Move faster with powerful developer tools
Know exactly what data you can request from your API without leaving your editor, highlight potential issues before sending a query, and take advantage of improved code intelligence. GraphQL makes it easy to build powerful tools like GraphiQL by leveraging your API’s type system.
无需离开编辑器即可准确了解您可以从 API 请求哪些数据,在发送查询之前高亮显示潜在问题,并利用改进的代码智能。 GraphQL 通过利用 API 的类型系统,可以轻松构建强大的工具,例如 GraphiQL。
6) Evolve your API without versions
Add new fields and types to your GraphQL API without impacting existing queries. Aging fields can be deprecated and hidden from tools. By using a single evolving version, GraphQL APIs give apps continuous access to new features and encourage cleaner, more maintainable server code.
在不影响现有查询的情况下向 GraphQL API 添加新字段和类型。 老化字段可以被工具弃用和隐藏。 通过使用一个不断发展的版本,GraphQL API 让应用程序可以持续访问新功能,并鼓励更清洁、更可维护的服务器代码。
7) Bring your own data and code
GraphQL creates a uniform API across your entire application without being limited by a specific storage engine. Write GraphQL APIs that leverage your existing data and code with GraphQL engines available in many languages. You provide functions for each field in the type system, and GraphQL calls them with optimal concurrency.
GraphQL 在整个应用程序中创建统一的 API,不受特定存储引擎的限制。 编写 GraphQL API,可利用现有的数据和代码, 以及多种语言的 GraphQL 引擎来编写GraphQL API。 你只需为类型系统中的每个字段提供函数,GraphQL 以最佳并发性调用它们。
- C++ 代码库
- 这里有篇基于 python 的教程写得不错,可以照着输入代码试一试