NiQin (blog: 泥芹) shared the aphorism --
我又转念,见日光之下,快跑的未必能赢,力战的未必得胜,智慧的未必得粮食,明哲的未必得资财,灵巧的未必得喜悦。所临到众人的,是在乎当时的机会。 -- <the Bible>

[GraphQL] 基于 actix-web + async-graphql + rbatis + postgresql / mysql 构建异步 Rust GraphQL 服务 - 起步及 crate 选择

💥 内容涉及著作权,均归属作者本人。若非作者注明,默认欢迎转载:请注明出处,及相关链接。

Summary: 本系列博客中,我们基于 actix-web + async-graphql + rbatis + postgresql / mysql 构建异步 Rust GraphQL 服务。 本文为起步教程,主要包括: 工程的创建、工具类 crate 安装、添加依赖 crate,以及依赖项支持特性(features)的设定。

Topics: rust graphql mysql actix-web rbatis postgresql

前段时间,笔者写了一个构建 Rust 异步 GraphQL 服务的系列博文,构建 Rust 异步 GraphQL 服务:基于 tide + async-graphql + mongodb,采用的 Rust web 框架是 Tide

感兴趣的朋友阅读以后,对 actix-web 更感兴趣。有几十位朋友建议笔者写个 actix-web + async-graphql 构建 GraphQL 服务的系列。看样子 Rust 的国内社区,虽然使用 Rust 的公司可能很少,但至少感兴趣的程序员基本面不小了。

因此,本系列文章,笔者以 actix-web + async-graphql + rbatis + postgresql / mysql 技术栈为骨架,简单进行 GraphQL 服务构建的实践。actix-web 是极为优秀的 Rust web 框架,笔者在 2018-2019 年间,GraphQL 服务后端,也一直使用的是 actix-web + juniper + postgresql 的组合。

同时,目前国内工作场景,还是 mysql 居多,所以本系列实践,我们采用 mysql 数据库。但这次实践采用了 orm 框架 rbatis,所以对于 postgresql 的支持,会很方便。在系列文章最后,我们增加很少量的代码,即可支持 postgresql。并且,我们将一并实现 GraphQL 服务的多数据源支持。

tide + async-graphql + mongodb 系列类似,我们需要做到前后端分离。

  1. 后端:主要提供 GraphQL 服务,使用到的 crate 包括:actix-webasync-graphql、jsonwebtoken、rbatis、serde、ring、base64 等。
  2. 前端(handlebars-rust):主要提供 WEB 应用服务,使用到 crate 包括:actix-webrhai、surf、graphql_client、handlebars-rust、cookie 等。
  3. 前端(WebAssembly ):主要提供 WEB 应用服务,笔者实际项目中,和伙伴们还处于尝试初期。后面写到这部分时,我们再确定使用的技术栈。如果您对于 wasm 的使用已经熟悉,欢迎您的指导。

Rust 环境的配置,cargo 工具的使用,以及 Rust 程序设计语言和 GraphQL 的介绍和优势,在此不再赘述。您可以参阅如下资料学习 Rust 程序设计语言,以及 Rust 生态中的 GraphQL 实现。

以下建议了解,技术选型很丰富,我们不必拘泥。

  • Tide,Rust 官方团队开发的 HTTP 服务器框架。推荐作为了解,本系列文章中我们选择 actix-web。
  • Juniper 中文文档,推荐作为了解,本系列文章中我们选择 async-graphql。

其它概念性、对比类的内容,请您自行搜索。

工程的创建

文章的开始提到,我们要做到前后端分离。因此,前、后端需要各自创建一个工程。同时,我们要使用 cargo 对工程进行管理和组织。

  • 首先,创新本次工程根目录和 cargo 清单文件
mkdir actix-web-async-graphql
cd ./actix-web-async-graphql

touch Cargo.toml 

Cargo.toml 文件中,填入以下内容:

[workspace]
members = [
  "./backend",
  "./frontend-handlebars",
  "./frontend-wasm",
]

resolver = "2"

[profile.dev]
split-debuginfo = "unpacked"

