graphql icon indicating copy to clipboard operation
graphql copied to clipboard

Subscriptions aren't adaptable enough.

Open Yomanz opened this issue 7 years ago • 14 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 behaviour

It is not possible to use this in the subscription resolve function. This makes it incredibly hard to use services to retrieve information from given information.

https://objects.black-out.xyz/a0f6d2b https://objects.black-out.xyz/200c4ce Some screenshots of the current behaviour ^

The code I had when using type-graphql https://objects.black-out.xyz/3172643

Expected behavior

The ability to use this or services in the subscription resolve function. type-graphql didn't force an async iterator to be returned so it was very easy to use add the equivalent of a resolve function in the actual function.

Minimal reproduction of the problem with instructions

  1. Create a resolver.
  2. Inject a service into the resolver.
  3. Create a subscription and attempt to use the service in the resolve function.

It could look something like https://objects.black-out.xyz/a4cef78

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

The current behaviour isn't adaptable enough to build bigger graphql apps compared to things like type-graphql I did spend quite some time with a member of the discord and neither of us could think of any workarounds. There might be a nice way to do it with DI but I'm not aware.

Environment


Nest version: 6.2.4
Nest GraphQL version: 6.4.2

Yomanz avatar Jul 26 '19 18:07 Yomanz

@Yomanz ! Maybe use function instead of arrow function ?

thomrick avatar Jul 27 '19 10:07 thomrick

@Yomanz ! Maybe use function instead of arrow function ?

I have sadly already tried that :(

Yomanz avatar Jul 27 '19 18:07 Yomanz

Let's try this :

class Message {

  @Subscription(returns => String, {
    resolve: async (message: any) => {
      ...
      const ret = Message.prototype.messageService.fetchMessage(...);
      return Message.resolve(ret[0]);
    }
  })
  public async message(...) {
     ...
}

thomrick avatar Jul 27 '19 18:07 thomrick

https://objects.black-out.xyz/deb891f https://i-am.bigno.se/b33b49f.png

Sadly, this doesn't work

Yomanz avatar Jul 28 '19 14:07 Yomanz

I think it's normal that you cannot use class properties / methods in a decorator configuration. You may find a way to get the message in you Subscription decorated method params to apply your fetchMessage process then emit the result to the pubsub...

thomrick avatar Jul 28 '19 18:07 thomrick

The message it receives contains information used to get further information. This is really the last part of my application and I don't know what to do at this point.

Yomanz avatar Jul 28 '19 19:07 Yomanz

Let me check if there is anything that we can do to fix your issue.

kamilmysliwiec avatar Jul 29 '19 08:07 kamilmysliwiec

Thanks very much!

Yomanz avatar Jul 29 '19 09:07 Yomanz

hey - im experiencing the same issue right now, is there any eta for when this will be fixed

mason-rogers avatar Aug 01 '19 02:08 mason-rogers

Let me check if there is anything that we can do to fix your issue.

Hey @kamilmysliwiec, any luck?

Yomanz avatar Aug 02 '19 16:08 Yomanz

up

ogheo avatar Sep 01 '19 13:09 ogheo

Update @nestjs/graphql to 6.5.0 version. Once package is installed, you can use something like this:

@Resolver(of => Model)
class Message {
  @Subscription(returns => String, {
    resolve(this: Message, message: any) => {
      // this will point at an instance of `Message`
      // hence, you can use all injected dependencies here
    }
  })
 async onMessage(...) {}
}

kamilmysliwiec avatar Sep 02 '19 12:09 kamilmysliwiec

Thanks

Yomanz avatar Sep 03 '19 10:09 Yomanz

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Apr 25 '20 09:04 lock[bot]