GraphQL 查询和 REST 变更操作在同一个项目中使用是否更合适?

huangapple go评论60阅读模式
英文:

Is it preferred to use GraphQL for querying and REST for mutation operations in the same project?

问题

我正在开发一个电子商务网站,它需要为其移动应用程序提供API。一开始看到GraphQL的实现问题让我考虑将GraphQL用于查询数据,而将REST用于变更操作(存储、更新、删除)。

这样做可以吗,还是应该选择其中一个来执行所有操作?

英文:

I am working on a Ecommerce Site which needs API's for its mobile application. Seeing the implementation issues at first of GraphQL made me think of integrating GraphQL for querying data and REST for the mutation operations (store, update, delete).

Is it ok to do these things or should I just stick with any one of them for complete operations?

答案1

得分: 2

有很多个别情况,其中更适合或甚至需要单独的端点。举几个例子:身份验证、文件上传、第三方 Web 钩子或任何应返回非 JSON 内容的请求。

说所有具有副作用的操作都应通过单独的端点完成似乎有些过度。您不仅会失去与 GraphQL 相关的典型优势(声明性数据获取、可预测的响应等),而且还会使前端开发人员的工作变得更加困难,特别是在使用 Apollo 的情况下。这是因为缓存的查询响应可以基于突变中返回的数据进行自动更新——但是如果您不使用 GraphQL 来改变这些数据,您将不得不手动更新缓存。

英文:

There's plenty of individual cases where it's more appropriate or even necessary to have separate endpoints. To name a few: authentication, file uploads, third-party webhooks, or any requests that should return something other than JSON.

Saying that all operations with side-effects should be done through separate endpoints seems like overkill. You'll not only lose the typical benefits associated with GraphQL (declarative data fetching, predictable responses, etc.) but you'll also be making things harder for front end developers, particularly if using Apollo. That's because cached query responses can be automatically updated based on the data returned in a mutation -- if you don't use GraphQL to mutate that data, though, you'll have to manually update the cache yourself.

答案2

得分: 0

我建议坚持采用单一方法,原因如下:

  1. 在操作后,缓存数据可能发生预测性变化,否则您将不得不编写大量的临时代码,以确保基于REST的更新会更改客户端上的缓存数据。

  2. 单一模式有助于代码维护,并减少阅读代码时的开销。

  3. 可以将模式保存在一个地方,否则可能会出现重复的代码。

英文:

I would recommend to stick with a single approach because of the following reasons

  1. predictive changes to cached data after the operation otherwise you would have to write lot of duct tape code to ensure that the REST based updates mutates the cached data on the client.

  2. Single pattern for code maintenance and less overhead while reading code.

  3. To have a schema at a single place otherwise you might be duplicating code.

huangapple
  • 本文由 发表于 2020年1月3日 18:16:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定