Leanpub Header

Skip to main content

《GraphQL 学习之道》The Road to GraphQL(简体中文版)

Free With Membership

With Membership

Free!

$9.99

You pay

$9.99

Authors earn

$7.99
$

...Or Buy With Credits!

You can get credits monthly with a Reader Membership
PDF
EPUB
WEB
About

About

About the Book

本书的主旨是 GraphQL 在现代应用中的使用。GraphQL 只是一种规范,虽然它可以运用在许多编程语言中,但本书将主要聚焦于 JavaScript 中的 GraphQL。GraphQL 规范的缔造者 Facebook,发布了用 JavaScript 实现 GraphQL 的样例,而用 JavaScript 的原因是:对于许多公司以及他们的客户端服务器架构来说 JavaScript 是近来最备受瞩目的编程语言和生态系统了。

GraphQL 是一种可以用在任何地方的查询语言,但它通常用于客户端与服务器应用间的通讯。它并不局限于网络层协议的使用,所以数据可以在客户端和服务器应用间读写。它用于补足 JavaScript 中各种库和框架的网络协议栈。客户端可以用像 React、Angular、 Vue 等流行的解决方案,而服务端可以在 Node.js 的环境中使用 Express、Koa、 Hapi 等中间件库。有了这些前后端框架,剩下的就只是通过网路发送带有 GraphQL 指令的 HTTP 请求去链接两端了。

GraphQL 的出现开启了 web 开发的新纪元。RESTful 应用是现在业界在客户端与服务器数据传输上最受欢迎的方式,但是现代的需求已经发生了改变。现在许多应用不得不处理多种客户端,比如桌面应用,web 应用,移动端应用,可穿戴设备应用,他们都需要服务端暴露 API。这就变成了两种选择,一种是用 REST 实现多个针对不同客户端的 API,而另一种则是用 GraphQL 实现一个对所有客户端应用都通用的 API,对于该如何选择就不言而喻了。但 GraphQL 不仅仅只是一个统一的接口,随着开源社区的发展,它的生态系统赋予了它强大的功能甚至更多的可能性。

我恰好有幸与一位 Java GraphQL 开源代码的贡献者共事过。他的努力使他成为了首批 GraphQL 开源代码的贡献者之一,并最终构建了可靠的 Java 实现。这段经历塑造了我自己对 GraphQL 的思考,而后当我的雇主为其企业级应用评估 GraphQL 时,又加深了我对它的思考。最初,我们客户端与服务器之间的粘合主要受 REST 的影响,我们拥有适用于所有 RESTful 资源的 API 端点,但最终遇到了问题,像 Facebook 这样的第三方客户端,无法消费我们的 API 。由于 API 端点过于严格,因此需要对它进行聚合和修改。 聚合意味着将更多的资源放入单个 API 端点,而修改则是为了提供 API 端点的变形,以满足对同一个请求资源的不同表示。我们引入了自己实现的方法去服务器请求资源,最终我们实现了简单的 GraphQL。在客户端方面,当时只有 Relay for React 这一个成熟的库按照消费 GraphQL API 来进行发布的,因此我们也投入了时间去研究它。最后,发现 GraphQL 尚处于发展阶段,因此我们推迟了将其引入我们的技术栈的决定。但不可否认,当时的我们都未料到它会变得如此强大且受欢迎。

因为这本书以 GraphQL 在客户端的应用为开端,所以你将使用 React 作为 UI 框架去消费 GraphQL API,被消费的是 GitHub 的 GraphQL API。它之所以能成为开发者们热衷选择的原因是:GitHub 是向公众发布 GraphQL API 的少数几家流行公司之一;GitHub 的稳定增长也为 GraphQL 本身带来了一定的可信度;还有就是第三方 API 通常会在刚开始的时候聚焦于客户端 GraphQL 应用上。此时本书讲授如何在客户端程序中去消费 GraphQL API 而无需你自己去实现一个 GraphQL 的服务器。

当本书内容转向服务器端的 GraphQL 时,我们将实现最终可由 GraphQL 客户端应用程序消费的 GraphQL API。你将实现如身份验证、数据库连接、分页这样强大的功能。最后,你应该能够牢牢掌握如何在 JavaScript 应用中使用 GraphQL 的知识。

