Subscriptions, when combined with MongoDB watch, are not working. The example provided seems to be working but any kind of custom implementation isn't. I ran some tests and figured out that it's because subscription implementation expects a return to work but when you are actually having a live DB connection, you can't exactly return the function (usually a goroutine), which can be easily solved by using a channel instead.
Channels would be a great way to implement subscriptions, unlike how they are implemented currently.
I actually found a workaround but involved me messing with the generated code. I wrote a blog about it which you can find here: https://medium.freecodecamp.org/https-medium-com-anshap1719-graphql-subscriptions-with-go-gqlgen-and-mongodb-5e008fc46451
I am not sure if my solution is the best way to go, but I would love to figure out a solution and eventually contribute to gqlgen. Can you have a look at that?
@anshap1719 how did you prevent your custom code from being overwritten when the generated code is regenerated?
@maria-jabon I didn't actually. As I mentioned, the solution is hacky. I created this issue because I think we can use that as a base to actually add to the gqlgen project.
I see. It appears they don’t fix bugs. They haven’t fixed issues reported
over a year ago.
On Tue, Jun 4, 2019 at 9:33 PM Anshul Sanghi notifications@github.com
wrote:
@maria-jabon https://github.com/maria-jabon I didn't actually. As I
mentioned, the solution is hacky. I created this issue because I think we
can use that as a base to actually add to the gqlgen project.—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/99designs/gqlgen/issues/733?email_source=notifications&email_token=AG52OQQ767EY3IDHDQ36NLTPY462NA5CNFSM4HR7ATG2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODW6SITQ#issuecomment-498934862,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AG52OQSKELF3RMQFN5WNO7DPY462NANCNFSM4HR7ATGQ
.>
Maria
I see. Thanks for letting me know.
Subscriptions, when combined with MongoDB watch, are not working.
Why not? What did you try?
Channels would be a great way to implement subscriptions, unlike how they are implemented currently.
We do use channels for subscriptions.
I'm going to close this. If you would like to propose a change create an issue which clearly states what you want, dont make me go read a long poorly formatted blog post.
Sorry about that poor formatting. I wrote the blog on Medium with proper formatting and everything then FreeCodeCamp moved out of medium and made it shit on their blog. But anyways, all I was saying was the current channels implementation doesn't make sense (and I might be wrong but I have spent hours on this thing already).
The current implementation requires the resolver function to return a channel. But returning from the function would just end the function there and return an empty channel since the values are added later on to the channel from the database. A better approach would be to pass a reference to a channel to the resolver function which would allow the resolver function to add values to channel later on and as the values are added, they can be received on the parent function (gqlgen generated code) and be passed on to the frontend via WebSocket. Basically, create a channel in the generated code itself and pass a pointer to it to the resolver function for lazily adding values.
Oh and trust me, I tried a whole lot of things before deciding to edit the generated code. From goroutines within resolvers that get passed a reference to the channel which is being returned from resolver to the method shown in your example. Nothing seemed to work and every time the issue was that channel was being closed before it received any value from the MongoDB watch function.
Did you see the chat example? https://github.com/99designs/gqlgen/blob/master/example/chat/resolvers.go#L86-L110
The chat example works, but thesame procedure is not applicable for a mongo database. I read @anshap1719 blog. It does work, but is hacky, so whenever I want to regenerate from schema, I have do it all over again for all subscription resolvers.
Please!
We need a fix for this!!
Most helpful comment
The chat example works, but thesame procedure is not applicable for a mongo database. I read @anshap1719 blog. It does work, but is hacky, so whenever I want to regenerate from schema, I have do it all over again for all subscription resolvers.
Please!
We need a fix for this!!