简体   繁体   English

GraphQL Federation Nodemon 在重启时中断网关

[英]GraphQL Federation Nodemon breaks Gateway on Restart

I'm struggeling to setup my GraphQL Federation development environment.我正在努力设置我的 GraphQL Federation 开发环境。 I have a few services and I want the server to automatically restart whenever a file changes.我有一些服务,我希望服务器在文件更改时自动重新启动。 I am using Typescript, so Typescript has to transpile first into the dist folder and then nodemon should restart the service where the change happended.我正在使用 Typescript,因此 Typescript 必须先转换到 dist 文件夹,然后 nodemon 应该重新启动发生更改的服务。

I created the following setup in my package.json file:我在我的 package.json 文件中创建了以下设置:

"dev": "concurrently -k \"tsc -w\" \"npm:dev:*\"",
"dev:accounts": "nodemon -r dotenv/config -r esm --watch ./dist/services/accounts ./dist/services/accounts/index.js",
"dev:profiles": "nodemon -r dotenv/config -r esm --watch ./dist/services/profiles ./dist/services/profiles/index.js",
"dev:gateway": "wait-on tcp:4001 tcp:4002 && nodemon -r dotenv/config -r esm ./dist/index.js"

This works in the beginning when I run npm run dev - the accounts and profiles services start and once they are running, the gateway starts.这在我运行npm run dev时开始工作 - 帐户和配置文件服务启动,一旦它们运行,网关就会启动。 However, as soon as Typescript is finished transpiling, nodemon restarts all services and the gateway, because the files in the dist folder have been changed.但是,一旦 Typescript 完成转译,nodemon 会重新启动所有服务和网关,因为 dist 文件夹中的文件已更改。

Here is the issue.这是问题所在。 Nodemon restarts the services and gateway independently and the gateway restarts faster, which means that the gateway is running before the services, resulting in the following error: nodemon独立重启服务和网关,网关重启速度较快,说明网关先于服务运行,导致如下错误:

[dev:gateway] Error checking for changes to service definitions: Couldn't load service definitions for "profiles" at http://localhost:4002: request to http://localhost:4002/ failed, reason: connect ECONNREFUSED 127.0.0.1:4002    
[dev:gateway] This data graph is missing a valid configuration. Couldn't load service definitions for "profiles" at http://localhost:4002: request to http://localhost:4002/ failed, reason: connect ECONNREFUSED 127.0.0.1:4002

I couldn't figure out how to tell nodemon to wait until every services has restarted whenever nodemon is performing a restart!每当 nodemon 执行重启时,我不知道如何告诉 nodemon 等到每个服务都重启!

Because right now the wait-on only works on the first inital execution of npm run dev .因为现在等待仅适用于npm run dev的第一次初始执行。 After that for every change nodemon is responsible for the restarts and does not care about wait-on.之后,对于每次更改,nodemon 负责重新启动并且不关心等待。


Here is the entire trainwreck:这是整个火车残骸:

$ npm run dev $ npm 运行开发

> my-app@1.0.0 dev
> concurrently -k "tsc -w" "npm:dev:*"

17:08:48 - Starting compilation in watch mode...

[dev:accounts] > my-app@1.0.0 dev:accounts
[dev:accounts] > nodemon -r dotenv/config -r esm --watch ./dist/services/accounts ./dist/services/accounts/index.js

[dev:gateway] > my-app@1.0.0 dev:gateway
[dev:gateway] > wait-on tcp:4001 tcp:4002 && nodemon -r dotenv/config -r esm ./dist/index.js

[dev:profiles] > my-app@1.0.0 dev:profiles
[dev:profiles] > nodemon -r dotenv/config -r esm --watch ./dist/services/profiles ./dist/services/profiles/index.js

[dev:accounts] [nodemon] 2.0.7
[dev:accounts] [nodemon] to restart at any time, enter `rs`
[dev:accounts] [nodemon] watching path(s): dist/services/accounts/**/*
[dev:accounts] [nodemon] watching extensions: js,mjs,json
[dev:accounts] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/accounts/index.js`

[dev:profiles] [nodemon] 2.0.7
[dev:profiles] [nodemon] to restart at any time, enter `rs`
[dev:profiles] [nodemon] watching path(s): dist/services/profiles/**/*
[dev:profiles] [nodemon] watching extensions: js,mjs,json
[dev:profiles] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/profiles/index.js`

