【问题标题】:Apollo Server Express - Playground cannot be reachedApollo Server Express - 无法到达游乐场
【发布时间】: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();”文档中指定的行,如果我不这样做,我会收到错误

我尝试过的事情:

知道问题出在哪里吗?似乎 express 没有为 /graphql 路由创建 POST 端点。

编辑:如果我改变它,它会起作用: apolloServer.applyMiddleware({ app }); 对此: apolloServer.applyMiddleware({ app, path: "/" });

【问题讨论】:

    标签: node.js express graphql apollo-server


    【解决方案1】:

    尝试使用 127.0.0.1 而不是 localhost。它对我有用。此外,如果您缓存了仍想使用的查询和突变,从 127.0.0.1 切换回 localhost 将使 localhost 再次工作。

    【讨论】:

      【解决方案2】:

      我找到了答案here!有帮助。一定要看看!

      【讨论】:

      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
      猜你喜欢
      • 2021-12-05
      • 2020-01-05
      • 2020-01-08
      • 2020-12-17
      • 2019-09-10
      • 1970-01-01
      • 2021-10-01
      • 2019-12-19
      • 1970-01-01
      相关资源
      最近更新 更多