无法发布/ graphql

时间:2018-10-28 21:47:37

标签: javascript express graphql

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

1 个答案:

答案 0 :(得分:1)

您没有为/graphql设置路由-graphqlExpress路由同时使用graphiqlExpress/graphiql中间件。只需更新路径:

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