我正在尝试设置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`);
    }
  });

最佳答案

您没有为/graphql设置路由-graphqlExpressgraphiqlExpress中间件都与/graphiql路由一起使用。只需更新路径:

app.use(
    '/graphql',
    graphqlExpress(req => ({
      schema,
      context: {
        user: req.user
      }
    })),
);

09-27 20:44