【问题标题】:Apollo: Difference Between Subscription vs Query Resolvers?Apollo:订阅与查询解析器之间的区别?
【发布时间】:2017-03-04 02:48:44
【问题描述】:

这个解析器工作正常:

const resolvers = {
    Query: {
        instant_message(_, args) {
            var ret = connectors.IM.findAll({ where: args }).then((res) => res.map((item) => item.dataValues));
            return ret;
        }
    },
    Subscription: {
    //[.....]
        },
    }

};

订阅解析器与查询解析器使用完全相同的代码是否有意义?即:

const resolvers = {
    Query: {
        instant_message(_, args) {
            var ret = connectors.IM.findAll({ where: args }).then((res) => res.map((item) => item.dataValues));
            return ret;
        }
    },
    Subscription: {
        instant_message(_, args) {
            var ret = connectors.IM.findAll({ where: args }).then((res) => res.map((item) => item.dataValues));
            return ret;
        }
    }

};

如果不是,需要什么区别?提前感谢大家提供任何信息。

【问题讨论】:

    标签: graphql apollo apollo-server


    【解决方案1】:

    是的,如果您希望在订阅结果中收到与在查询结果中收到的相同数据,那么采用相同的逻辑是有意义的。在这种情况下,分享实际的实现可能是有意义的:

    // Used in both query and subscription field
    function instant_message(root, args) {
      return connectors.IM.findAll({ where: args }).then((res) => res.map((item) => item.dataValues));
    }
    
    const resolvers = {
        Query: {
            instant_message,
        },
        Subscription: {
            instant_message,
        },
    };
    

    查询和订阅之间的最大区别在于订阅可能会从发布-订阅消息中接收附加信息。例如,在 GitHunt 示例中,我们有一个 commentAdded 订阅解析器,它使用来自 pub-sub 有效负载的数据并且根本不访问数据库:https://github.com/apollostack/GitHunt-API/blob/cc67a4506c31310b4ba8d811dda11d258c7d60d6/api/schema.js#L166-L171

    【讨论】:

      猜你喜欢
      • 2018-09-30
      • 2019-07-14
      • 2019-07-11
      • 2017-12-03
      • 2012-05-11
      • 2021-03-03
      • 2014-10-10
      • 2021-01-15
      • 2018-07-08
      相关资源
      最近更新 更多