注1resolver = "2" 是 Rust 1.51.0 中 cargo 工具新支持的设定项,主要用于解决依赖项管理的难题。目前,Cargo 的默认行为是:在依赖关系图中,当单个包被多次引用时,合并该包的特性。而启用 resolver 后,则可避免合并包,启用所有特性。但这种新的解析特性,可能会导致一些 crate 编译不止一次。具体参阅 Rust 1.51.0 已正式发布,及其新特性详述,或者 Rust 2021 版本特性预览,以及工作计划 中的 Cargo resolver 小节。

注2[profile.dev] 是 Rust 1.51.0 中的关于“拆分调试信息”的设定,这个主要是 macOS 上的改进。

文件中,workspace 是 cargo 中的工作区。cargo 中,工作区共享公共依赖项解析(即具有共享 Cargo.lock),输出目录和各种设置,如配置文件等的一个或多个包的集合。

虚拟工作区是 Cargo.toml 清单中,根目录的工作空间,不需要定义包,只列出工作区成员即可。

上述配置中,包含 3 个成员 backendfrontend-handlebars,以及 frontend-wasm 即我们需要创建 3 个工程(请注意您处于 actix-web-async-graphql 目录中):前端和后端 —— 均为二进制程序,所以传递 --bin 参数,或省略参数。

cargo new backend --bin
cargo new frontend-handlebars --bin
cargo new frontend-wasm --bin

注3:如果你不想要产生 git 信息,或者你有自己的 git 配置,请在 cargo 命令后再加上 --vcs none 参数。

创建后,工程结构如下图所示——

工程结构

我们可以看到,因为还未编译,没有 Cargo.lock 文件;main.rs 文件也是 Cargo 产生的默认代码。

现在,这个全新的工程,已经创建完成了。

工具类 crate 安装

工程创建完成后,我们即可以进入开发环节了。开发中,一些工具类 crate 可以起到“善其事”的作用,我们需要先进行安装。

  • cargo-edit,包含 cargo addcargo rm,以及 cargo upgrade,可以让我们方便地管理 crate。
  • cargo-watch,监视项目的源代码,以了解其更改,并在源代码发生更改时,运行 Cargo 命令。

好的,我们安装这 2 个 crate。

cargo install cargo-edit
cargo install cargo-watch

安装依赖较多,如果时间较长,请配置 Rust 工具链的国内源

添加依赖 crate

接着,我们需要添加开发所需依赖项。依赖项的添加,我们不用一次性全部添加,我们根据开发需要,一步步添加。首先,从后端工程开始。

后端工程中,我们提供 GraphQL 服务,需要依赖的基本 crate 有 actix-web、async-graphql、rbatis。我们使用 cargo add 命令来安装,其将安装最新版本。

cd backend
cargo add actix-web async-graphql rbatis

安装依赖较多,如果时间较长,请配置 Cargo 国内镜像源

执行上述命令后,actix-web-async-graphql/backend/Cargo.toml 内容如下所示——

...
[dependencies]
actix-web = "3.3.2"

async-graphql = "2.8.2"
rbatis = "1.8.83"
...

Rust 生态和社区中,发展是非常迅猛的。虽然 Rust 的稳定性、安全性非常高,但活跃的社区导致 crate 的迭代版本很快。所以我们使用的都是最新版本的 crate,跟上 Rust 生态的最新潮流。

依赖项支持特性(features)

本文开始,我们已经提到:本系列,我们将以 mysql、postgresql 作为数据库进行实践。rbatis 默认为特性为 all-database,支持包括 sqlite、sqlserver 等,我们不需要,所以限定特性为 mysql、postgresql 即可。

另外,async-graphql 从 2.6.3 开始,默认不激活所有特性,所以我们本次实践,也需要做一些设定。

最终,actix-web-async-graphql/backend/Cargo.toml 内容如下所示——

...
[dependencies]
[dependencies]
actix-web = "3.3.2"

async-graphql = { version = "2.8.2", features = ["chrono"] }
rbatis = { version = "1.8.83", default-features = false, features = ["mysql", "postgres"] }
...

至此,我们构建基于 Rust 技术栈的 Graphql 服务的后端基础工程已经搭建完成。下一篇文章中,我们开始构建一个最基本的 GraphQL 服务器。

实例源码仓库在 github,欢迎您共同完善。

如果您发现任何错误/错别字,请给予指导(微信号 yupen-com,或者页底邮箱)。

谢谢您的阅读。


Related Articles

  1. [Rust] RustHub.org:基于 Rust-Web 技术栈,及 image-rs、fluent-rs、rhai-script ……
  2. [WebAssembly] yew SSR 服务器端渲染
  3. [Rust] async-std 创建者对于最近“项目是否已死?”,移除对其支持等的答复
  4. [Rust] Rust 1.56.0 版本和 Rust 2021 版次发布,新特性一览,及项目的迁移、升级
  5. [WebAssembly] Rust 和 Wasm 的融合,使用 yew 构建 WebAssembly 博客应用的体验报告
  6. [Rust] Rust 官方周报 399 期(2021-07-14)
  7. [WebAssembly] Rust 和 Wasm 的融合,使用 yew 构建 web 前端(5)- 构建 HTTP 请求、与外部服务器通信的两种方法
  8. [Rust] Rust 官方周报 398 期(2021-07-07)
  9. [Rust] Rust 官方周报 397 期(2021-06-30)
  10. [Rust] Rust 官方周报 396 期(2021-06-23)
  11. [Rust] Rust 官方周报 395 期(2021-06-16)
  12. [Rust] Rust 1.53.0 明日发布,关键新特性一瞥
  13. [Rust] 使用 tide、handlebars、rhai、graphql 开发 Rust web 前端(3)- rhai 脚本、静态/资源文件、环境变量等
  14. [Rust] 使用 tide、handlebars、rhai、graphql 开发 Rust web 前端(2)- 获取并解析 GraphQL 数据
  15. [Rust] 使用 tide、handlebars、rhai、graphql 开发 Rust web 前端(1)- crate 选择及环境搭建
  16. [Rust] Rust 官方周报 394 期(2021-06-09)
  17. [Rust] Rust web 前端库/框架评测,以及和 js 前端库/框架的比较
  18. [WebAssembly] Rust 和 Wasm 的融合,使用 yew 构建 web 前端(4)- 获取 GraphQL 数据并解析
  19. [WebAssembly] Rust 和 Wasm 的融合,使用 yew 构建 web 前端(3)- 资源文件及小重构
  20. [WebAssembly] Rust 和 Wasm 的融合,使用 yew 构建 WebAssembly 标准的 web 前端(2)- 组件和路由
  21. [WebAssembly] Rust 和 Wasm 的融合,使用 yew 构建 WebAssembly 标准的 web 前端(1)- 起步及 crate 选择
  22. [Rust] Rust 官方周报 393 期(2021-06-02)
  23. [Rust] Rust 官方周报 392 期(2021-05-26)
  24. [Rust] Rust 中,对网址进行异步快照,并添加水印效果的实践
  25. [Rust] Rust 官方周报 391 期(2021-05-19)
  26. [Rust] Rust,风雨六载,砥砺奋进
  27. [Rust] 为什么我们应当将 Rust 用于嵌入式开发?
  28. [Rust] Rust 官方周报 390 期(2021-05-12)
  29. [Rust] Rust + Android 的集成开发设计
  30. [Rust] Rust 1.52.1 已正式发布,及其新特性详述
  31. [Rust] 让我们用 Rust 重写那些伟大的软件吧
  32. [Rust] Rust 1.52.0 已正式发布,及其新特性详述
  33. [Rust] Rust 官方周报 389 期(2021-05-05)
  34. [GraphQL] 基于 actix-web + async-graphql + rbatis + postgresql / mysql 构建异步 Rust GraphQL 服务(4) - 变更服务,以及小重构
  35. [Rust] Rust 1.52.0 稳定版预发布测试中,关键新特性一瞥
  36. [Rust] Rust 生态中,最不知名的贡献者和轶事
  37. [Rust] Rust 基金会迎来新的白金会员:Facebook
  38. [Rust] Rustup 1.24.1 已官宣发布,及其新特性详述
  39. [Rust] Rust 官方周报 388 期(2021-04-28)
  40. [Rust] Rust 官方周报 387 期(2021-04-21)
  41. [GraphQL] 构建 Rust 异步 GraphQL 服务:基于 tide + async-graphql + mongodb(4)- 变更服务,以及第二次重构
  42. [Rust] Rustup 1.24.0 已官宣发布,及其新特性详述
  43. [Rust] basedrop:Rust 生态中,适用于实时音频的垃圾收集器
  44. [Rust] Rust 编译器团队对成员 Aaron Hill 的祝贺
  45. [Rust] Jacob Hoffman-Andrews 加入 Rustdoc 团队
  46. [机器人] 为什么应将 Rust 引入机器人平台?以及机器人平台的 Rust 资源推荐
  47. [Rust] rust-lang.org、crates.io,以及 docs.rs 的管理,已由 Mozilla 转移到 Rust 基金会
  48. [Rust] Rust 官方周报 386 期(2021-04-14)
  49. [Rust] Rust 编译器(Compiler)团队 4 月份计划 - Rust Compiler April Steering Cycle
  50. [GraphQL] 基于 actix-web + async-graphql + rbatis + postgresql / mysql 构建异步 Rust GraphQL 服务(3) - 重构
  51. [Rust] 头脑风暴进行中:Async Rust 的未来熠熠生辉
  52. [GraphQL] 基于 actix-web + async-graphql + rbatis + postgresql / mysql 构建异步 Rust GraphQL 服务(2) - 查询服务
  53. [GraphQL] 基于 actix-web + async-graphql + rbatis + postgresql / mysql 构建异步 Rust GraphQL 服务 - 起步及 crate 选择
  54. [Rust] Rust 2021 版本特性预览,以及工作计划
  55. [Rust] Rust 用在生产环境的 42 家公司
  56. [Rust] 构建最精简的 Rust Docker 镜像
  57. [Rust] Rust 官方周报 385 期(2021-04-07)
  58. [Rust] 使用 Rust 做异步数据采集的实践
  59. [Rust] Android 支持 Rust 编程语言,以避免内存缺陷
  60. [Rust] Android 平台基础支持转向 Rust
  61. [Rust] Android 团队宣布 Android 开源项目(AOSP),已支持 Rust 语言来开发 Android 系统本身
  62. [Rust] RustyHermit——基于 Rust 实现的下一代容器 Unikernel
  63. [Rust] Rustic:完善的纯粹 Rust 技术栈实现的国际象棋引擎,多平台支持(甚至包括嵌入式设备树莓派 Raspberry Pi、Buster)
  64. [Rust] Rust 迭代器(Iterator trait )的要诀和技巧
  65. [Rust] 使用 Rust 极致提升 Python 性能:图表和绘图提升 24 倍,数据计算提升 10 倍
  66. [Rust] 【2021-04-03】Rust 核心团队人员变动
  67. [Rust] Rust web 框架现状【2021 年 1 季度】
  68. [Rust] Rust 官方周报 384 期(2021-03-31)
  69. [Rust] Rust 中的解析器组合因子(parser combinators)
  70. [生活] 毕马威(KPMG)调查报告:人工智能的实际采用,在新冠疫情(COVID-19)期间大幅提升
  71. [Python] HPy - 为 Python 扩展提供更优秀的 C API
  72. [Rust] 2021 年,学习 Rust 的网络资源推荐(2)
  73. [Rust] 2021 年,学习 Rust 的网络资源推荐
  74. [生活] 况属高风晚,山山黄叶飞——彭州葛仙山露营随笔
  75. [Rust] Rust 1.51.0 已正式发布,及其新特性详述
  76. [Rust] 为 Async Rust 构建共享的愿景文档—— Rust 社区的讲“故事”,可获奖
  77. [Rust] Rust 纪元第 382 周最佳 crate:ibig 的实践,以及和 num crate 的比较
  78. [Rust] Rust 1.51.0 稳定版本改进介绍
  79. [Rust] Rust 中将 markdown 渲染为 html
  80. [生活] 国民应用 App 的用户隐私数据窥探
  81. [GraphQL] 构建 Rust 异步 GraphQL 服务:基于 tide + async-graphql + mongodb(3)- 重构
  82. [GraphQL] 构建 Rust 异步 GraphQL 服务:基于 tide + async-graphql + mongodb(2)- 查询服务
  83. [GraphQL] 构建 Rust 异步 GraphQL 服务:基于 tide + async-graphql + mongodb(1)- 起步及 crate 选择
  84. [Rust] Rust 操控大疆可编程 tello 无人机