[dev:accounts] Accounts service ready at http://localhost:4001/
[dev:profiles] Profiles service ready at http://localhost:4002/

[dev:gateway] [nodemon] 2.0.7
[dev:gateway] [nodemon] to restart at any time, enter `rs`
[dev:gateway] [nodemon] watching path(s): *.*
[dev:gateway] [nodemon] watching extensions: js,mjs,json
[dev:gateway] [nodemon] starting `node -r dotenv/config -r esm ./dist/index.js`

[dev:gateway] 🚀 Server started at http://localhost:4000/graphql

[dev:accounts] > my-app@1.0.0 dev:accounts
[dev:accounts] > nodemon -r dotenv/config -r esm --watch ./dist/services/accounts ./dist/services/accounts/index.js

[dev:gateway] > my-app@1.0.0 dev:gateway
[dev:gateway] > wait-on tcp:4001 tcp:4002 && nodemon -r dotenv/config -r esm ./dist/index.js

[dev:profiles] > my-app@1.0.0 dev:profiles
[dev:profiles] > nodemon -r dotenv/config -r esm --watch ./dist/services/profiles ./dist/services/profiles/index.js

[dev:accounts] [nodemon] 2.0.7
[dev:accounts] [nodemon] to restart at any time, enter `rs`
[dev:accounts] [nodemon] watching path(s): dist/services/accounts/**/*
[dev:accounts] [nodemon] watching extensions: js,mjs,json
[dev:accounts] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/accounts/index.js`

[dev:profiles] [nodemon] 2.0.7
[dev:profiles] [nodemon] to restart at any time, enter `rs`
[dev:profiles] [nodemon] watching path(s): dist/services/profiles/**/*
[dev:profiles] [nodemon] watching extensions: js,mjs,json
[dev:profiles] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/profiles/index.js`

[dev:accounts] Accounts service ready at http://localhost:4001/

[dev:profiles] Profiles service ready at http://localhost:4002/

[dev:gateway] [nodemon] 2.0.7
[dev:gateway] [nodemon] to restart at any time, enter `rs`
[dev:gateway] [nodemon] watching path(s): *.*
[dev:gateway] [nodemon] watching extensions: js,mjs,json
[dev:gateway] [nodemon] starting `node -r dotenv/config -r esm ./dist/index.js`

[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...

Typescript doing stuff...
[0] 17:08:57 - Found 226 errors. Watching for file changes.

[dev:profiles] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] starting `node -r dotenv/config -r esm ./dist/index.js`
[dev:profiles] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/accounts/index.js`
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/profiles/index.js`
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/profiles/index.js`
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...

[dev:gateway] [nodemon] starting `node -r dotenv/config -r esm ./dist/index.js`

[dev:accounts] Accounts service ready at http://localhost:4001/

[dev:gateway] 🚀 Server started at http://localhost:4000/graphql

[dev:gateway] Error checking for changes to service definitions: Couldn't load service definitions for "profiles" at http://localhost:4002: request to http://localhost:4002/ failed, reason: connect ECONNREFUSED 127.0.0.1:4002

[dev:gateway] This data graph is missing a valid configuration. Couldn't load service definitions for "profiles" at http://localhost:4002: request to http://localhost:4002/ failed, reason: connect ECONNREFUSED 127.0.0.1:4002

[dev:profiles] Profiles service ready at http://localhost:4002/

I hope this is understandable and I'm looking forward to any suggestions on how I could fix this.我希望这是可以理解的,我期待有关如何解决此问题的任何建议。

Thanks, Markus谢谢,马库斯

Try using wait-on in your code rather than in your package.json scripts.尝试在您的代码中而不是在 package.json 脚本中使用等待。 Make sure to wrap your gateway server with the wait-on code.确保使用等待代码包装您的网关服务器。 Example:例子:

import waitOn from 'wait-on'
import app from './config/app'
import server from './config/apollo'

const port = process.env.PORT

const options = {
  resources: ['tcp:4001'],
}

waitOn(options)
  .then(() => {
    server.applyMiddleware({ app })
    app.listen({ port }, () => {
      console.log(
        `Server ready at http://localhost:${port}${server.graphqlPath}`
      )
    })
  })
  .catch((err) => {
    console.error('ERR:', err)
  })

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

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