首页 > 解决方案 > 在阿波罗 2 中更新 graphql 模式而不重新启动服务器

问题描述

我的项目有一种定义模式和解析器的声明方式,它在单独的存储库中维护。我的 graphql 服务器轮询此结果以查找对模式的更新。

使用 apollo-server-express@1,我可以直接访问 graphqlExpress 中间件,因此当架构更改时,我可以构建它的新实例并丢弃旧实例,就像这样

const { graphqlExpress } = require('apollo-server-express');

let api;

const constructAPI = () => {
    try {
        const newSchema = createSchema();
        api = graphqlExpress(({ headers }) => ({
            schema: newSchema,

        }));

        logger.info({ event: 'GRAPHQL_SCHEMA_UPDATED' });

};

schemaPoller.on('change', constructAPI);

module.exports = router => {

    // Note that we wrap the api controller in a function that passes
    // the original args through because a new api controller is generated
    // every time the schema changes. We can't pass express a direct
    // reference to the api controller on startup, or it will
    // never update the reference to point at the latest version of the
    // controller using the latest schema
    router
        .route('/')
        .get((...args) => api(...args))
        .post((...args) => api(...args));

    return router;
};

在 apollo-server-express@2 中,对中间件的访问被隐藏了,并且有 2 种新的、更具声明性的方式使用该库,乍一看,这两种方式都不兼容在不停止服务器的情况下更新模式,获取新模式并从新数据重新开始,这是我想避免的停机时间。

任何人都可以建议一种让这个设置与 apollo@2 一起使用的方法吗?

标签: graphqlapollographql-js

解决方案


推荐阅读