Graphql Apollo:订阅和查询解析器之间的区别?

Graphql Apollo:订阅和查询解析器之间的区别?,graphql,apollo,apollo-server,Graphql,Apollo,Apollo Server,此解析程序工作正常: const resolvers = { Query: { instant_message(_, args) { var ret = connectors.IM.findAll({ where: args }).then((res) => res.map((item) => item.dataValues)); return ret; } }, Subscrip

此解析程序工作正常:

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;
        }
    }

};

如果没有,需要什么区别?提前感谢所有人提供的任何信息。

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

// 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
subscription解析器,它使用发布子负载中的数据,并且根本不会命中数据库: