question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Subscription filter not working

See original GitHub issue

I’m submitting a…


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

For the following simple example:

  @Subscription(returns => Object, {
    filter: (payload: any, variables: any) => {
      console.log('payload', payload);
      console.log('variables', variables);
      return true;
    },
  })
  objectAdded() {
    return pubSub.asyncIterator<Station>('objectAdded');
  }

Got the following result:

TypeError: asyncIterator.return is not a function

Because in https://github.com/nestjs/graphql/blob/master/lib/services/resolvers-explorer.service.ts -> createSubscriptionMetadata, when filter is present the method withFilter is called with a promise as parameter instead of AsyncIterator.

Expected behavior

No error should occur.

Minimal reproduction of the problem with instructions

  @Subscription(returns => Object, {
    filter: (payload: any, variables: any) => {
      console.log('payload', payload);
      console.log('variables', variables);
      return true;
    },
  })
  objectAdded() {
    return pubSub.asyncIterator<Station>('objectAdded');
  }

What is the motivation / use case for changing the behavior?

Cannot update the project from 5.x to 6.x.

Environment


Nest version: 6.0.1

 
For Tooling issues:
- Node version: v10.15.0  
- Platform: Mac  

Others:


The project is a NX one.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:24 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
cncoldercommented, Apr 15, 2019

A temp resolution for Code first style subscription. You need an asyncInterator filter function:

@Subscription(returns => Media)
mediaChanged(@Context() ctx) {
    return asyncFilter(
        pubSub.asyncIterator('mediaChanged'),
        payload => payload.mediaChanged.userId === ctx.session.id
    )
}

asyncFilter implement below (pay attention it’s NOT same as withFilter from ‘graphql-subscriptions’):

import { $$asyncIterator } from 'iterall'

export type FilterFn<T> = (rootValue?: T) => boolean | Promise<boolean>

export const asyncFilter = <T = any>(asyncIterator: AsyncIterator<T>, filterFn: FilterFn<T>): AsyncIterator<T> => {
    const getNextPromise = () => {
        return asyncIterator.next().then(payload => {
            if (payload.done === true) {
                return payload
            }

            return Promise.resolve(filterFn(payload.value))
                .catch(() => false)
                .then(filterResult => {
                    if (filterResult === true) {
                        return payload
                    }

                    // Skip the current value and wait for the next one
                    return getNextPromise()
                })
        })
    }

    return {
        next() {
            return getNextPromise()
        },
        return() {
            return asyncIterator.return()
        },
        throw(error) {
            return asyncIterator.throw(error)
        },
        [$$asyncIterator]() {
            return this
        },
    }
}
3reactions
raymcleecommented, Mar 30, 2019

i got the same result #182 the filter function doesn’t run

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot subscription filter policy issues in Amazon SNS
Subscription filter policies can filter message attributes only, not the message body. If the MessageAttributeValue is left empty on a ...
Read more >
Amazon SQS subscription filters not working · Issue #1761
The goal is to setup SQS/SNS to have a consumer that only processes message that fit a certain criteria. I can find examples...
Read more >
AWS SNS Subscription Filter policy checking a key in ...
I would like to use a filter subscription policy that ignores the second type but passes the first type to the subscriber. So...
Read more >
Search Subscription filter not working with eCommerce ...
The Subscription filter in Search preferences is not associated with the eCommerce Subscription functionality. Instead, this filter is ...
Read more >
Subscriptions filter in AAD and Azure portal are not in sync
While in the meantime, my account has Owner access to various Azure Subscriptions. Well, after lot of troubleshooting with Microsoft Support, it ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found