Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/racerxdl/go-subscription-handler
Golang Simple GraphQL Subscriptions Handler
https://github.com/racerxdl/go-subscription-handler
golang graphql subscriptions websocket
Last synced: 11 days ago
JSON representation
Golang Simple GraphQL Subscriptions Handler
- Host: GitHub
- URL: https://github.com/racerxdl/go-subscription-handler
- Owner: racerxdl
- License: apache-2.0
- Created: 2020-02-05T02:32:23.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-05T03:31:29.000Z (almost 5 years ago)
- Last Synced: 2024-06-21T06:11:34.041Z (5 months ago)
- Topics: golang, graphql, subscriptions, websocket
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Golang Simple GraphQL Subscription Handler
## Usage
Subscription node:
```go
var rootSubscriptions = graphql.ObjectConfig{
Name: "RootSubscriptions",
Fields: graphql.Fields{
"serverTime": &graphql.Field{
Type: graphql.Float,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
err := subhandler.Subscribe(p.Context, "serverTime")if p.Source != nil {
// We received a event data
v, ok := p.Source.(map[string]interface{})
if ok && v["time"] != nil {
return v["time"], nil
}
}// We didn't receive a event data, so resolve normally
return time.Now().String(), err
},
},
},
}var rootQuery = graphql.ObjectConfig{
Name: "RootQuery",
Fields: graphql.Fields{
"serverTime": &graphql.Field{
Type: graphql.Float,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return time.Now().Unix(), nil
},
},
},
}var schemaConfig = graphql.SchemaConfig{
Query: graphql.NewObject(rootQuery),
Subscription: graphql.NewObject(rootSubscriptions),
}func GetSchema() (graphql.Schema, error) {
return graphql.NewSchema(schemaConfig)
}```
Main Code:
```go
package mainimport (
"github.com/asaskevich/EventBus"
"github.com/graphql-go/handler"
"github.com/racerxdl/go-subscription-handler/subhandler"
"net/http"
"time"
"fmt"
)// Create a notifier
type BusNotifier struct {
bus EventBus.Bus // You can use any pub-sub lib for that
}func MakeBusNotifier(bus EventBus.Bus) *BusNotifier {
return &BusNotifier{
bus: bus,
}
}func (bn *BusNotifier) Subscribe(topic string, cb func(data map[string]interface{})) {
bn.bus.Subscribe(topic, cb)
}func (bn *BusNotifier) Unsubscribe(topic string, cb func(data map[string]interface{})) {
bn.bus.Unsubscribe(topic, cb)
}func (bn *BusNotifier) Notify(topic string, data map[string]interface{}) {
bn.bus.Publish(topic, data)
}// Initialize a Sub Handler
func main() {schema, _ := GetSchema()
// Create normal mutation / query handlers
h := handler.New(&handler.Config{
Schema: &schema,
Pretty: true,
Playground: true,
})
// Initialize our notifier
notifier := MakeBusNotifier(EventBus.New())// Create a goroutine to send notifications through notifier
go func() {
for {
data := map[string]interface{}{
"time": time.Now().String(),
}
notifier.Notify("serverTime", data)
time.Sleep(time.Second) // Sleep some interval// This also works, makes the resolver decide what to do
notifier.Notify("serverTime", nil)
}
}()// Create the subscription handler using notifier and schema
sh := subhandler.MakeSubscriptionHandler(notifier, schema)// Optional, check origin of the request
sh.SetCheckOrigin(func(r *http.Request) bool {
// Accept any origin
return true
})// Attach the normal query / mutation handlers
http.Handle("/", h)
// Attach the subscription handlers
http.Handle("/subscriptions", sh)
fmt.Println("Listening in :8080")
http.ListenAndServe(":8080", nil)
}
```