项目作者: racerxdl

项目描述 :
Golang Simple GraphQL Subscriptions Handler
高级语言: Go
项目地址: git://github.com/racerxdl/go-subscription-handler.git
创建时间: 2020-02-05T02:32:23Z
项目社区:https://github.com/racerxdl/go-subscription-handler

开源协议:Apache License 2.0

下载


Golang Simple GraphQL Subscription Handler

Usage

Subscription node:

  1. var rootSubscriptions = graphql.ObjectConfig{
  2. Name: "RootSubscriptions",
  3. Fields: graphql.Fields{
  4. "serverTime": &graphql.Field{
  5. Type: graphql.Float,
  6. Resolve: func(p graphql.ResolveParams) (interface{}, error) {
  7. err := subhandler.Subscribe(p.Context, "serverTime")
  8. if p.Source != nil {
  9. // We received a event data
  10. v, ok := p.Source.(map[string]interface{})
  11. if ok && v["time"] != nil {
  12. return v["time"], nil
  13. }
  14. }
  15. // We didn't receive a event data, so resolve normally
  16. return time.Now().String(), err
  17. },
  18. },
  19. },
  20. }
  21. var rootQuery = graphql.ObjectConfig{
  22. Name: "RootQuery",
  23. Fields: graphql.Fields{
  24. "serverTime": &graphql.Field{
  25. Type: graphql.Float,
  26. Resolve: func(p graphql.ResolveParams) (interface{}, error) {
  27. return time.Now().Unix(), nil
  28. },
  29. },
  30. },
  31. }
  32. var schemaConfig = graphql.SchemaConfig{
  33. Query: graphql.NewObject(rootQuery),
  34. Subscription: graphql.NewObject(rootSubscriptions),
  35. }
  36. func GetSchema() (graphql.Schema, error) {
  37. return graphql.NewSchema(schemaConfig)
  38. }

Main Code:

  1. package main
  2. import (
  3. "github.com/asaskevich/EventBus"
  4. "github.com/graphql-go/handler"
  5. "github.com/racerxdl/go-subscription-handler/subhandler"
  6. "net/http"
  7. "time"
  8. "fmt"
  9. )
  10. // Create a notifier
  11. type BusNotifier struct {
  12. bus EventBus.Bus // You can use any pub-sub lib for that
  13. }
  14. func MakeBusNotifier(bus EventBus.Bus) *BusNotifier {
  15. return &BusNotifier{
  16. bus: bus,
  17. }
  18. }
  19. func (bn *BusNotifier) Subscribe(topic string, cb func(data map[string]interface{})) {
  20. bn.bus.Subscribe(topic, cb)
  21. }
  22. func (bn *BusNotifier) Unsubscribe(topic string, cb func(data map[string]interface{})) {
  23. bn.bus.Unsubscribe(topic, cb)
  24. }
  25. func (bn *BusNotifier) Notify(topic string, data map[string]interface{}) {
  26. bn.bus.Publish(topic, data)
  27. }
  28. // Initialize a Sub Handler
  29. func main() {
  30. schema, _ := GetSchema()
  31. // Create normal mutation / query handlers
  32. h := handler.New(&handler.Config{
  33. Schema: &schema,
  34. Pretty: true,
  35. Playground: true,
  36. })
  37. // Initialize our notifier
  38. notifier := MakeBusNotifier(EventBus.New())
  39. // Create a goroutine to send notifications through notifier
  40. go func() {
  41. for {
  42. data := map[string]interface{}{
  43. "time": time.Now().String(),
  44. }
  45. notifier.Notify("serverTime", data)
  46. time.Sleep(time.Second) // Sleep some interval
  47. // This also works, makes the resolver decide what to do
  48. notifier.Notify("serverTime", nil)
  49. }
  50. }()
  51. // Create the subscription handler using notifier and schema
  52. sh := subhandler.MakeSubscriptionHandler(notifier, schema)
  53. // Optional, check origin of the request
  54. sh.SetCheckOrigin(func(r *http.Request) bool {
  55. // Accept any origin
  56. return true
  57. })
  58. // Attach the normal query / mutation handlers
  59. http.Handle("/", h)
  60. // Attach the subscription handlers
  61. http.Handle("/subscriptions", sh)
  62. fmt.Println("Listening in :8080")
  63. http.ListenAndServe(":8080", nil)
  64. }