首页 > 解决方案 > GraphIql apollo 引擎不支持的内容类型

问题描述

我正在运行 apollo-server-express,一切正常。我有 2 个端点 - 1 个用于 graphiql (/graphql) 和一个用于非交互式 (/client) 查询(我知道 - 在我开始使用 apollo 之前它们被称为)。

 app.use('/client', bodyParser.json() ,  
  (req, res,next) => {
    const context = { pool , apiKey:req.query.key , bidules };
    if (server.isAuthorized(context.apiKey)) {
      return graphqlExpress({
        schema: schema,
        context: context,
        tracing: true,
        cacheControl: {
          defaultMaxAge: 30,
        }
      }) (req,res,next); 
    }
    else {
      res.setHeader('Content-Type', 'application/json');
      res.status(403)
      .send (JSON.stringify({
        errors:[ {message: "api key is unauthorized"} ]
      })); 
    }
  }
);

// endpoint for browser graphIQL
app.use('/graphql',  graphiqlExpress({ 
  endpointURL: '/client' 
}));


app.use("/schema", (req, res) => {
  res.set("Content-Type", "text/plain");
  res.send(printSchema(schema));
});

但是,当我介绍阿波罗引擎时

engine.listen({
  port: port,
  expressApp: fidserver.app,
  graphqlPaths: ['/graphql', '/client']
});

一切仍然正常 - 除非我在浏览器上使用查询作为浏览器 url 上的参数刷新 graphiql。

然后我得到这个错误

{"errors":[{"message":"Unsupported Content-Type from origin: text/html"}]}

在没有 apollo 引擎运行的情况下做同样的事情不会导致错误。如果再次运行查询,或者在没有查询和变量参数的情况下刷新浏览器,无论是否启用 Apollo 引擎,一切都可以正常工作。

当错误发生时,我可以从我的服务器日志中看到它正在尝试返回一个反应网页,其中包含一些用于从某处解码参数的 javascript,但我无法从哪里追踪 - 它没有达到我的任何代码。

标签: graphqlapollo-server

解决方案


阿波罗的人解决了这个问题。这是答案 - 我不应该在 engine.listen 选项中提到我的 graphIQL 端点。

引擎应该只放置在客户端和 GraphQL 服务器端点之间。尝试这样的配置:

engine.listen({
  port: port,
  expressApp: fidserver.app,
  graphqlPaths: ['/client'] // removed "graphql"
});

推荐阅读