【问题标题】:Apollo GraphQL: Modifying Payload when Using withFilter?Apollo GraphQL:使用 withFilter 时修改有效负载?
【发布时间】:2020-09-13 17:51:29
【问题描述】:

我有一个使用 withFilter 的有效订阅:

   User_Presence_Subscription: {
        subscribe: withFilter(
            () => pubsub.asyncIterator(USER_PRESENCE_UPDATED_CHANNEL),
            (payload, args, context) => {
                if (typeof (payload) === 'undefined') {
                    return false;
                }
                const localUserId = (typeof(context) == 'undefined' || typeof(context.userId) == 'undefined') ? null : context.userId;
                const ids_to_watch = args.ids_to_watch;
                const usersWithUpdatedPresence = payload.User_Presence_Subscription;

                let result = false;
                console.log("User_Presence_Subscription - args == ", args, result);
                return result;
            }
        )
    }

我想在将负载发送到客户端之前对其进行修改。我尝试添加一个resolve 函数as shown in the docs

   User_Presence_Subscription: {
        resolve: (payload, args, context) => {
            debugger; <== NEVER ACTIVATES
            return {
                User_Presence_Subscription: payload,
            };
        },
        subscribe: withFilter(
            () => pubsub.asyncIterator(USER_PRESENCE_UPDATED_CHANNEL),
            (payload, args, context) => {
                if (typeof (payload) === 'undefined') {
                    return false;
                }
                const localUserId = (typeof(context) == 'undefined' || typeof(context.userId) == 'undefined') ? null : context.userId;
                const ids_to_watch = args.ids_to_watch;
                const usersWithUpdatedPresence = payload.User_Presence_Subscription;

                let result = false;
                console.log("User_Presence_Subscription - args == ", args, result);
                return result;
            }
        )
    }

...但是resolve 函数中的debugger 行永远不会被命中。

这里使用的正确语法是什么?

【问题讨论】:

    标签: graphql apollo apollo-server


    【解决方案1】:

    解决了。解析器没有被命中的唯一原因是在我的测试代码中我从withFilter 函数返回false。当它返回 true 时,解析器会按预期命中。

    【讨论】:

      猜你喜欢
      • 2018-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-15
      • 2022-11-03
      • 2019-03-13
      • 2020-11-15
      • 1970-01-01
      相关资源
      最近更新 更多