1
1
# graphqlws
2
2
3
- Implementation of the Apollo GraphQL WebSocket protocol in Go.
3
+ Implementation of the [ GraphQL over WebSocket protocol] in Go.
4
4
5
5
## Getting started
6
6
@@ -24,8 +24,73 @@ Implementation of the Apollo GraphQL WebSocket protocol in Go.
24
24
go run graphqlws/examples/server
25
25
```
26
26
27
+ ## Usage
28
+
29
+ ### Setup
30
+
31
+ ``` go
32
+ package main
33
+
34
+ import (
35
+ " net/http"
36
+
37
+ " github.com/functionalfoundry/graphqlws"
38
+ )
39
+
40
+ func main () {
41
+ // Create a subscription manager
42
+ subscriptionManager := graphqlws.NewSubscriptionManager ()
43
+
44
+ graphqlwsHandler := graphqlws.NewHandler (graphqlws.HandlerConfig {
45
+ // Wire up the GraphqL WebSocket handler with the subscription manager
46
+ SubscriptionManager: subscriptionManager,
47
+
48
+ // Optional: Add a hook to resolve auth tokens into users that are
49
+ // then stored on the GraphQL WS connections
50
+ Authenticate: func (authToken string ) (interface {}, error ) {
51
+ // This is just a dumb example
52
+ return " Joe" , nil
53
+ },
54
+ })
55
+
56
+ // The handler integrates seamlessly with existing HTTP servers
57
+ http.Handle (" /subscriptions" , graphqlwsHandler)
58
+ http.ListenAndServe (" :8080" , nil )
59
+ }
60
+ ```
61
+
62
+ ### Working with subscriptions
63
+
64
+ ``` go
65
+ // This assumes you have access to the above subscription manager
66
+
67
+ subscriptions := subscriptionManager.Subscriptions ()
68
+
69
+ for _ , conn := range subscriptions {
70
+ if conn.User == " Joe" {
71
+ for _ , subscription := range subscriptions[conn] {
72
+ // Things you have access to here:
73
+ subscription.ID // The subscription ID (unique per conn)
74
+ subscription.OperationName // The name of the subcription
75
+ subscription.Query // The subscription query/queries string
76
+ subscription.Variables // The subscription variables
77
+ subscription.Document // The GraphQL AST for the subscription
78
+ subscription.Fields // The names of top-level queries
79
+ subscription.Connection // The GraphQL WS connection
80
+
81
+ // Send query results back to the subscriber at any point
82
+ subscription.SendData (subscription, &graphqlws.DataMessagePayload {
83
+ Data: ..., // Can be anything (interface{})
84
+ Errors: ..., // Optional ([]error)
85
+ })
86
+ }
87
+ }
88
+ ` ` `
89
+
27
90
## License
28
91
29
92
Copyright (C) 2017 Functional Foundry, LLC.
30
93
31
94
Licensed under the [MIT License](LICENSE.md).
95
+
96
+ [graphql over websocket protocol]: https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md
0 commit comments