mercurius icon indicating copy to clipboard operation
mercurius copied to clipboard

Federation Gateway not work properly behind reverse proxy

Open fahmifan opened this issue 2 years ago • 6 comments

I am testing mercurius as federation gateway, everything works correctly until i try to put it behind a proxy using caddy server.

This is my Caddyfile

:3000 {
    reverse_proxy 127.0.0.1:8100
    tls internal
}

This is the error i got

{
	"statusCode": 500,
	"code": "FST_ERR_BAD_STATUS_CODE",
	"error": "Internal Server Error",
	"message": "Called reply with an invalid status code: undefined"
}
{
  errors: [
    Error: Symbol(FEDERATED_ERROR)
        at Object.sendRequest (LOCAL_PATH/node_modules/mercurius/lib/gateway/request.js:96:13)
        at processTicksAndRejections (internal/process/task_queues.js:93:5)
        at async LOCAL_PATH/node_modules/mercurius/lib/gateway/make-resolver.js:498:24
        at async Object.fastifyGraphQl [as graphql] (LOCAL_PATH/node_modules/mercurius/index.js:627:23) {
      path: [Array],
      locations: [Array],
      extensions: [Object]
    }
  ],
  data: null
}
TypeError: Cannot read property 'data' of undefined
    at maybeFormatErrors (LOCAL_PATH/node_modules/mercurius/index.js:644:39)
    at Object.fastifyGraphQl [as graphql] (LOCAL_PATH/node_modules/mercurius/index.js:636:12)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

Caddy Version: v2.4.6 node dependencies

{
    "fastify": "^3.27.1",
    "graphql": "^16.3.0",
    "mercurius": "^9.3.2"
}

fahmifan avatar Feb 18 '22 03:02 fahmifan

So i clone the mercurius master to do some debug. And found the cause, it was this code, ~~this request never reached the federated service when the original request comes from caddy proxy~~ the response is not a json

image

The SDL query was ok

image

I also try upgrade the udici version to latest ^4.14.1 but, still the same

fahmifan avatar Feb 18 '22 05:02 fahmifan

So the issue is the federated service return response in gzip, but the mercurius gateway can't handle it if the request comes from reverse proxy

fahmifan avatar Feb 18 '22 06:02 fahmifan

We are not passing an Accept-Encoding header and it seems your server is always applying gzip encoding.

Could you try a quick fix passing Content-Encoding: identity via initHeaders and rewriteHeaders.

It might also be that we need a fix somewhere to handle this. Could you write an example to reproduce this with Node.js?

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding

mcollina avatar Feb 18 '22 20:02 mcollina

We are not passing an Accept-Encoding header and it seems your server is always applying gzip encoding.

Could you try a quick fix passing Content-Encoding: identity via initHeaders and rewriteHeaders.

It might also be that we need a fix somewhere to handle this. Could you write an example to reproduce this with Node.js?

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding

The federated service is written in Go and unfortunately I am unable to replicate it outside the project. But my team decided to disable compression from the federated service.

fahmifan avatar Feb 19 '22 00:02 fahmifan

@fahmifan is this issue solved?

simoneb avatar Jul 11 '22 15:07 simoneb

@simoneb well, yeah we resolved it by remove the gzip compression from the federated service. And we enable gzip only in the Load balancer

fahmifan avatar Jul 11 '22 15:07 fahmifan

I'm going to close this issue then

simoneb avatar Oct 18 '22 10:10 simoneb