Skip to content

Commit

Permalink
Use generics for subscriptions responses (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
HaraldNordgren authored Nov 30, 2024
1 parent e030ff1 commit ca5889f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/subscriptions.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Using genqlient with GraphQL subscriptions

This document describes how to use genqlient to make GraphQL subscriptions. It assumes you already have the basic [client](./client_config.md) set up. Subscription support is fairly new; please report any bugs or missing features!
This document describes how to use genqlient to make GraphQL subscriptions. It assumes you already have the basic [client](./client_config.md) set up. Subscription support is fairly new; please report any bugs or missing features!

## Client setup

Expand Down
10 changes: 3 additions & 7 deletions generate/operation.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,16 @@ func {{.Name}}(
resp_,
)
{{end}}

return {{if eq .Type "subscription"}}dataChan_, subscriptionID_,{{else}}data_, {{if .Config.Extensions -}}resp_.Extensions,{{end -}}{{end}} err_
}

{{if eq .Type "subscription"}}
type {{.Name}}WsResponse struct {
Data *{{.ResponseName}} `json:"data"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
Errors error `json:"errors"`
}
type {{.Name}}WsResponse graphql.BaseResponse[*{{.ResponseName}}]

func {{.Name}}ForwardData(interfaceChan interface{}, jsonRawMsg json.RawMessage) error {
var gqlResp graphql.Response
var wsResp {{.Name}}WsResponse
var wsResp {{.Name}}WsResponse
err := json.Unmarshal(jsonRawMsg, &gqlResp)
if err != nil {
return err
Expand Down

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

12 changes: 7 additions & 5 deletions graphql/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ type Request struct {
OpName string `json:"operationName"`
}

type BaseResponse[T any] struct {
Data T `json:"data"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
Errors gqlerror.List `json:"errors,omitempty"`
}

// Response that contains data returned by the GraphQL API.
//
// Typically, GraphQL APIs will return a JSON payload of the form
Expand All @@ -206,11 +212,7 @@ type Request struct {
// It may additionally contain a key named "extensions", that
// might hold GraphQL protocol extensions. Extensions and Errors
// are optional, depending on the values returned by the server.
type Response struct {
Data interface{} `json:"data"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
Errors gqlerror.List `json:"errors,omitempty"`
}
type Response BaseResponse[any]

func (c *client) MakeRequest(ctx context.Context, req *Request, resp *Response) error {
var httpReq *http.Request
Expand Down
6 changes: 1 addition & 5 deletions internal/integration/generated.go

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

0 comments on commit ca5889f

Please sign in to comment.