graphql icon indicating copy to clipboard operation
graphql copied to clipboard

Subscription filter not working

Open cosmin-petrescu opened this issue 7 years ago • 24 comments

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.

cosmin-petrescu avatar Mar 19 '19 12:03 cosmin-petrescu

Would you be interested in creating a PR? :)

kamilmysliwiec avatar Mar 19 '19 12:03 kamilmysliwiec

Should be fixed in 6.0.2 :) Let me know if you face any issues

kamilmysliwiec avatar Mar 20 '19 13:03 kamilmysliwiec

Hi, I installed 6.0.2 and have the same code as above. the filter function doesn't run and I don't see any error tho

raymclee avatar Mar 27 '19 16:03 raymclee

I tried it too, and the initial error is not present anymore, but the filter method is not called. The result is a subscription without filter.

cosmin-petrescu avatar Mar 29 '19 04:03 cosmin-petrescu

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

raymclee avatar Mar 30 '19 13:03 raymclee

Same problem, no filter method called, but resolve function is ok any progress?

ThinkontrolSY avatar Apr 07 '19 07:04 ThinkontrolSY

Same problem :(

realtomaszkula avatar Apr 07 '19 08:04 realtomaszkula

@ 6.0.2 and issue is still there

VRspace4 avatar Apr 11 '19 01:04 VRspace4

Same

sygeman avatar Apr 11 '19 09:04 sygeman

Same problem

PainteR avatar Apr 11 '19 13:04 PainteR

same @6.0.5

cncolder avatar Apr 15 '19 09:04 cncolder

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

cncolder avatar Apr 15 '19 14:04 cncolder

Same issue with 6.1.1 and a code-first resolver.

@Subscription(type => PairStatus, {
    filter: (payload: PairStatus, variables: any, context: any) => {
      console.log(variables);
      console.log(payload);
      console.log(context);
      return payload.pairCode === variables.code;
    },
    resolve: value => {
      console.log(value);
      return value as any;
    }
  })
  pairStatus(@Args({ type: () => String, name: 'code' }) code: string) {
    return this.pubSub.asyncIterator('devicePaired');
  }

Only the console.log inside the resolve function prints and the value seems to be the context, not the value that is being resolved. Without the resolve function, the subscription does not resolve when I publish to the PubSub.

kirkbrauer avatar Apr 30 '19 01:04 kirkbrauer

I have the same problem on my project.

phoenix741 avatar May 11 '19 09:05 phoenix741

It should be fixed in the @next version ($ npm i @nestjs/graphql@next). You will also have to update @nestjs/core to 6.2.0 ($ npm i @nestjs/[email protected]). Please, let me know if you face any issue.

kamilmysliwiec avatar May 11 '19 12:05 kamilmysliwiec

@kamilmysliwiec Unfortunately, subscriptions in @next version are not working at all.

tverdohleb avatar May 13 '19 14:05 tverdohleb

Thanks for reporting @tverdohleb! Could you check 6.2.0-next.4?

kamilmysliwiec avatar May 13 '19 14:05 kamilmysliwiec

@kamilmysliwiec, thanks for the update! Subscriptions basic functionality work again, but the context in the filter method is still undefined (at the same time, resolve method gots the right values).

tverdohleb avatar May 13 '19 17:05 tverdohleb

I published 6.2.0-next.5 with a few fixes. Feel free to test whenever you have some spare time :)

kamilmysliwiec avatar May 13 '19 17:05 kamilmysliwiec

Published as 6.2.0 ($ npm i @nestjs/graphql@latest). You will also have to update @nestjs/core to 6.2.0 ($ npm i @nestjs/core@latest)

kamilmysliwiec avatar May 14 '19 09:05 kamilmysliwiec