如何在GraphQL服务器上启用gzip? [英] How to enable gzip at GraphQL server?

查看:53
本文介绍了如何在GraphQL服务器上启用gzip?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据

解决方案

问:如何在graphQL服务器上启用GZIP编码?

A:简短的答案,您不能.

为什么?因为GraphQL只是一个库,用于解析图查询并调用您提供的适当函数来建立图响应.

这与压缩您的 HTTP 响应并确保响应中包含 Content-Type = Gzip 标头无关.

换句话说,该文档试图说的是 GraphQL 响应看起来很像 JSON ,因此它们在压缩时可以很好地工作.主要是因为原始的 graph 响应的大小可能过大,并且在网络上传输它们的效率低下且速度慢.

只要在 NodeJS 中实现了用于 graphql 的HTTP服务器,您就可以使用 zlib 进行压缩.请参阅文档.

如果您是使用 express 框架处理NodeJ的大多数人,那么它会更加容易,因为已经有一个压缩插件.请参见此处.

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?

解决方案

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.

这篇关于如何在GraphQL服务器上启用gzip?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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