How to enable gzip at GraphQL server?

懵懂的女人 提交于 2020-05-15 06:57:10

问题


According to the this article, it's encouraged that any production GraphQL services enable GZIP and encourage their clients to send the header: Accept-Encoding: gzip

I've tested this in Postman, with "Accept-Encoding" enabled or disable, I didn't see any difference in the responded "content-length".

So my question, how to enable GZIP encoding at graphQL server?


回答1:


Q: How to enable GZIP encoding at graphQL server?

A: Short answer, you can't.

Why? Because GraphQL is just a library for parsing your graph queries and calling the appropriate functions, supplied by you, to build out a graph response.

It is not about compressing your HTTP response and making sure that the response has the Content-Type=Gzip header in it.

In other words, what that piece of document is trying to say is that the GraphQL response looks pretty much like JSON and therefore they work well when compressed. Mainly because raw graph responses can be quite bloated in size and it is inefficient and slow to transfer them across the network.

Just in case your HTTP server for graphql is implemented in NodeJS, you can use the zlib to do the compression. See documentation.

If you are the majority of people who uses theexpress framework for NodeJs then it is even more easier as there is a compression plugin for it already. See here.



来源:https://stackoverflow.com/questions/49187470/how-to-enable-gzip-at-graphql-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!