我正在尝试设置graphiql API,但是我在控制台的屏幕和网络选项卡上收到此错误,我不明白这是什么错误,因为我在其他项目中运行的是相同的样板代码。我尝试删除httpServer,尝试添加cors,检查了几乎所有与此错误相关的信息,但无法弄清楚,请建议Cannot POST /graphql
import express from 'express';
import { createServer } from 'http';
import bodyParser from 'body-parser';
import { graphiqlExpress, graphqlExpress } from 'apollo-server-express';
import { makeExecutableSchema } from 'graphql-tools';
import typeDefs from './graphql/schema';
import resolvers from './graphql';
const schema = makeExecutableSchema({
typeDefs,
resolvers
})
const app = express();
app.use(bodyParser.json());
//issue is here
app.use(
'/graphiql',
graphiqlExpress({
endpointURL: "/graphql"
})
);
app.use(
'/graphiql',
graphqlExpress(req => ({
schema,
context: {
user: req.user
}
})),
);
const httpServer = createServer(app);
httpServer.listen(3000, err => {
if (err) {
console.error(err);
} else {
console.log(`App listen to port 3000`);
}
});
答案 0 :(得分:1)
您没有为/graphql
设置路由-graphqlExpress
路由同时使用graphiqlExpress
和/graphiql
中间件。只需更新路径:
app.use(
'/graphql',
graphqlExpress(req => ({
schema,
context: {
user: req.user
}
})),
);