Gqlgen: Can't find docs/examples of Subscriptions

Created on 11 Dec 2019  路  6Comments  路  Source: 99designs/gqlgen

What happened?

I want to write a Subscription resolver

What did you expect?

  • Expected to find a section called "Subscriptions" explaining how to write them with gqlgen

More info

Through some issue searching I found a link to this: https://github.com/99designs/gqlgen/blob/master/example/chat/resolvers.go#L115-L150

That's what I'm working off of currently. It would be useful to have some part of the docs touch on subscriptions.

accepted needs docs

Most helpful comment

@vektah is anyone working on this? I need to create a gql subscription and if no one is working on it, I will write some basic documentation once I figure it out

All 6 comments

While the chat example is a good starting point, it misses in my opinion two important parts:

  1. The channel is not closed in the cleanup. This leads to gqlgen never being able to terminate the internal go routine.
go func() {
  <-ctx.Done()
  r.mu.Lock()
  delete(room.Observers, id)
  close(events) // <-
  r.mu.Unlock()
}()
  1. The lock in this case can get blocked when there are connection issues with one client. The example buffers the channel, but I would argue that at some point the message should just be dropped so that no other clients are blocked from receiving messages.
r.mu.Lock()
for _, observer := range room.Observers {
  if observer.Username == "" || observer.Username == message.CreatedBy {
    select {
    case observer.Message <- &message:
    default:
      log.Printf("could not send message to %v\n", observer.Username)
    }
  }
}
r.mu.Unlock()

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@vektah is anyone working on this? I need to create a gql subscription and if no one is working on it, I will write some basic documentation once I figure it out

Yes, it would be nice to have some documentation on subscriptions. I have been trying to figure out how they work.

The only other reference I can find is https://www.freecodecamp.org/news/https-medium-com-anshap1719-graphql-subscriptions-with-go-gqlgen-and-mongodb-5e008fc46451/

I got them working with arsmn's fork for gofiber framework. However, I am having an issue where I get an error whenever a user abruptly closes the subscription by closing the browser window/tab

https://github.com/99designs/gqlgen/issues/1281

Was this page helpful?
0 / 5 - 0 ratings