From 828a1f9d73ab4ceb3c9fa2fad8d8093e771ffb1f Mon Sep 17 00:00:00 2001 From: Ben Kraft Date: Fri, 16 Aug 2024 16:33:24 -0700 Subject: [PATCH] docs nits --- docs/subscriptions.md | 15 ++++++++------- graphql/client.go | 6 +++++- graphql/subscription.go | 3 --- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/docs/subscriptions.md b/docs/subscriptions.md index 26571a97..ee4d3d89 100644 --- a/docs/subscriptions.md +++ b/docs/subscriptions.md @@ -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: @@ -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, @@ -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") ``` diff --git a/graphql/client.go b/graphql/client.go index cd57d95e..526395b8 100644 --- a/graphql/client.go +++ b/graphql/client.go @@ -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{}, @@ -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 diff --git a/graphql/subscription.go b/graphql/subscription.go index 85162974..f86f75ee 100644 --- a/graphql/subscription.go +++ b/graphql/subscription.go @@ -1,7 +1,6 @@ package graphql import ( - "encoding/json" "fmt" "reflect" "sync" @@ -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()