简体   繁体   English

REST 与数据库作为 GraphQL API 数据源?

[英]REST vs Database as GraphQL API data source?

We are starting a new full stack project.我们正在开始一个新的全栈项目。 It has been decided we are going to use React for the front end which should consume a GraphQL API.已经决定我们将使用 React 作为前端,它应该使用 GraphQL API。

We are contemplating two scenarios for the development of this API:我们正在考虑开发此 API 的两种情况:

  • The first one is to build a GraphQL API that uses a REST API as data source using Apollo.第一个是使用 Apollo 构建一个使用 REST API 作为数据源的 GraphQL API。

  • The second one is to build a GraphQL API that uses a database as data source skipping the REST API.第二个是构建一个使用数据库作为数据源的 GraphQL API,跳过 REST API。

What are the advantages and disadvantages of each scenario?每种情况的优缺点是什么? Is one better than the other?这个比那个好吗?

Based on our discussion on gitter,根据我们对 gitter 的讨论,

Since you are building your platform from scratch, you don't need to build your GraphQL layer over REST APIs.由于您是从头开始构建平台,因此无需通过 REST API 构建 GraphQL 层。 That will be double work and is not needed.那将是双重工作,不需要。 I recommend that you set up Apollo-Server which will directly interact with your database(s).我建议您设置Apollo-Server ,它将直接与您的数据库交互。

You said that you might need to use some legacy APIs for which I recommended Microservices which can easily work with graphql.你说你可能需要使用一些遗留的 API,我推荐了微服务,它可以很容易地与 graphql 一起工作。

Regarding PRISMA, it doesn't give you as much flexibility.关于 PRISMA,它没有给你太多的灵活性。 It also does not allow ACID compliant updates for fields such as increment or decrement a value.它还不允许对字段进行符合 ACID 的更新,例如递增或递减值。 This issue on github can be used to track updates on this feature. github 上的这个问题可用于跟踪此功能的更新。

PRISMA is good for getting you set up quickly but I(personal opinion) feel that it is not as flexible. PRISMA 有助于快速设置,但我(个人意见)觉得它不那么灵活。

Your final question was how do you query database/databases without REST API, for which I suggested querying directly from your server.您的最后一个问题是如何在没有 REST API 的情况下查询数据库,为此我建议直接从您的服务器查询。

GraphQL is client side and Apollo will help you to extend it and provide much more features. GraphQL 是客户端,Apollo 将帮助您扩展它并提供更多功能。 You don't need to do things much different than what you'd do when creating a REST API other than the fact that you don't need to define separate endpoints for each request.除了不需要为每个请求定义单独的端点之外,您不需要做与创建 REST API 时所做的事情有很大不同的事情。 The database interactions remain almost the same other than the fact that you only return/query for the data that you need and this can be done dynamically.除了您只返回/查询您需要的数据并且这可以动态完成之外,数据库交互几乎保持不变。 From graphql website来自graphql 网站

Send a GraphQL query to your API and get exactly what you need, nothing more and nothing less.向您的 API 发送 GraphQL 查询并准确获取您需要的内容,仅此而已。 GraphQL queries always return predictable results. GraphQL 查询总是返回可预测的结果。 Apps using GraphQL are fast and stable because they control the data they get, not the server.使用 GraphQL 的应用程序快速且稳定,因为它们控制获得的数据,而不是服务器。

Other mentions: AWS Appsync其他提及: AWS Appsync

The second scenario is best第二种情况最好

The second one is to build a GraphQL API that uses a database as data source skipping the REST API.第二个是构建一个使用数据库作为数据源的 GraphQL API,跳过 REST API。

REST and GraphQL can both be operated over HTTP, though GraphQL is protocol agnostic. REST 和 GraphQL 都可以通过 HTTP 操作,尽管 GraphQL 与协议无关。 And GraphQL is the new kid in the block with lot of cool features and it solves most of the problem faced with REST like: GraphQL 是这个领域的新生事物,具有许多很酷的特性,它解决了 REST 面临的大部分问题,例如:

1.) Multiple end points to fetch data 1.) 多个端点获取数据

In rest we have create multiple API endpoints

2.) Over/Under Fetching 2.) 过度/不足获取

Some time API return extra data that we don't need or less data in response

3.) Network Requests 3.) 网络请求

multiple endpoint leads to more network requests 

4.) Error Handling 4.) 错误处理

Error handling in REST is pretty straightforward, we simply check the HTTP headers to get the status of a response. REST 中的错误处理非常简单,我们只需检查 HTTP 标头即可获取响应状态。 Depending on the HTTP status code ( 404, 503, 500 etc) we get, we can easily tell what the error is and how to go about resolving it.根据我们获得的 HTTP 状态代码(404、503、500 等),我们可以轻松判断错误是什么以及如何解决它。 GraphQL on the other hand, when operated over HTTP, we will always get a 200 OK response status.另一方面,当通过 HTTP 操作 GraphQL 时,我们将始终获得 200 OK 响应状态。 When an error occurs while processing GraphQL queries, the complete error message is sent to the client with the response当处理 GraphQL 查询时发生错误时,完整的错误消息与响应一起发送到客户端

5.) Versioning 5.) 版本控制