Share this book

Categories

Author

About the Authors

Wang Shiji

Robin Wieruch

Robin Wieruch is a german software engineer and freelancer located in Berlin. In the recent years he has gained experiences in single page applications which use Ember, Angular and React. He is a full-time JavaScript developer, is doing his best to teach students on his blog and strives to learn something new every day himself.

Contents

Table of Contents

简介

  1. 关于作者
  2. 基本要求
  3. FAQ
  4. 如何使用本书
  5. 挑战

GraphQL

  1. 什么是 GraphQL?
  2. GraphQL 的优势
  3. GraphQL 的劣势
  4. 为什么不用 REST
  5. GraphQL 的替代方案

Apollo

  1. Apollo 优势
  2. Apollo 劣势
  3. 针对 JavaScript, React 和 Node.js 的 Apollo 替代选择

GraphQL 准备, 相关工具和 API

  1. 利用 GitHub 数据来提供 API
  2. 使用 GitHub 的 access token 来读写数据
  3. 与 GitHub GraphQL API 交互

GraphQL 基础

  1. GraphQL 基础: 查询
  2. GraphQL 基础:变更
  3. GraphQL 分页

React 和 GraphQL 的结合

  1. 编写你的第一个 React GraphQL 客户端
  2. 在 React 中执行 GraphQL 查询
  3. React 中的 GraphQL 分页
  4. React 中的 GraphQL 变更操作
  5. React 中不借助 Apollo 使用 GraphQL 的缺点

Apollo Client

  1. 从在命令行中使用 Apollo Boost 开始
  2. Apollo 客户端和 GraphQL 查询
  3. 带分页,变量,嵌套对象和列表字段的 Apollo Client
  4. Apollo Client 和 GraphQL 变更

React 结合 GraphQL 与 Apollo 客户端

  1. 编写你的第一个使用 GraphQL 和 Apollo 客户端的 React 应用
  2. 配置使用 React 和 GitHub GraphQL API 的 Apollo 客户端
  3. 绑定数据层与视图层:介绍 React Apollo
  4. 在 React 中 使用 Apollo 客户端进行 GraphQL 查询
  5. Apollo 客户端在 React 中的错误处理
  6. 在 React 中使用 Apollo 客户端变更操作
  7. 使用 React 高阶组件来完成 GraphQL 的查询/变更操作
  8. Apollo 客户端在 React 中使用本地状态管理
  9. React 中的 Apollo 客户端乐观 UI
  10. React 中 Apollo 客户端的 GraphQL 分页
  11. React 中通过 Apollo 客户端实现 GraphQL 缓存查询
  12. 实现 Issues 功能:准备
  13. 实现 Issue 功能: 客户端过滤
  14. 实现 Issues 功能: 服务端过滤
  15. React 中 Apollo 客户端预加载
  16. 练习:评论功能
  17. 附录: CSS 文件与样式

在 GraphQL 和 Apollo 服务端中使用 Node.js

  1. 使用 Express 设置 Apollo 服务端
  2. Apollo 服务端: 类型定义
  3. Apollo 服务端: 解析器
  4. Apollo 服务端: 关系类型
  5. Apollo 服务端: 查询与变更
  6. Apollo 服务端中的 GraphQL 模式拼接
  7. PostgreSQL 与 Sequelize 的 GraphQL 服务器
  8. 连接解析器和数据库
  9. Apollo 服务端:校验和错误处理
  10. Apollo 服务端: 认证
  11. 使用 GraphQL 和 Apollo 服务端进行授权
  12. Apollo 服务端中的 GraphQL 自定义标量
  13. Apollo 服务端中 GrapgQL 的分页
  14. GraphQL 订阅
  15. 测试 GraphQL 服务器
  16. 在 GraphQL 中使用批处理和缓存
  17. GraphQL 服务器 + PostgreSQL 部署到 Heroku

最后的一些想法

  1. 下一步学习路径
  2. 永远不要停止学习
  3. 感谢

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub