GraphQL 初探

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对于 API 而言,GraphQL 被视为一种革命性的新思路、新技术。GraphQL 改变了前后端团队的交互方式、颠覆了前后端团队的通信方式,使得他们可以更顺畅而高效地协作。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"正如Samer Buna在其著作 《GraphQL in Action》 中的序言中所说的那样:","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"早在 2015 年,Facebook 首次宣布 GraphQL 项目时,我第一次听说了 GraphQL,那个时候 GraphQL 就深深的吸引了我。并且,学习 GraphQL 是我做过的最好的时间投资之一。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最近,在不断的了解、学习、使用 GraphQL 之后,我也像 Samer Buna 一样,被 GraphQL 深深的吸引了。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"GraphQL简介","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"关于 GraphQL 的介绍,网上已经有非常多的资料了,这里不再过多描述,具体可以参考 ","attrs":{}},{"type":"link","attrs":{"href":"https://graphql.cn/","title":null,"type":null},"content":[{"type":"text","text":"GraphQL.org","attrs":{}}]},{"type":"text","text":",","attrs":{}},{"type":"link","attrs":{"href":"https://docs.github.com/en/graphql","title":null,"type":null},"content":[{"type":"text","text":"GitHub GraphQL Docs","attrs":{}}]},{"type":"text","text":",以及 《GraphQL in Action》。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"GraphQL vs. REST","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"关于 GraphQL 和 REST 之间的详细对比,我认为可以参考 ","attrs":{}},{"type":"link","attrs":{"href":"https://www.apollographql.com/blog/graphql/basics/graphql-vs-rest/","title":null,"type":null},"content":[{"type":"text","text":"GraphQL vs. REST","attrs":{}}]},{"type":"text","text":" 以及 ","attrs":{}},{"type":"link","attrs":{"href":"https://www.howtographql.com/basics/1-graphql-is-the-better-rest/","title":null,"type":null},"content":[{"type":"text","text":"GraphQL is the better REST","attrs":{}}]},{"type":"text","text":",此处不再赘述了。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"GraphQL 的优势","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 《GraphQL in Action》 中,作者对 GraphQL 的优缺点做了很全面的介绍;在 ","attrs":{}},{"type":"link","attrs":{"href":"https://graphql.org/","title":null,"type":null},"content":[{"type":"text","text":"GraphQL 的社区网站","attrs":{}}]},{"type":"text","text":" 上,也对 GraphQL 的优点做了全面的介绍;甚至,可以用搜索引擎搜索出一堆 GraphQL 相关的文章和资源……","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是,作为一个刚接触 GraphQL 的门外汉,我还是希望写一写自己学习 GraphQL 之后的感想,尤其是我觉的 GraphQL 那些能让我激动无比的特性。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"GraphQL 的强类型系统","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 GraphQL 中,类型系统用于描述 GraphQL Server 的能力并用于判断一个查询是否有效。类型系统还描述了查询参数的输入类型,并在 GraphQL Runtime 中检查参数值的有效性。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一个 GraphQL 服务是通过定义","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"类型","attrs":{}}],"attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"类型","attrs":{}}],"attrs":{}},{"type":"text","text":"上的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"字段","attrs":{}}],"attrs":{}},{"type":"text","text":"来创建的,然后给每个","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"类型","attrs":{}}],"attrs":{}},{"type":"text","text":"上的每个","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"字段","attrs":{}}],"attrs":{}},{"type":"text","text":"提供","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"解析函数","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如,在 ","attrs":{}},{"type":"link","attrs":{"href":"https://docs.github.com/en/graphql/overview/explorer","title":null,"type":null},"content":[{"type":"text","text":"Github GraphQL Server","attrs":{}}]},{"type":"text","text":" 中,使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"viewer","attrs":{}}],"attrs":{}},{"type":"text","text":" 字段来描述当前登录用户的信息,而 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"viewer","attrs":{}}],"attrs":{}},{"type":"text","text":" 的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"类型","attrs":{}}],"attrs":{}},{"type":"text","text":"为 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"User","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"type Query {\n viewer: User!\n}\n\ntype User {\n // ...\n location: String\n login: String!\n name: String\n // ...\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一旦启动某个 GraphQL Server,它就能接收 GraphQL 查询,并验证和执行该查询。GraphQL Server 首先会检查该查询以确保它只引用了已定义的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"类型","attrs":{}}],"attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"字段","attrs":{}}],"attrs":{}},{"type":"text","text":",然后运行指定的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"解析函数","attrs":{}}],"attrs":{}},{"type":"text","text":"来生成结果。例如,我们可以在 ","attrs":{}},{"type":"link","attrs":{"href":"https://docs.github.com/en/graphql/overview/explorer","title":null,"type":null},"content":[{"type":"text","text":"Github GraphQL Server","attrs":{}}]},{"type":"text","text":" 中运行如下的查询:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"query { \n viewer { \n login\n name\n }\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"从如上的例子可以看出,GraphQL 实际上是一种强类型的 API。与使用普通的 REST API 相比,强类型系统是 GraphQL 最吸引我的地方之一。在我看来,正是 GraphQL 强类型的特性,开创了 API 的新天地。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在我看来,REST API 缺乏","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"类型系统","attrs":{}}],"attrs":{}},{"type":"text","text":"特性在开发中会引入很多的问题,工作中,我经常会遇到如下的情况:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上游服务修改了 REST API 的字段类型而未周知下游消费者导致下游服务异常甚至崩溃","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"服务端未返回特定字段导致客户端崩溃","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客户端请求参数异常,导致服务端的错误率上升","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"到处找借口文档,好不容易找到了忽然发现,接口文档和接口的返回并不一致,文档太滞后了","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"……","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果有一个类型系统进行约束,则情况会变的更好一点。因此,针对于 json 格式的 REST API 而言,开始出现类似 ","attrs":{}},{"type":"link","attrs":{"href":"https://swagger.io/tools/swagger-ui/","title":null,"type":null},"content":[{"type":"text","text":"Swagger UI","attrs":{}}]},{"type":"text","text":" 这样的工具,利用 ","attrs":{}},{"type":"link","attrs":{"href":"https://json-schema.org/","title":null,"type":null},"content":[{"type":"text","text":"Json Schema","attrs":{}}]},{"type":"text","text":" 来强化其类型系统。虽然如此,但是,在我看来,基于 Json Schema 的 REST API 类型系统仅仅是一个规范和建议而已,团队中的其他人不使用也没办法。但是,GraphQL 的类型系统是其根基,所有人必须遵守,这就在大家对接口描述形成统一认识上发挥着重要作用。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同样的道理,对于 ","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/Tencent/rapidjson/","title":null,"type":null},"content":[{"type":"text","text":"rapidjson","attrs":{}}]},{"type":"text","text":" 而言,从 v1.1 版本开始,也加入了 JSON Schema 功能,使其可以在解析或生成 JSON 时进行校验,以避免类型错误的 json 而导致的解析时崩溃问题的发生。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"弱类型语言引入类型系统也变得越来越流行,例如微软的 ","attrs":{}},{"type":"link","attrs":{"href":"https://www.typescriptlang.org/","title":null,"type":null},"content":[{"type":"text","text":"TypeScript","attrs":{}}]},{"type":"text","text":",PHP7 的 ","attrs":{}},{"type":"link","attrs":{"href":"https://www.php.net/manual/en/language.types.declarations.php","title":null,"type":null},"content":[{"type":"text","text":"强类型模式","attrs":{}}]},{"type":"text","text":",还有 Facebook 开发的 ","attrs":{}},{"type":"link","attrs":{"href":"https://hacklang.org/","title":null,"type":null},"content":[{"type":"text","text":"Hack语言","attrs":{}}]},{"type":"text","text":"……","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"弱类型,看似无拘无束,实则充满了混乱与危险;强类型,看似画地为牢,实则是另一种自由。在 GraphQL 类型系统的帮助下,我们可以精确的描述我们的接口数据,并且可以做到数据描述和接口完全对应。这种能力对于大型、多人协作式团队而言,至关重要。尤其是微服务盛行的今天,我认为,GraphQL 的强类型系统为微服务交互提供了新的活力。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"我的地盘听我的,仅要我所需","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 GraphQL 中,GraphQL Runtime 确定 GraphQL Server 可以提供的能力,而消费端具体要获取哪些数据则完全由消费者说了算。对于下游消费者而言真正做到了:我的地盘,听我的!只要我所要的,不多也不少!","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"之所以说 GraphQL 是革命性的新技术,我觉得就在于此。GraphQL 改变了服务之间交互的方式,让通信双方在交互的过程中更加平等,改变了之前响应数据完全由 Server 端说了算的局面。如今,在 GraphQL 中,客户端(消费端)可以以更加平等的地位,更加积极的参与到整个的数据交互过程。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"借助于 GraphQL 的类型系统,客户端可以更加自由的根据自己的需求来获得自己的所需,而无需收到 Server 端的限制。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在我的个人博客站点中,我采用 ","attrs":{}},{"type":"link","attrs":{"href":"https://docs.github.com/en/actions","title":null,"type":null},"content":[{"type":"text","text":"github action","attrs":{}}]},{"type":"text","text":" 来编译并发布站点内容。站点中,会有很多基于 ","attrs":{}},{"type":"link","attrs":{"href":"https://www.gitbook.com/","title":null,"type":null},"content":[{"type":"text","text":"gitbook","attrs":{}}]},{"type":"text","text":" 的书籍,并且每一本书都对应一个 github 仓库。为了加快站点的发布速度,对于每一本书籍而言,都会提前编译并且将编译产物置于 latest release 中。在发布主站内容时,我利用 ","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/wangwei1237/wangwei1237.github.io_src/blob/master/build_books.sh","title":null,"type":null},"content":[{"type":"text","text":"build_books.sh","attrs":{}}]},{"type":"text","text":" 脚本来获取书籍的 release 产物。如果采用REST API,则相对比较简单,直接请求如下的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"end point","attrs":{}}],"attrs":{}},{"type":"text","text":" 即可:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"https://api.github.com/repos/wangwei1237/${BOOKS[i]}/releases/latest","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这种情况下,我只需要对应的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"assets.browser_download_url","attrs":{}}],"attrs":{}},{"type":"text","text":"字段,但是使用 REST API 我没办法控制接口的返回数据,即便我只需要某一个字段,接口还是会返给我所有的数据。而如果使用 GraphQL,我就可以自己说了算了:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"// GraphQL Query\nquery getBooksAssetUrl($repo: String!, $owner: String!) { \n repository(name: $repo, owner: $owner) {\n latestRelease {\n releaseAssets(first: 1) {\n nodes {\n downloadUrl\n }\n }\n }\n }\n}\n\n// Query Variables\n{\n \"repo\": \"monolith-to-microservices\",\n \"owner\": \"wangwei1237\"\n}\n\n// Query Response\n{\n \"data\": {\n \"repository\": {\n \"latestRelease\": {\n \"releaseAssets\": {\n \"nodes\": [\n {\n \"downloadUrl\": \"https://github.com/wangwei1237/monolith-to-microservices/releases/download/v1.1.1/monolith-to-microservices.tar.gz\"\n }\n ]\n }\n }\n }\n }\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因此,在这种情况下,GraphQL 完美的避免了不必要的数据传输(overfetching),尤其是对于如下的场景:流量较大,网络较差,针对不同的客户端返回不同数据……,这个特性尤为重要。更重要的是,这个特性改变了通信双方的话语权。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"精确预测响应数据","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GraphQL 不但改变了通信双方的话语权,还使得客户端可以精确的预测服务端的响应。在使用 REST API 时,我们需要借助各种 API 文档才能","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"大概","attrs":{}},{"type":"text","text":"预测请求的响应,在开发中,这确实是一件非常讨厌的事情。无法精确的预测请求的响应,这回直接导致代码的容错性大大折扣。谁能确保所有的接口的接口文档都会详细的穷举所有情况下的接口响应呢?据我的经验而言,很难……","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当我请求一个没有 release 产物的的仓库时,根据 GraphQL 的类型系统,我会预测到,此时 GraphQL 会返回 null,如下所示:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"// latestRelease type\nlatestRelease: Release\n\n// Query Variables\n{\n \"repo\": \"wangwei1237.github.io\",\n \"owner\": \"wangwei1237\"\n}\n\n// Query Response\n{\n \"data\": {\n \"repository\": {\n \"latestRelease\": null\n }\n }\n}","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"GraphQL API 的版本控制","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对于 API 的版本控制而言,GraphQL 借鉴了其他语言中的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"@deprecated注解","attrs":{}}],"attrs":{}},{"type":"text","text":"。如果经受过 REST API 的版本控制之痛,经受过那些 v1,v2,……,那么你也会像我一样,喜欢 GraphQL 提供的这一特性。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"摘录Samer Buna在 《GraphQL in Action》 中的例子如下:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"type Query {\n hello: String\n currentTime: String\n sumNumbersInRange(begin: Int!, end: Int!): Int! @deprecated(reason: \"use new fields numbersInRange\")\n numbersInRange(begin: Int!, end: Int!): NumbersInRange!\n taskMainList: [Task!]\n taskInfo(taskId: ID!): Task!\n}\n\n\"\"\"Aggregate info on a range numbers\"\"\"\ntype NumbersInRange {\n sum: Int!\n count: Int!\n avg: Float!\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对于 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sumNumbersInRange","attrs":{}}],"attrs":{}},{"type":"text","text":" 字段,使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"@deprecated","attrs":{}}],"attrs":{}},{"type":"text","text":" 标志该字段是废弃字段,并且利用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"reason","attrs":{}}],"attrs":{}},{"type":"text","text":" 参数来将该废弃字段和推荐的新字段关联起来。如果配合 IDE 的代码扫描能力,当在接口中请求 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sumNumbersInRange","attrs":{}}],"attrs":{}},{"type":"text","text":" 的时候,IDE 给出对应的废弃提示和原因,那么经过一段时间的迭代,标记为 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"@deprecated","attrs":{}}],"attrs":{}},{"type":"text","text":" 的字段就会慢慢消失在产品代码中。这会使得 API 的版本管理更加容易,也使得客户端更加容易理解 API 的进化和向后兼容。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而使用 REST API 的时候,在没有这种类似机制的帮助下,我经常会使用了上游服务准备 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"deprecated","attrs":{}}],"attrs":{}},{"type":"text","text":" 的字段,因此,在这种情况下,准备废弃的字段不得不为了兼容而永久的保留在了服务之中。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我见过一个 REST API,这个 API 经过好几年的发展,每次请求都会返回 10K+ 的数据,并且里面有很多字段的含义基本一致,只不过是有些字段为了兼容某个特定的需求而增加。这个 API 中的字段谁也不敢动,能做的只能是随着需求的增加而不断的增加该 API 的规模。开发这个 API 是一件非常可怕的事情。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"客观看待 GraphQL 的缺点","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"新生之物,其貌必丑。虽然 GraphQL 已经发展了几年,但是实际上而言 GraphQL 还算是一个比较新颖的技术。根据 《GraphQL in Action》 中的第 1.3 节的介绍,到目前为止,GraphQL 仍然有很多的问题需要解决,例如:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"安全性问题","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"缓存问题","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"学习门槛问题","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"……","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"虽然如此,我还是认为 GraphQL 开创了 API 交互的新天地,代表着新的技术的发展方向。并且我也发现,目前整个 GraphQL 社区也在做很多的工作来解决目前的一些问题。例如 Facebook 为了解决缓存问题而开发的 dataloader 库,","attrs":{}},{"type":"link","attrs":{"href":"https://www.apollographql.com/","title":null,"type":null},"content":[{"type":"text","text":"apollo graphql","attrs":{}}]},{"type":"text","text":" 在推动 GraphQL 工业落地方面所做的各种努力,……","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"正如我在 ","attrs":{}},{"type":"link","attrs":{"href":"https://wangwei1237.gitee.io/2021/05/01/how-to-improve-the-work-efficiency/","title":null,"type":null},"content":[{"type":"text","text":"如何提升工作效率","attrs":{}}]},{"type":"text","text":" 一文中说的那样:","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们必须认识到,短期看,新技术会解决我们当前的问题,但是新技术必然会引入新的问题,我们需要与时俱进,在新的场景下不断解决新技术带来的问题。这有这样,才能不断的提升工作效率。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们不能因为堵车,就回退到自行车的时代;我们不能因为有假币,就回退到物物交换的时代;……","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"是否马上使用 GraphQL","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"即便如此,在确定是否要使用 GraphQL 技术时,需要做认真的分析,且不可为了追新而采用 GraphQL。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果所提供服务的下游消费者较少并且范围也较少,那么还是推荐使用 REST API。但是如果提供的服务是类似 Github Open API 这样的规模较大的服务,或者使用该服务的下游消费者范围较大、数量较多,我认为采用 GraphQL 确实是上上之选。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"总之,越是涉及到团队交互多、团队协作多的服务,我越是建议采用 GraphQL。因为协作最复杂的地方在于 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"communication","attrs":{}}],"attrs":{}},{"type":"text","text":",而 GraphQL 就是革新多方协作的一种新技术。就像万维网彻底改变了人类世界的交流方式一样,我想,GraphQL 正在彻底改变 API 的交流方式,尤其是在微服务架构中。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f7/f739f971a1af25b58c88a113e4d765e0.png","alt":"","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"学习资源","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://spec.graphql.cn/","title":null,"type":null},"content":[{"type":"text","text":"https://spec.graphql.cn/","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://graphql.org/","title":null,"type":null},"content":[{"type":"text","text":"https://graphql.org/","attrs":{}}]},{"type":"text","text":" or ","attrs":{}},{"type":"link","attrs":{"href":"https://graphql.cn/","title":null,"type":null},"content":[{"type":"text","text":"https://graphql.cn","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://graphql.org/swapi-graphql/","title":null,"type":null},"content":[{"type":"text","text":"https://graphql.org/swapi-graphql/","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://docs.github.com/en/graphql","title":null,"type":null},"content":[{"type":"text","text":"https://docs.github.com/en/graphql","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://docs.github.com/en/graphql/overview/explorer","title":null,"type":null},"content":[{"type":"text","text":"https://docs.github.com/en/graphql/overview/explorer","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://www.apollographql.com/docs/","title":null,"type":null},"content":[{"type":"text","text":"https://www.apollographql.com/docs/","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://www.howtographql.com/","title":null,"type":null},"content":[{"type":"text","text":"https://www.howtographql.com","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://wangwei1237.gitee.io/shares/GraphQL_in_Action.pdf","title":null,"type":null},"content":[{"type":"text","text":"https://wangwei1237.gitee.io/shares/GraphQL_in_Action.pdf","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://github.com/graphql/graphql-playground","title":null,"type":null},"content":[{"type":"text","text":"https://github.com/graphql/graphql-playground","attrs":{}}]}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"参考文献","attrs":{}}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"Samer Buna. GraphQL in Action. Manning Publications, March 9, 2021. ","attrs":{}},{"type":"link","attrs":{"href":"https://wangwei1237.gitee.io/2021/06/15/Preliminary-Exploration-of-the-GraphQL/#fnref1","title":null,"type":null},"content":[{"type":"text","text":"↩︎","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://graphql.org/","title":null,"type":null},"content":[{"type":"text","text":"https://graphql.org/","attrs":{}}]},{"type":"text","text":", ","attrs":{}},{"type":"link","attrs":{"href":"https://graphql.cn/","title":null,"type":null},"content":[{"type":"text","text":"https://graphql.cn/","attrs":{}}]},{"type":"text","text":". ","attrs":{}},{"type":"link","attrs":{"href":"https://wangwei1237.gitee.io/2021/06/15/Preliminary-Exploration-of-the-GraphQL/#fnref2","title":null,"type":null},"content":[{"type":"text","text":"↩︎","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://docs.github.com/en/graphql","title":null,"type":null},"content":[{"type":"text","text":"Github Docs: GitHub GraphQL API","attrs":{}}]},{"type":"text","text":" ","attrs":{}},{"type":"link","attrs":{"href":"https://wangwei1237.gitee.io/2021/06/15/Preliminary-Exploration-of-the-GraphQL/#fnref3","title":null,"type":null},"content":[{"type":"text","text":"↩︎","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://www.apollographql.com/blog/graphql/basics/graphql-vs-rest/","title":null,"type":null},"content":[{"type":"text","text":"GraphQL vs. REST","attrs":{}}]},{"type":"text","text":". ","attrs":{}},{"type":"link","attrs":{"href":"https://wangwei1237.gitee.io/2021/06/15/Preliminary-Exploration-of-the-GraphQL/#fnref4","title":null,"type":null},"content":[{"type":"text","text":"↩︎","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://www.howtographql.com/basics/1-graphql-is-the-better-rest/","title":null,"type":null},"content":[{"type":"text","text":"GraphQL is the better REST","attrs":{}}]},{"type":"text","text":" ","attrs":{}},{"type":"link","attrs":{"href":"https://wangwei1237.gitee.io/2021/06/15/Preliminary-Exploration-of-the-GraphQL/#fnref5","title":null,"type":null},"content":[{"type":"text","text":"↩︎","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://www.prisma.io/blog/top-5-reasons-to-use-graphql-b60cfa683511","title":null,"type":null},"content":[{"type":"text","text":"Reasons to use GraphQL | Top 5 Reasons Why and How to use GraphQL","attrs":{}}]},{"type":"text","text":" ","attrs":{}},{"type":"link","attrs":{"href":"https://wangwei1237.gitee.io/2021/06/15/Preliminary-Exploration-of-the-GraphQL/#fnref6","title":null,"type":null},"content":[{"type":"text","text":"↩︎","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":7,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://www.cloudbees.com/blog/graphql-as-an-api-gateway-to-micro-services","title":null,"type":null},"content":[{"type":"text","text":"GraphQL as an API Gateway to Microservices","attrs":{}}]},{"type":"text","text":" ","attrs":{}},{"type":"link","attrs":{"href":"https://wangwei1237.gitee.io/2021/06/15/Preliminary-Exploration-of-the-GraphQL/#fnref7","title":null,"type":null},"content":[{"type":"text","text":"↩︎","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":8,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://www.infoq.com/news/2021/03/netflix-graphql-microservices/","title":null,"type":null},"content":[{"type":"text","text":"Netflix Embraces GraphQL Microservices for Rapid Application Development","attrs":{}}]},{"type":"text","text":" ","attrs":{}},{"type":"link","attrs":{"href":"https://wangwei1237.gitee.io/2021/06/15/Preliminary-Exploration-of-the-GraphQL/#fnref8","title":null,"type":null},"content":[{"type":"text","text":"↩︎","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":9,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://netflixtechblog.com/beyond-rest-1b76f7c20ef6","title":null,"type":null},"content":[{"type":"text","text":"Beyond REST——Rapid Development with GraphQL Microservices","attrs":{}}]},{"type":"text","text":" ","attrs":{}},{"type":"link","attrs":{"href":"https://wangwei1237.gitee.io/2021/06/15/Preliminary-Exploration-of-the-GraphQL/#fnref9","title":null,"type":null},"content":[{"type":"text","text":"↩︎","attrs":{}}]}]}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章