Compression in HTTP
Compression is a technique used to reduce the size of the data that is being transferred between the server and the client. This is done by compressing the data before sending it and then decompressing it on the client side. This can help reduce the amount of data that needs to be transferred, which can improve the performance of your website.
Content-Encoding and Accept-Encoding HTTP headers are used for this behavior. The
Content-Encoding header is used to specify the compression algorithm that was used to compress the
data, while the Accept-Encoding header is used to specify the compression algorithms that the
client supports.
Learn more about compression in HTTP
Hive Gateway is capable of handling compressions in the following directions. We can selectively enable or disable compression in each direction.
From the gateway to the client
When the client sends a request to the gateway, it can specify the compression algorithm that it
supports using the Accept-Encoding header. Then the gateway can compress the response using the
specified algorithm before sending it back to the client with the Content-Encoding header, so that
the client can decompress it.
In the following example, we say that the client supports the gzip algorithm for compression. Then
the gateway compresses the response using the gzip algorithm before sending it back to the client.
So the client can decompress the response using the gzip algorithm.
You need to configure the gateway for this feature. See here
From the client to the gateway
When the client sends a request to the gateway, it can compress the request using the specified algorithm before sending it to the gateway. Then the gateway can decompress the request before processing it.
In the following example, we compress the request using the gzip algorithm before sending it to
the gateway. Then the gateway decompresses the request before processing it.
Here we are using the gzip function to compress the request body before sending it to the gateway.
We assume that the gzip function is a function that compresses the data using the gzip algorithm.
Configuration on Gateway
In your gateway configuration, you need to enable the compression for the gateway.
Now gateway will respect the Accept-Encoding header from the client and compress the response
accordingly.
From the subgraph to the gateway
When the subgraph sends a response to the gateway, it can compress the response using the specified algorithm before sending it to the gateway. Then the gateway can decompress the response before sending it to the client.
It has the same principle as the previous example, but here the gateway is acting like a client against a subgraph.
Configuration on Subgraph
You should configure your subgraph to respect the Accept-Encoding header and compress the response
accordingly. For example if you have a GraphQL subgraph using
GraphQL Yoga server you can use useContentEncoding
plugin to enable this;
From the gateway to the subgraph
When the gateway sends a request to the subgraph, it can compress the request using the specified algorithm before sending it to the subgraph. Then the subgraph can decompress the request before processing it.
In this case, gateway will always send a compressed request to the defined subgraphs with
Content-Encoding header.
Configuration on Gateway
In your gateway configuration, you need to enable the compression.