部署Apollo Express服务器到Heroku遇到问题:“GET查询丢失”。

6
我使用Apollo Express编写了一个简单的GraphQL服务器,并将其部署到Heroku。在处理Procfiles等问题后,它构建成功。
当我访问主URL https://limitless-atoll-59109.herokuapp.com 时,出现错误:

Cannot GET /

好吧,我想Express服务器必须只是在graphql端点上寻找get请求。但是,当我访问https://limitless-atoll-59109.herokuapp.com/graphql 时,出现以下错误:

GET查询丢失。

我需要在URL中包含端口吗?代码中已正确设置端口。
const PORT = process.env.PORT || 4000

  app.listen({port: PORT}, () =>

  console.log(`Server ready at http://localhost:${PORT}${server.graphqlPath}`)
  );

但我认为在访问Heroku服务器时不需要包含它,对吗?

值得一提的是,这是错误日志中的错误:

019-05-08T17:07:41.492327+00:00 heroku[router]: at=info method=GET path="/graphql" host=limitless-atoll-59109.herokuapp.com request_id=b6171835-aac4-4b45-8a7b-daebbb3167ed fwd="139.47.21.74" dyno=web.1 connect=0ms service=900ms status=400 bytes=196 protocol=https

感谢任何帮助!

2个回答

22
答案是带有“/graphql”结尾的url。但是,当你从浏览器访问该url时,它会尝试加载playground并失败,因为在生产环境中,默认情况下禁用playground。不过,你可以从你的客户端应用程序对该url发起graphql调用,这样就可以正常工作了。

14

如果您正在使用Apollo服务器,您只需在生产环境中启用它:

const server = new ApolloServer({
  typeDefs,
  resolvers,
  introspection: true,
  playground: true,
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接