Often when consuming third-party REST APIs, we see stuff like v1, v2, v3 etc. which simply indicate the version of the REST API we are using.通常在使用第三方 REST API 时,我们会看到 v1、v2、v3 等内容,它们仅指示我们正在使用的 REST API 的版本。 This leads to code redundancy and less maintainable code.这会导致代码冗余和代码的可维护性降低。 With GraphQL, there is no need for versioning as we can easily add new fields and types to our GraphQL API without impacting existing queries.使用 GraphQL,不需要版本控制,因为我们可以轻松地向我们的 GraphQL API 添加新字段和类型,而不会影响现有查询。 Also, we can easily mark fields as deprecated and the fields will be excluded from the response gotten from the server此外,我们可以轻松地将字段标记为已弃用,并且这些字段将从服务器获得的响应中排除

For detailed comparison please visit GraphQl vs Rest有关详细比较,请访问GraphQl vs Rest

or this post 或这篇文章

Or Official GraphQL site或官方 GraphQL 站点

Using REST to power your GraphQL is like using a to power your Tesla.使用 REST 为您的 GraphQL 提供动力就像使用 a 为您的特斯拉提供动力一样。 Simply dumb,.简直傻逼,。 Use a database but only select the fields and records asked by GraphQL query.使用数据库但只选择 GraphQL 查询询问的字段和记录。 NOT "select *".不是“选择 *”。 Use Redis cache if you need the speed.如果需要速度,请使用 Redis 缓存。

REST is a software architecture that defines a set of constraints to be used for creating web services, Introduced in the year 2000. Whereas GraphQL is a data query and manipulation language for APIs, and runtime to fulfill queries with existing data, developed by Facebook in 2011. REST是一种软件体系结构,定义了用于创建Web服务的一组约束,于2000年推出。而GraphQL是API的数据查询和操作语言,以及使用现有数据执行查询的运行时,由Facebook在2000年开发。 2011。

Drawbacks of REST API? REST API的缺点?

The problem with REST APIs is they have multiple endpoints. REST API的问题在于它们具有多个端点。 These require round-trips to get the data. 这些需要往返来获取数据。 Every endpoint represents a resource, If we need data from multiple resources, It requires multiple round-trips to get the data. 每个端点都代表一个资源,如果我们需要来自多个资源的数据,则需要多次往返来获取数据。 The language needed to request is very limited in the REST API. REST API中要求的语言非常有限。

In REST there is a problem of over-fetching, for example: If a client wants to select a specific record in the resource, the REST API will always fetch all of the fields irrespective of the client's needs. 在REST中,存在一个过度获取的问题,例如:如果客户端要选择资源中的特定记录,则REST API将始终获取所有字段,而与客户端的需求无关。 It is over usage of network and memory resources but both client-side as well as server-side 它超出了网络和内存资源的使用范围,但是客户端和服务器端

Advantages of Graphql over REST Graphql相对于REST的优势

Resolves over-fetching and under-fetching Apps Using REST APIs results in over-fetching as well as under-fetching because of the entire data in that endpoint will be returned in the JSON format. 解决使用REST API 过度获取和不足获取的应用程序导致过度获取和不足获取的问题,因为该端点中的所有数据将以JSON格式返回。 This causes performance and scalability issues. 这会导致性能和可伸缩性问题。

Whereas GraphQL with its queries, schemas, and resolvers enable developers to design API call only specific data requirement, This resolves Over-fetching and Under-fetching challenges. 借助GraphQL及其查询,模式和解析器,开发人员可以设计API仅调用特定的数据需求,而这解决了过度提取和提取不足的难题。

Faster product Iterations on the frontend When designing REST APIs, It can be a bottleneck when it comes to faster or quick iterations on the frontend. 前端上更快的产品迭代在设计REST API时,谈到前端上更快或更快速的迭代可能会成为瓶颈。 The reason behind this is because of REST APIs design endpoints according to the views in the application. 其背后的原因是因为REST API根据应用程序中的视图设计端点。

With GraphQL, Developers can write queries specifying their data requirement, and the iterations for developing frontend can continue without having to change the backend 借助GraphQL,开发人员可以编写查询以指定其数据要求,并且开发前端的迭代可以继续进行而无需更改后端

GraphQL enables better analytics on the backend Apps that use REST APIs get entire data in an endpoint, using this application owner can't gain insights on the usage of specific data elements since the entire data is returned every time. GraphQL可以在后端上进行更好的分析,而使用REST API的应用程序可以在端点中获取全部数据,使用此应用程序的所有者无法获得有关特定数据元素使用情况的见解,因为每次都会返回整个数据。

On the other hand, GraphQL uses resolvers, and they implement particular fields in a type. 另一方面,GraphQL使用解析器,并且它们实现类型中的特定字段。 That way Application owner can track the performance of the resolvers, and find out whether the systems need performance tuning. 这样,应用程序所有者可以跟踪解析器的性能,并找出系统是否需要性能调整。

The Advantages of the GraphQL schema GraphQL uses Schema Definition Language (SDL), The schema includes all the types used in an API, It defines how a client should access data on the server. GraphQL模式的优点 GraphQL使用模式定义语言(SDL),该模式包括API中使用的所有类型,它定义客户端如何访问服务器上的数据。 After defining schema by the developers, Both the front and backend teams work parallel as they know the structure of the data. 由开发人员定义架构后,前端团队和后端团队都可以并行工作,因为他们知道数据的结构。 This helps to improve the productivity of the team. 这有助于提高团队的生产力。

GraphQL proves to be very useful where it comes to fetching data that satisfies the given condition. 事实证明,GraphQL在获取满足给定条件的数据时非常有用。 Example:- Instead of requesting all the students in the school, you can specifically ask for the students of a particular batch. 示例:-您可以不要求所有学校的学生,而可以专门要求特定批次的学生。

Full Article: GraphQL vs. REST: A Detailed Comparison. 全文: GraphQL与REST:详细比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM