在Azure上部署Apollo GraphQL服务器

时间:2018-08-28 15:25:34

标签: azure azure-web-sites graphql apollo-server

我试图在Azure上部署一个基于Javascript的示例Apollo GraphQL Server,但是它无法正常工作。尝试了以下方法

1) Create Azure Web App (Linux + Node Runtime) with ZipDeploy of tested app
2) Create Web App with Node Starter template and do ZipDeploy
3) Create Ubuntu VM and install Node+NPM followed by deploying app

在上述所有情况下,我都怀疑端口问题,但我无法发现正确的调整。使用以下教程创建了带有apollo服务器的示例NodeJS应用程序

https://www.apollographql.com/docs/apollo-server/getting-started.html

如果有人成功部署了apollo graphql服务器,请提出建议

3 个答案:

答案 0 :(得分:0)

Dockerize一个Node应用并部署到您喜欢的主机,同时暴露正确的端口。 Azure容器服务可能很昂贵,因此可能不是最佳选择。

答案 1 :(得分:0)

您应该能够像部署到Azure App Service中的任何其他node.js Web应用程序一样部署此node.js Web应用程序。因此,在不知道更多细节的情况下,我会说#1。

答案 2 :(得分:0)

选项 1 应该可以正常工作。正如 OP 所提到的,如果您不设置端口,则会遇到问题。 Azure 通过 process.env.PORT 公开端口供您设置。这是带有所需调整的 Apollo 示例:

const server = new ApolloServer({
  typeDefs,
  resolvers
});

// Azure will set `process.env.port` so use it (or 4000 for local dev)
server.listen({ port: process.env.PORT || 4000 }).then(({ url }) => {
  console.log(`?  Server ready at ${url}`);
});

当使用核心 ApolloServer 包中的 apollo-server 时,底层实现使用节点 http 包 (https://nodejs.org/api/http.html)。

我们传递给 ApolloServer.listen 方法的任何选项随后都会传递给 http.Server.listen (https://nodejs.org/api/http.html#http_server_listen)。

这里是接收到的选项传递到 httpServer 的地方:https://github.com/apollographql/apollo-server/blob/a3282a2d7df0c20d9e10b058defae835120fa5b1/packages/apollo-server/src/index.ts#L152