Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL GET response time is slow when comparing to RESTful

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) enter image description here

GraphQL endpoint result for POST request (response time: 88ms) enter image description here

RESTful endpoint result (response time: 44ms) enter image description here

like image 934
Kaz Avatar asked Jun 28 '19 06:06

Kaz


2 Answers

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.

like image 184
Daniel Rearden Avatar answered Oct 18 '22 21:10

Daniel Rearden


TLDR: Your REST example is simple and less complicated

In Lighthouse it is creating a AST for parsing the graphql request and your schema. It then afterwards passes all the directives and so on to figure out what you are trying to do. It also has to validate your query, to see if you can actually run it on the schema.

Depending on how you defined it in your application, there is a lot of steps it is passing through. However this can be reduced by multiple different ways, the parsing of your graphql schema can be cached, you could cache the result, use deferred fields (prob. wont speed up this example). You can read more about this in the performance section of the docs.

You are not specifying how your REST is setup, if you are using some kind of REST standard where it has to parse the data also. If you add more features, there is more code to run through, hence higher load speed.

like image 5
Oliver Nybroe Avatar answered Oct 18 '22 23:10

Oliver Nybroe