【问题标题】:How to update schema without restarting service如何在不重新启动服务的情况下更新架构
【发布时间】:2018-11-20 03:02:17
【问题描述】:


我的程序使用 apollo-server-koa 作为 graphql 服务,我想动态更新模式。例如:
- 用户上传数据到服务器时,schema也需要更新,但不需要重启服务。
我阅读了模式转换和模式拼接,但这些不符合我的要求。
任何人都可以给我一个建议。
非常感谢!

【问题讨论】:

  • 在特殊情况下,我想同时更新 typedef 和解析器
  • Apollo 不支持动态更新架构。如果您想这样做,您需要设置自己的中间件,重新创建每个请求的架构并自己调用graphqlexecute 函数。

标签: graphql apollo-server


【解决方案1】:

我使用 apollo-server-express@1.3.2 执行此操作。我还没有转换到 2,而且我没有做 koa,但我认为它大致相同。如果没有,希望它至少有所帮助。

const { graphqlExpress } = require('apollo-server-express')
let schema = loadRemoteSchemas() // this loads it on start-up

app.use(async (req, res) => {
  return {
    schema: await schema,
  }
}

app.use('refresh', async (req, res) => {
  // some middleware that does stuff and changes the `schema` pointer
})

【讨论】:

  • 我知道这一点,但我的项目使用 apollo server 2
  • 但是在您的解决方案中,我发现它的性能不好,因为服务器必须为每个请求重建架构
  • 不应该。 loadRemoteSchemas() 仅在以这种方式编写的启动时被调用。因为它是一个已经解决的承诺,所以它每次调用await schema 时都会使用预取的值。如果loadRemoteSchemas() 在中间件中,它最终会在每次请求时重建。
猜你喜欢
  • 1970-01-01
  • 2011-09-28
  • 1970-01-01
  • 1970-01-01
  • 2013-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多