graphql - 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,但我无法从哪里追踪 - 它没有达到我的任何代码。
解决方案
阿波罗的人解决了这个问题。这是答案 - 我不应该在 engine.listen 选项中提到我的 graphIQL 端点。
引擎应该只放置在客户端和 GraphQL 服务器端点之间。尝试这样的配置:
engine.listen({
port: port,
expressApp: fidserver.app,
graphqlPaths: ['/client'] // removed "graphql"
});
推荐阅读
- java - Android项目的按钮根本不起作用
- reactjs - 根据每个 id 点击编辑 React 功能组件弹出窗口
- ios - Swift 5 - 根据键将变量设置为 plist 值
- sql - 如何使用存储过程从 t-sql 表中现有的两列计算一列的值?
- javascript - 在 IE 浏览器中填充下拉值
- svelte - sveltejs/kit 不接受“npm run start”
- c - 从函数写入时,链表状态保持不变
- django - 如何配置 Django(在 docker 上)通过 Postfix 发送邮件
- sql - 每组不同的 SQL 计数除以总数的不同计数
- python - `id()` 返回的值有什么限制