|
Post by account_disabled on Jan 29, 2024 23:11:19 GMT -5
GraphQL 是一种 API 查询语言。尽管它与 REST 有着根本的不同,但 GraphQL 可以作为 REST 的替代品,提供性能、出色的开发人员体验和非常强大的工具。 在本文中,我们将了解如何使用 REST 和 GraphQL 处理一些常见用例。本文包含三个项目。您将找到 REST 和 GraphQL API 的代码,这些 API 提供有关流行电影和演员的信息,以及使用 HTML 和 jQuery 构建的简单前端应用程序。 我们将使用这些 API 来研究这些技术的不同之处,以便我们可以确定它们的优点和缺点。不过,首先让我们快速了解一下这些技术是如何诞生的。 网络的早期 网络的早期很简单。Web 应用程序始于早期互联网上提供的静态 HTML 文档。网站经过改进,包含存储在数据库(例如 SQL)中的动态内容,并使用 JavaScript 来添加交互性。绝大多数网络内容是通过台式计算机上的网络浏览器查看的,一切都很好。 正常图像 REST:API 的兴起 快进到 2007 年,史蒂夫·乔布斯 (Steve Jobs) 推出了 iPhone。智能手机除了对世界、文化和通信产生深远影响外,还使开发人员的生活变得更加复杂。智能手机打破了发展现状。在短短几年内,我们突然有了台式机、iPhone、Android 和平板电 WhatsApp 号码数据 脑。 作为回应,开发人员开始使用RESTful API为各种形状和大小的应用程序提供数据。新的开发模式看起来像这样: 休息服务器 GraphQL:API 的演变 GraphQL 是一种API 查询语言,由 Facebook 设计并开源。您可以将 GraphQL 视为构建 API 的 REST 的替代方案。REST 是一种可用于设计和实现 API 的概念模型,而 GraphQL 是一种标准化语言、类型系统和规范,可在客户端和服务器之间创建强大的契约。拥有一种可供所有设备进行通信的标准语言可以简化创建大型跨平台应用程序的过程。 使用 GraphQL,我们的图表得以简化: GraphQL 服务器 在本教程的其余部分(没有双关语),我鼓励您遵循代码!您可以在随附的 GitHub 存储库中找到本文的代码。 该代码包括三个项目 构建的简单客户端网页。 这些项目故意简单化,旨在提供这些技术之间尽可能简单的比较。 如果您想继续操作,请打开三个终端窗口以及项目存储库中c目录。Client从每个目录中,通过 运行开发服务器npm run dev。一旦服务器准备就绪,请继续阅读 使用 REST 查询 我们的 RESTful API 包含一些端点: 端点 描述 电影 返回包含电影链接的对象数组(例如 演员 返回一个对象数组,其中包含 的电影中演员的链接演员 返回包含演员链接的对象数组 的单个演我们的简单数据模型已经有 6 个端点,我们需要维护和记录它们。 让我们想象一下,我们是客户端开发人员,需要使用我们的电影 API 通过 HTML 和 jQuery 构建一个简单的网页。
|
|