gpt4 book ai didi

javascript - 无法发布/graphql

转载 作者:行者123 更新时间:2023-12-03 00:51:08 24 4
gpt4 key购买 nike

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

关于javascript - 无法发布/graphql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53036387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com