Search code examples
graphqlapolloapollo-server

Apollo GraphQL: Modifying Payload when Using withFilter?


I have a working subscription that uses 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;
            }
        )
    }

I'd like to modify the payload before sending it to the client. I tried adding a resolve function 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;
            }
        )
    }

...but the debugger line in the resolve function never gets hit.

What's the correct syntax to use here?


Solution

  • Solved. The only reason the resolver wasn't being hit was that in my test code I was returning false from the withFilter function. When it returns true the resolver is hit as expected.