Topics

rust(84)

graphql(17)

rust-官方周报(17)

webassembly(16)

wasm(10)

tide(9)

async-graphql(9)

yew(9)

rust-web(8)

rust-官方博客(8)

this-week-in-rust(6)

mysql(5)

actix-web(5)

rbatis(5)

android(4)

mongodb(3)

json-web-token(3)

jwt(3)

cargo(3)

技术延伸(3)

rust-wasm(3)

trunk(3)

handlebars(3)

rhai(3)

async-std(3)

用户隐私(2)

学习资料(2)

python(2)

ai(2)

人工智能(2)

postgresql(2)

rust-compiler(2)

rust-基金会(2)

rust-foundation(2)

rustup(2)

rust-toolchain(2)

rust-工具链(2)

rust-游戏开发(2)

rust-区块链(2)

rust-2021(2)

graphql-client(2)

surf(2)

rust-game(2)

rusthub(2)

tello(1)

drone(1)

无人机(1)

隐私数据(1)

markdown(1)

html(1)

crate(1)

async(1)

异步(1)

旅游(1)

不忘生活(1)

葛仙山(1)

hpy(1)

python-扩展(1)

正则表达式(1)

解析器组合因子(1)

组合器(1)

regular-expression(1)

parser-combinator(1)

regex(1)

官方更新(1)

rust-工作招聘(1)

rust-技术资料(1)

rust-周最佳-crate(1)

rust-web-框架(1)

rust-web-framework(1)

rust-核心团队(1)

rust-core-team(1)

rust-language-team(1)

pyo3(1)

rust-python-集成(1)

python-性能改进(1)

迭代器(1)

iterator-trait(1)

国际象棋(1)

chess(1)

游戏引擎(1)

game-engine(1)

虚拟化(1)

unikernel(1)

rustyhermit(1)

linux(1)

virtualization(1)

sandboxing(1)

沙箱技术(1)

数据采集(1)

异步数据采集(1)

docker(1)

镜像(1)

生产环境(1)

rust-评价(1)

rust-2021-edition(1)

rust-2021-版本(1)

graphql-查询(1)

vision-doc(1)

愿景文档(1)

代码重构(1)

steering-cycle(1)

方向周期(1)

隐私声明(1)

机器人(1)

robotics(1)

rustdoc(1)

rust-编译器(1)

实时音频(1)

real-time-audio(1)

变更服务(1)

mutation(1)

查询服务(1)

query(1)

rust-贡献者(1)

rust-轶事(1)

rust-稳定版(1)

rust-预发布(1)

rust-测试(1)

安全编程(1)

可信计算(1)

安全代码(1)

secure-code(1)

rust-android-integrate(1)

rust-embedded(1)

rust-嵌入式(1)

rust-生产环境(1)

rust-production(1)

网页快照(1)

网页截图(1)

水印效果(1)

图片水印(1)

yew-router(1)

css(1)

web-前端(1)

wasm-bindgen(1)

区块链(1)

blockchain(1)

dotenv(1)

标识符(1)

rust-1.53.0(1)

rust-1.56.0(1)

rust-项目升级(1)

异步运行时(1)

ssr(1)

tokio(1)

warp(1)

reqwest(1)

graphql-rust(1)


Elsewhere

- Open Source
  1. github/zzy
  2. github/sansx
- Learning & Studying
  1. Rust 学习资料 - 泥芹