Skip to content

Commit

Permalink
docs nits
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminjkraft committed Aug 16, 2024
1 parent d18e6b5 commit 828a1f9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
15 changes: 8 additions & 7 deletions docs/subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This document describes how to use genqlient to make GraphQL subscriptions. It a

## Client setup

You will need to use a different client calling `graphql.NewClientUsingWebSocket`, passing as parameter your own websocket client.
You will need to use a different client calling `graphql.NewClientUsingWebSocket`, passing as a parameter your own websocket client.

Here is how to configure your webSocket client to match the interfaces:

Expand Down Expand Up @@ -72,11 +72,11 @@ func (md *MyDialer) DialContext(ctx context.Context, urlStr string, requestHeade

## Making subscriptions

Once your webSocket client matches the interfaces, you can get your `graphql.WebSocketClient` and listen in
Once your websocket client matches the interfaces, you can get your `graphql.WebSocketClient` and listen in
a loop for incoming messages and errors:

```go
graphqlClient := graphql.NewClientUsingWebSocket(
graphqlClient := graphql.NewClientUsingWebSocket(
"ws://localhost:8080/query",
&MyDialer{Dialer: dialer},
headers,
Expand Down Expand Up @@ -109,13 +109,14 @@ a loop for incoming messages and errors:
}
case err = <-errChan:
return
case <-time.After(time.Minute):
err = wsClient.Unsubscribe(subscriptionID)
loop = false
case <-time.After(time.Minute):
err = wsClient.Unsubscribe(subscriptionID)
loop = false
}
}
```

To change the websocket protocol from its default value `graphql-transport-ws`, add the following header before calling `graphql.NewClientUsingWebSocket()`:
```go
headers.Add("Sec-WebSocket-Protocol", "graphql-ws")
headers.Add("Sec-WebSocket-Protocol", "graphql-ws")
```
6 changes: 5 additions & 1 deletion graphql/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type WebSocketClient interface {
// forwardDataFunc is the function that will cast the received interface into
// the valid type for the subscription's response.
//
// returns a subscriptionID if successful, an error otherwise
// Returns a subscriptionID if successful, an error otherwise.
Subscribe(
req *Request,
interfaceChan interface{},
Expand All @@ -76,6 +76,10 @@ type WebSocketClient interface {
Unsubscribe(subscriptionID string) error
}

// ForwardDataFunction is a part of the WebSocketClient interface, see
// [WebSocketClient.Subscribe] for details.
type ForwardDataFunction func(interfaceChan interface{}, jsonRawMsg json.RawMessage) error

type client struct {
httpClient Doer
endpoint string
Expand Down
3 changes: 0 additions & 3 deletions graphql/subscription.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package graphql

import (
"encoding/json"
"fmt"
"reflect"
"sync"
Expand All @@ -20,8 +19,6 @@ type subscription struct {
hasBeenUnsubscribed bool
}

type ForwardDataFunction func(interfaceChan interface{}, jsonRawMsg json.RawMessage) error

func (s *subscriptionMap) Create(subscriptionID string, interfaceChan interface{}, forwardDataFunc ForwardDataFunction) {
s.Lock()
defer s.Unlock()
Expand Down

0 comments on commit 828a1f9

Please sign in to comment.