Search code examples
encodinggraphql

How to enable gzip at GraphQL server?


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?

enter image description here


Solution

  • 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.