本文介绍了与RESTful相比,GraphQL GET响应时间慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试GraphQL端点和RESTful端点的响应时间,因为我以前从未使用过GraphQL,因此我将在下一个Laravel项目中使用它.

I wanted to test the response times of a GraphQL endpoint, and a RESTful endpoint as I haven't ever used GraphQL before, and I am about to use it in my next Laravel project.

因此,我正在使用 Lighthouse PHP软件包通过我的Laravel应用提供GraphQL端点,已创建一个RESTful端点.

So I am using Lighthouse PHP package to serve a GraphQL endpoint from my Laravel app, and also I have created a RESTful endpoint.

两个端点(GraphQL和RESTful)都旨在从我的本地数据库中获取所有用户(250个用户).

Both endpoints(GraphQL and RESTful) are intended to get all Users(250 users) from my local Database.

因此,基于测试,我在这里注意到的是,当我在Postman上测试这两个端点时,RESTful端点响应比GraphQL端点更快.

So based on the test what I have noticed here is that, when I tested this both endpoints on Postman, the RESTful endpoint response is faster than GraphQL endpoint.

我能知道为什么在两个端点都获取相同数据时GraphQL端点的响应比RESTful需要更多的时间吗?

Can I know why GraphQL endpoint's response takes more time than RESTful while both endpoints are getting same data?

GETQ请求的GraphQL端点结果(响应时间:88ms)

GraphQL endpoint result for GET request (response time: 88ms)

POST请求的GraphQL端点结果(响应时间:88ms)

GraphQL endpoint result for POST request (response time: 88ms)

RESTful端点结果(响应时间:44ms)

RESTful endpoint result (response time: 44ms)

推荐答案

没有免费的午餐.

GraphQL提供了许多有用的功能,但是这些相同的功能总是会产生一些开销.尽管REST端点可以有效地从某些来源提取数据并将其重新配置回客户端,但即使对于相对较小的数据集,GraphQL也会拥有进行一些额外的处理来解析和验证每个个体字段.更不用说解析和验证请求本身所需的处理.而且,这种开销只会随着返回数据的大小而变得更大.

GraphQL offers a lot of useful features, but those same features invariably incur some overhead. While a REST endpoint can effectively pull data from some source and regurgitate it back to the client, even for a relatively small dataset, GraphQL will have to do some additional processing to resolve and validate each individual field in the response. Not to mention the processing required to parse and validate the request itself. And this overhead only gets bigger with the size of the data returned.

如果要向GraphQL镜像的REST端点引入其他功能(请求响应验证,对部分响应的支持,对单个响应字段进行别名的能力等),您将看到两者之间的性能差距不断缩小.尽管如此,尽管如此,它仍然还是苹果和橘子的比较,因为GraphQL服务将经历某些动作,仅仅是因为这就是规范说可以.

If you were to introduce additional features to your REST endpoint (request and response validation, support for partial responses, ability to alias individual response fields, etc.) that mirrored GraphQL, you would see the performance gap between the two shrink. Even then, though, it's still somewhat of an apples and oranges comparison, since a GraphQL service will go through certain motions simply because that's what the spec says to do.

这篇关于与RESTful相比,GraphQL GET响应时间慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:19