【发布时间】:2021-11-01 21:00:56
【问题描述】:
我正在尝试按照本教程 https://www.youtube.com/watch?v=I6ypD7qv3Z8&t=48972s 进行操作,但我一直在尝试使操场正常工作。
我通过“http://localhost:4000/graphql”进入操场,但不知何故我收到“无法访问服务器”错误。在网络检查器中,我看到“无法 POST /”404。
这是我的代码 (app.ts):
import { ApolloServer } from "apollo-server-express";
import { ApolloServerPluginLandingPageGraphQLPlayground } from "apollo-server-core";
import { buildSchema } from "type-graphql";
import { PORT } from "./constants";
import { HelloResolver } from "./graphql/resolvers";
export const main = async () => {
const app = express();
const apolloServer = new ApolloServer({
schema: await buildSchema({ resolvers: [HelloResolver], validate: false }),
plugins: [ApolloServerPluginLandingPageGraphQLPlayground],
});
await apolloServer.start();
apolloServer.applyMiddleware({ app });
app.listen(PORT, () => {
console.log(
`Server started on http://localhost:${PORT}${apolloServer.graphqlPath}`
);
});
};
我从 tut 中额外做的事情:
- 将 PORT 设为值为 4000 的变量
- 为旧学校操场添加了“ApolloServerPluginLandingPageGraphQLPlayground”(最新的也不起作用)
- 添加了“等待 apolloServer.start();”文档中指定的行,如果我不这样做,我会收到错误
我尝试过的事情:
- 使用文档中的 http 服务器内容 (https://www.apollographql.com/docs/apollo-server/integrations/middleware/#apollo-server-express) -> 相同问题
知道问题出在哪里吗?似乎 express 没有为 /graphql 路由创建 POST 端点。
编辑:如果我改变它,它会起作用:
apolloServer.applyMiddleware({ app });
对此:
apolloServer.applyMiddleware({ app, path: "/" });
【问题讨论】:
标签: node.js express graphql apollo-server