From 90626ca0c5e046ef09c0ffb5d525751db611b754 Mon Sep 17 00:00:00 2001 From: Harald Nordgren Date: Sat, 28 Sep 2024 17:28:50 +0200 Subject: [PATCH] Remove type param from user-space --- docs/subscriptions.md | 4 ++-- generate/operation.go.tmpl | 16 +++++++++++----- ...iption.graphql-SimpleSubscription.graphql.go | 16 +++++++++++----- graphql/client.go | 3 +-- internal/integration/generated.go | 15 ++++++++++----- internal/integration/integration_test.go | 2 +- internal/integration/roundtrip.go | 17 ++++++++++++----- 7 files changed, 48 insertions(+), 25 deletions(-) diff --git a/docs/subscriptions.md b/docs/subscriptions.md index 267afe8b..eccc3f50 100644 --- a/docs/subscriptions.md +++ b/docs/subscriptions.md @@ -76,7 +76,7 @@ Once your websocket client matches the interfaces, you can get your `graphql.Web a loop for incoming messages and errors: ```go - graphqlClient := graphql.NewClientUsingWebSocket[countResponse]( + graphqlClient := countClientUsingWebSocket( "ws://localhost:8080/query", &MyDialer{Dialer: dialer}, headers, @@ -116,7 +116,7 @@ a loop for incoming messages and errors: } ``` -To change the websocket protocol from its default value `graphql-transport-ws`, add the following header before calling `graphql.NewClientUsingWebSocket()`: +To change the websocket protocol from its default value `graphql-transport-ws`, add the following header before calling `countClientUsingWebSocket()`: ```go headers.Add("Sec-WebSocket-Protocol", "graphql-ws") ``` diff --git a/generate/operation.go.tmpl b/generate/operation.go.tmpl index 8c26b86e..728604c8 100644 --- a/generate/operation.go.tmpl +++ b/generate/operation.go.tmpl @@ -2,12 +2,12 @@ const {{.Name}}_Operation = `{{$.Body}}` {{.Doc}} -func {{.Name}}{{if eq .Type "subscription"}}[T any]{{end}}( +func {{.Name}}( {{if ne .Config.ContextType "-" -}} ctx_ {{ref .Config.ContextType}}, {{end}} {{- if not .Config.ClientGetter -}} - client_ {{if eq .Type "subscription"}}{{ref "github.com/Khan/genqlient/graphql.WebSocketClient"}}[T]{{else}}{{ref "github.com/Khan/genqlient/graphql.Client"}}{{end}}, + client_ {{if eq .Type "subscription"}}{{ref "github.com/Khan/genqlient/graphql.WebSocketClient"}}[{{.ResponseName}}]{{else}}{{ref "github.com/Khan/genqlient/graphql.Client"}}{{end}}, {{end}} {{- if .Input -}} {{- range .Input.Fields -}} @@ -15,7 +15,7 @@ func {{.Name}}{{if eq .Type "subscription"}}[T any]{{end}}( {{.GraphQLName}} {{.GoType.Reference}}, {{end -}} {{end -}} -) ({{if eq .Type "subscription"}}dataChan_ chan graphql.WsResponse[T], subscriptionID_ string,{{else}}data_ *{{.ResponseName}}, {{if .Config.Extensions -}}ext_ map[string]interface{},{{end}}{{end}} err_ error) { +) ({{if eq .Type "subscription"}}dataChan_ chan graphql.WsResponse[{{.ResponseName}}], subscriptionID_ string,{{else}}data_ *{{.ResponseName}}, {{if .Config.Extensions -}}ext_ map[string]interface{},{{end}}{{end}} err_ error) { req_ := &graphql.Request{ OpName: "{{.Name}}", Query: {{.Name}}_Operation, @@ -36,8 +36,8 @@ func {{.Name}}{{if eq .Type "subscription"}}[T any]{{end}}( } {{end}} {{if eq .Type "subscription"}} - dataChan_ = make(chan graphql.WsResponse[T]) - subscriptionID_, err_ = client_.Subscribe(req_, dataChan_, graphql.ForwardData[T]) + dataChan_ = make(chan graphql.WsResponse[{{.ResponseName}}]) + subscriptionID_, err_ = client_.Subscribe(req_, dataChan_, graphql.ForwardData[{{.ResponseName}}]) {{else}} data_ = &{{.ResponseName}}{} resp_ := &graphql.Response{Data: data_} @@ -51,3 +51,9 @@ func {{.Name}}{{if eq .Type "subscription"}}[T any]{{end}}( return {{if eq .Type "subscription"}}dataChan_, subscriptionID_,{{else}}data_, {{if .Config.Extensions -}}resp_.Extensions,{{end -}}{{end}} err_ } + +{{if eq .Type "subscription"}} +func {{.Name}}ClientUsingWebSocket(endpoint string, wsDialer graphql.Dialer, headers http.Header) graphql.WebSocketClient[{{.ResponseName}}] { + return graphql.NewClientUsingWebSocket[{{.ResponseName}}](endpoint, wsDialer, headers) +} +{{end}} diff --git a/generate/testdata/snapshots/TestGenerate-SimpleSubscription.graphql-SimpleSubscription.graphql.go b/generate/testdata/snapshots/TestGenerate-SimpleSubscription.graphql-SimpleSubscription.graphql.go index 55bf604e..3053e0cc 100644 --- a/generate/testdata/snapshots/TestGenerate-SimpleSubscription.graphql-SimpleSubscription.graphql.go +++ b/generate/testdata/snapshots/TestGenerate-SimpleSubscription.graphql-SimpleSubscription.graphql.go @@ -3,6 +3,8 @@ package test import ( + "net/http" + "github.com/Khan/genqlient/graphql" ) @@ -22,17 +24,21 @@ subscription SimpleSubscription { ` // To unsubscribe, use [graphql.WebSocketClient.Unsubscribe] -func SimpleSubscription[T any]( - client_ graphql.WebSocketClient[T], -) (dataChan_ chan graphql.WsResponse[T], subscriptionID_ string, err_ error) { +func SimpleSubscription( + client_ graphql.WebSocketClient[SimpleSubscriptionResponse], +) (dataChan_ chan graphql.WsResponse[SimpleSubscriptionResponse], subscriptionID_ string, err_ error) { req_ := &graphql.Request{ OpName: "SimpleSubscription", Query: SimpleSubscription_Operation, } - dataChan_ = make(chan graphql.WsResponse[T]) - subscriptionID_, err_ = client_.Subscribe(req_, dataChan_, graphql.ForwardData[T]) + dataChan_ = make(chan graphql.WsResponse[SimpleSubscriptionResponse]) + subscriptionID_, err_ = client_.Subscribe(req_, dataChan_, graphql.ForwardData[SimpleSubscriptionResponse]) return dataChan_, subscriptionID_, err_ } +func SimpleSubscriptionClientUsingWebSocket(endpoint string, wsDialer graphql.Dialer, headers http.Header) graphql.WebSocketClient[SimpleSubscriptionResponse] { + return graphql.NewClientUsingWebSocket[SimpleSubscriptionResponse](endpoint, wsDialer, headers) +} + diff --git a/graphql/client.go b/graphql/client.go index 0f4ad61e..cf2687a0 100644 --- a/graphql/client.go +++ b/graphql/client.go @@ -62,8 +62,7 @@ type WebSocketClient[T any] interface { // dataChan is a channel used to send the data that arrives via the // webSocket connection (it is the channel that is passed to `forwardDataFunc`). // - // forwardDataFunc is the function that will cast the received interface into - // the valid type for the subscription's response. + // forwardDataFunc is the function that will handle the subscription's response. // // Returns a subscriptionID if successful, an error otherwise. Subscribe( diff --git a/internal/integration/generated.go b/internal/integration/generated.go index 41e80e1e..65347b98 100644 --- a/internal/integration/generated.go +++ b/internal/integration/generated.go @@ -6,6 +6,7 @@ import ( "context" "encoding/json" "fmt" + "net/http" "time" "github.com/Khan/genqlient/graphql" @@ -3103,21 +3104,25 @@ subscription count { ` // To unsubscribe, use [graphql.WebSocketClient.Unsubscribe] -func count[T any]( +func count( ctx_ context.Context, - client_ graphql.WebSocketClient[T], -) (dataChan_ chan graphql.WsResponse[T], subscriptionID_ string, err_ error) { + client_ graphql.WebSocketClient[countResponse], +) (dataChan_ chan graphql.WsResponse[countResponse], subscriptionID_ string, err_ error) { req_ := &graphql.Request{ OpName: "count", Query: count_Operation, } - dataChan_ = make(chan graphql.WsResponse[T]) - subscriptionID_, err_ = client_.Subscribe(req_, dataChan_, graphql.ForwardData[T]) + dataChan_ = make(chan graphql.WsResponse[countResponse]) + subscriptionID_, err_ = client_.Subscribe(req_, dataChan_, graphql.ForwardData[countResponse]) return dataChan_, subscriptionID_, err_ } +func countClientUsingWebSocket(endpoint string, wsDialer graphql.Dialer, headers http.Header) graphql.WebSocketClient[countResponse] { + return graphql.NewClientUsingWebSocket[countResponse](endpoint, wsDialer, headers) +} + // The mutation executed by createUser. const createUser_Operation = ` mutation createUser ($user: NewUser!) { diff --git a/internal/integration/integration_test.go b/internal/integration/integration_test.go index 32545924..7ce6eba4 100644 --- a/internal/integration/integration_test.go +++ b/internal/integration/integration_test.go @@ -64,7 +64,7 @@ func TestSubscription(t *testing.T) { ctx := context.Background() server := server.RunServer() defer server.Close() - wsClient := newRoundtripWebSocketClient[countResponse](t, server.URL) + wsClient := newCountRoundtripWebSocketClient(t, server.URL) errChan, err := wsClient.Start(ctx) require.NoError(t, err) diff --git a/internal/integration/roundtrip.go b/internal/integration/roundtrip.go index cf4261c3..38988a61 100644 --- a/internal/integration/roundtrip.go +++ b/internal/integration/roundtrip.go @@ -156,14 +156,21 @@ func (md *MyDialer) DialContext(ctx context.Context, urlStr string, requestHeade return graphql.WSConn(conn), err } -func newRoundtripWebSocketClient[T any](t *testing.T, endpoint string) graphql.WebSocketClient[T] { - dialer := websocket.DefaultDialer +func wsAdress(endpoint string) string { if !strings.HasPrefix(endpoint, "ws") { _, address, _ := strings.Cut(endpoint, "://") endpoint = "ws://" + address } - return &roundtripClient[T]{ - wsWrapped: graphql.NewClientUsingWebSocket[T](endpoint, &MyDialer{Dialer: dialer}, nil), - t: t, + return endpoint +} + +func newCountRoundtripWebSocketClient(t *testing.T, endpoint string) graphql.WebSocketClient[countResponse] { + return &roundtripClient[countResponse]{ + wsWrapped: countClientUsingWebSocket( + wsAdress(endpoint), + &MyDialer{Dialer: websocket.DefaultDialer}, + nil, + ), + t: t, } }