简体   繁体   English

如何在 Heroku 上部署 GraphQL API 及其 React 客户端?

[英]How can I deploy a GraphQL API and its React Client on Heroku?

I have created an application with a GraphQL API and a React Client.我用 GraphQL API 和 React 客户端创建了一个应用程序。 My project's structure is as follows:我的项目结构如下:

project
│   README.md    
│
└───server
│   │   package.json
│   │   
│   └───src
│       │   index.ts
│       │
│       └───entity/migration/resolver/etc.
│       │   ...
│   
└───client
│   │   package.json
│   │
│   └───public
│   │   │   index.html
|   |   |   ...
│   │   │
│   └───src
│       │   index.js
│       │
│       └───components
│       │   ...

I'd like to deploy this entire app to Heroku, but I'm not quite sure how to do so.我想将整个应用程序部署到 Heroku,但我不太确定该怎么做。 On my local machine, I have my server running on localhost:4000 and my client running on localhost:3000 , and my client makes requests to my server using @apollo/client .在我的本地机器上,我的服务器在localhost:4000上运行,我的客户端在localhost:3000上运行,我的客户端使用@apollo/client向我的服务器发出请求。 Would I need to deploy two separate apps to Heroku, or is there a way to deploy these both as one app?我需要将两个单独的应用程序部署到 Heroku,还是有办法将这两个应用程序部署为一个应用程序?

Here's the code from my server/src/index.ts :这是来自我的server/src/index.ts的代码:

import dotenv from "dotenv"
import "reflect-metadata"
import { ApolloServer } from "apollo-server"
import { buildSchema } from "type-graphql"
import { createConnection } from "typeorm"

(async () => {  
  await createConnection()  

  const apolloServer = new ApolloServer({
    schema: await buildSchema({
      resolvers: [`${__dirname}/resolver/*.ts`],
    })
  })

  const port = process.env.PORT || 4000
  apolloServer.listen(port, () => {
    console.log(`Server started on port: ${port}`)
  })
})()

And here's the code from my client/src/index.js :这是来自我的client/src/index.js的代码:

import React from "react"
import ReactDOM from "react-dom"
import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client"

import App from "./components/App"

const client = new ApolloClient({
    uri: process.env.ENDPOINT || "http://localhost:4000",
    cache: new InMemoryCache()
})

ReactDOM.render(<ApolloProvider client={client}><App /></ApolloProvider>, document.getElementById("root"))

It's a long topic to be covered that's exactly what I have started developing recently, the entire source code is available on Github at https://github.com/mdarif/project-management这是一个很长的话题,这正是我最近开始开发的内容,整个源代码可在 https 的 Github 上获得://github.com/mdarif/project-management

This is MERN app where I have deployed Server/Client and MongoDB all in once on Heroku, there is also a Dockerize Version of the same MERN app available using Github Actions.这是 MERN 应用程序,我在 Heroku 上一次性部署了服务器/客户端和 MongoDB,还有一个使用 Github 操作的相同 MERN 应用程序的 Dockerize 版本。

Below important things should be taken care of:应注意以下重要事项:

  • Environment variables for Dev and Config Vars on Heroku Heroku 上的 Dev 和 Config Vars 的环境变量
  • Add Handler to Client Build in server/index.js or server.js将处理程序添加到server/index.js or server.js中的客户端构建
// Serve Frontend
if (process.env.NODE_ENV === 'production') {
  // Set build folder as static folder
  app.use(express.static(path.join(__dirname, '../client/build')))

  app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, '../client/build/index.html'))
  })
} else {
  app.get('/', (req, res) => {
    res.status(200).json({ message: 'Welcome to the Project...' })
  })
}
  • Add Scripts to package.json (server)将脚本添加到 package.json(服务器)
  "scripts": {
    ...
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
  },
  • Make sure to test your client & server build separately确保分别测试您的客户端和服务器构建
  • Connecting Heroku to GitHub将 Heroku 连接到 GitHub
    • Go to heroku.com Go 至 heroku.com
    • Choose Connect to GitHub as the deployment method部署方式选择 Connect to GitHub
    • If your GitHub account isn't connected to Heroku, do so如果您的 GitHub 帐户未连接到 Heroku,请执行此操作
    • Search for, and choose the project repository we created in the previous step搜索并选择我们在上一步中创建的项目存储库
    • Enable automatic deployments and select the deployment branch启用自动部署和 select 部署分支
    • Click on Deploy Branch点击部署分支

And that's it.就是这样。 You're done.你完成了。 Your app would now be deployed, If you face errors.如果您遇到错误,您的应用程序现在将被部署。 you can debug them using Heroku's logs.你可以使用 Heroku 的日志来调试它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM