与 RESTful 相比,GraphQL GET 响应时间很慢 [英] GraphQL GET response time is slow when comparing to RESTful

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

问题描述

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

所以我使用

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

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

解决方案

天下没有免费的午餐.

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

如果您要向 REST 端点引入其他功能(请求响应验证、支持部分响应、为单个响应字段设置别名等),您会看到镜像 GraphQL两者性能差距缩小.尽管如此,它仍然有点像苹果和橘子的比较,因为 GraphQL 服务会经历某些动作,仅仅是因为 spec 说要做.

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.

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

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

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.

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

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

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

RESTful endpoint result (response time: 44ms)

解决方案

There's no such thing as a free lunch.

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.

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 响应时间很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