-
Notifications
You must be signed in to change notification settings - Fork 574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
YogaDriver for NestJS: support nestjs/graphql's @Subscription filter
function
#2978
Comments
Hey!
|
I guess this is expected, but if we just add this implementation from apollo driver and copy createAsyncIterator it will be possible to make it work with some tweaking or config options both ways
|
Hi Mantas, import { PubSub } from 'graphql-subscriptions';
@Resolver()
export class TestSubscriptionsResolver {
private readonly pubSub = new PubSub();
constructor() {
setInterval(() => {
this.pubSub.publish('now', Math.floor(Date.now() / 1_000));
}, 1000);
}
@Subscription(() => TestSubscriptionOutput, {
filter: (payload) => payload % 2 === 0,
resolve: (payload) => ({ countdown: payload }),
})
async testSubscription(@Args('input') input: number) {
return this.pubSub.asyncIterator('now');
}
} |
Yeah it works with the snippet that i sent from apollo export const OrderUpdatedSubscriptionName = 'orderUpdated'
export interface OrderUpdatedSubscriptionPayload {
[OrderUpdatedSubscriptionName]: OrderUpdatedEventPayload;
}
@Subscription(() => OrderUpdatedPayload, {
filter: ({ orderUpdated }: OrderUpdatedSubscriptionPayload, _, { req: { user } }) => {
console.log(11111, orderUpdated.order)
return orderUpdated.order.userId === user.id
},
resolve: payload => {
console.log(2222, payload)
return payload
}
})
async [OrderUpdatedSubscriptionName] () {
return this.pubSubService.asyncIterator(OrderUpdatedSubscriptionName)
}
// Publishing looks like
this.pubSubService.publish<OrderUpdatedSubscriptionPayload>(OrderUpdatedSubscriptionName, {
[OrderUpdatedSubscriptionName]: event.payload
}) |
Now that Im looking at the code, maybe its related to the returned payload format
|
Yes, what you take as input of Same as in envelop/Yoga when you use query/mutation/subscription resolvers, except that codegen |
Yeah, so by documentation:
So this patch should work with structure that i provided, because thats what graphql-subscription expects. Do you think we can find a solution to support both built in and graphql-subscription? |
It already works, it's agnostic to graphql-subscriptions PubSub class. this.pubSubService.publish<OrderUpdatedSubscriptionPayload>(OrderUpdatedSubscriptionName, {
[OrderUpdatedSubscriptionName]: event.payload
}) What you should do is this.pubSubService.publish<OrderUpdatedSubscriptionPayload>(OrderUpdatedSubscriptionName, event.payload); The payload is already identified by the name of the event used to publish, subscribe and get an asyncIterator. |
Yeah i got this before, while analysing the error, but the problem is that if we make this then it goes agains nestjs documentation as it says to structure the payload nested that way |
@gthau Lol, i think i figured it out... I was using pipe, filter from rxjs instead of yoga... |
Imo this part of the documentation is bad. It mixes everything with no respect for separation of concerns. It doesn't make sense for the mutation to need to know about the subscription name in order to wrap the event, that's contrary to the goal of using a pubsub/bus/etc. Moreover the event might be used for several subscriptions, etc. Also it confuses about how the resolvers' chain works. Returning exactly the exact shape from the operation resolver completely ignores the fact that type resolvers might be used to recursively produce the correct shape. But it's not the topic of this PR 😄 |
This issue was resolved with #2977 and released with in |
PR submitted: #2977
Is your feature request related to a problem? Please describe.
NestJS GraphQL declares Subscriptions' resolvers using the
@Subscription
decorator. This decorator accepts an options object with aresolve
andfilter
functions. Currently the YogaDriver for NestJS doesn't support thefilter
method.It should be supported to ensure feature parity with the Apollo and Mercurius driver and to make sure the NestJS main documentation is relevant to devs choosing to use the YogaDriver.
Describe the solution you'd like
Add support for the
filter
function.Describe alternatives you've considered
Two alternatives:
pipe
andfilter
The text was updated successfully, but these errors were encountered: