[英]GraphQL Federation Nodemon breaks Gateway on Restart
我正在努力设置我的 GraphQL Federation 开发环境。 我有一些服务,我希望服务器在文件更改时自动重新启动。 我正在使用 Typescript,因此 Typescript 必须先转换到 dist 文件夹,然后 nodemon 应该重新启动发生更改的服务。
我在我的 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"
这在我运行npm run dev
时开始工作 - 帐户和配置文件服务启动,一旦它们运行,网关就会启动。 但是,一旦 Typescript 完成转译,nodemon 会重新启动所有服务和网关,因为 dist 文件夹中的文件已更改。
这是问题所在。 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
每当 nodemon 执行重启时,我不知道如何告诉 nodemon 等到每个服务都重启!
因为现在等待仅适用于npm run dev
的第一次初始执行。 之后,对于每次更改,nodemon 负责重新启动并且不关心等待。
这是整个火车残骸:
$ 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/
我希望这是可以理解的,我期待有关如何解决此问题的任何建议。
谢谢,马库斯
尝试在您的代码中而不是在 package.json 脚本中使用等待。 确保使用等待代码包装您的网关服务器。 例子:
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.