Skip to content

Commit

Permalink
Remove type param from user-space
Browse files Browse the repository at this point in the history
  • Loading branch information
HaraldNordgren committed Sep 28, 2024
1 parent 71a7e54 commit 90626ca
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 25 deletions.
4 changes: 2 additions & 2 deletions docs/subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
```
16 changes: 11 additions & 5 deletions generate/operation.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
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 -}}
{{/* the GraphQL name here is the user-specified variable-name */ -}}
{{.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,
Expand All @@ -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_}
Expand All @@ -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}}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions graphql/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 10 additions & 5 deletions internal/integration/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 12 additions & 5 deletions internal/integration/roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

0 comments on commit 90626ca

Please sign in to comment.