代码之家  ›  专栏  ›  技术社区  ›  jcollum

Passthrough proxy为一个GQL服务器返回404,但为另一个服务器正确地传递请求

  •  0
  • jcollum  · 技术社区  · 3 年前

    我们有一些GQL端点是不可靠的。此外,如果能够在我的应用程序中测试失败路径,那就太好了。因此,我创建了一个本地passthrough代理来发回一些固定的响应:

    import {createProxyMiddleware} from 'http-proxy-middleware'; // 3.0.0-beta.0
    
    const port = 3000;
    const app = express();
    app.use(cors());
    
    ...
    
    const onProxyRes = function (proxyRes, req, res) {
     // actual body trimmed for space
     // if the query string matches a particular value
     //   and the matcher is enabled
     // use the res object to send a response
     // e.g. res.send(accountsInRegion)
    }
    
    app.post(
     '/hub-gql-proxy',
     createProxyMiddleware({
       target: 'https://api.dev.cps-mycompany.com/digital-hub-api-gateway/',
       changeOrigin: true,
       on: {
         proxyRes: onProxyRes,
         error: (err, req, res) => {
           console.error(`\n\n*** proxy error at ${req.url}`, {err});
         },
       },
     })
    );
    
    app.post(
     '/data-product-gql-proxy',
     createProxyMiddleware({
       target: 'https://graphql.dev.mycompanydata.com/',
       changeOrigin: true,
       on: {
         proxyRes: onProxyRes,
         error: (err, req, res) => {
           console.error(`\n\n*** proxy error at ${req.url}`, {err});
         },
       },
     })
    );
    

    首先,我检查“旧的”GQL端点,它似乎工作得很好。我已经确认它工作正常,代理会毫无问题地传递请求,并使用适当的查询体返回正确的值。但是,为了进行一个简单的测试,我想确保空查询会为代理和实际端点返回相同的结果。修剪了大部分冗长的输出。

    ✗ curl -v -X POST \
    http://localhost:3000/hub-gql-proxy/\?GQLoperationName\=accountsInRegion
    < HTTP/1.1 400 Bad Request
    {"errors":[{"message":"Context creation failed: Missing authorization headers"
    
    ✗ curl -v -X POST \
    'https://api.dev.cps-mycompany.com/digital-hub-api-gateway/?GQLoperationName=accountsInRegion'
    < HTTP/2 400
    {"errors":[{"message":"Context creation failed: Missing authorization headers",
    

    好吧,这些看起来很好,通过代理和真正的端点获得相同的响应代码和主体。

    但是对于新的GQL端点,代理似乎没有正确地传递请求(提醒:除了响应代码之外,详细的输出被修剪掉了):

    ✗ curl -v -X POST \
    https://graphql.dev.mycompanydata.com/\?GQLoperationName\=GeneratePdfFile
    < HTTP/2 415
    Expected request with `Content-Type: application/json`%
    

    415是有道理的,没有尸体。

    ✗ curl -v -X POST \
    http://localhost:3000/data-product-gql-proxy\?GQLoperationName\=GeneratePdfFile
    < HTTP/1.1 404 Not Found
    (no response body)
    

    404?服务器是如何找不到端点的?我是否遗漏了有关代理配置的一些简单内容?

    0 回复  |  直到 3 年前
    推荐文章