Skip to content

Commit 7d24db2

Browse files
author
Jannis Pohlmann
committed
README: Add information on using graphqlws
1 parent ce5565b commit 7d24db2

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# graphqlws
22

3-
Implementation of the Apollo GraphQL WebSocket protocol in Go.
3+
Implementation of the [GraphQL over WebSocket protocol] in Go.
44

55
## Getting started
66

@@ -24,8 +24,73 @@ Implementation of the Apollo GraphQL WebSocket protocol in Go.
2424
go run graphqlws/examples/server
2525
```
2626

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+
2790
## License
2891
2992
Copyright (C) 2017 Functional Foundry, LLC.
3093
3194
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

Comments
 (0)