Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom websocket upgrader #1595

Merged
merged 1 commit into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/recipes/cors.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {

srv := handler.NewDefaultServer(starwars.NewExecutableSchema(starwars.NewResolver()))
srv.AddTransport(&transport.Websocket{
Upgrader: websocket.Upgrader{
Upgrader: &websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
// Check against your desired domains here
return r.Host == "example.org"
Expand Down
2 changes: 1 addition & 1 deletion example/chat/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
srv.AddTransport(transport.POST{})
srv.AddTransport(transport.Websocket{
KeepAlivePingInterval: 10 * time.Second,
Upgrader: websocket.Upgrader{
Upgrader: &websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
Expand Down
10 changes: 8 additions & 2 deletions graphql/handler/transport/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (

type (
Websocket struct {
Upgrader websocket.Upgrader
Upgrader WebsocketUpgrader
InitFunc WebsocketInitFunc
KeepAlivePingInterval time.Duration
}
Expand All @@ -51,10 +51,16 @@ type (
ID string `json:"id,omitempty"`
Type string `json:"type"`
}
WebsocketUpgrader interface {
Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*websocket.Conn, error)
}
WebsocketInitFunc func(ctx context.Context, initPayload InitPayload) (context.Context, error)
)

var _ graphql.Transport = Websocket{}
var (
_ graphql.Transport = Websocket{}
_ WebsocketUpgrader = &websocket.Upgrader{}
)

func (t Websocket) Supports(r *http.Request) bool {
return r.Header.Get("Upgrade") != ""
Expand Down
5 changes: 2 additions & 3 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/99designs/gqlgen/graphql/handler/lru"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/gorilla/websocket"
)

// Deprecated: switch to graphql/handler.New
Expand Down Expand Up @@ -74,7 +73,7 @@ func GraphQL(exec graphql.ExecutableSchema, options ...Option) http.HandlerFunc
// Deprecated: switch to graphql/handler.New
type Config struct {
cacheSize int
upgrader websocket.Upgrader
upgrader transport.WebsocketUpgrader
websocketInitFunc transport.WebsocketInitFunc
connectionKeepAlivePingInterval time.Duration
recover graphql.RecoverFunc
Expand All @@ -93,7 +92,7 @@ type Config struct {
type Option func(cfg *Config)

// Deprecated: switch to graphql/handler.New
func WebsocketUpgrader(upgrader websocket.Upgrader) Option {
func WebsocketUpgrader(upgrader transport.WebsocketUpgrader) Option {
return func(cfg *Config) {
cfg.upgrader = upgrader
}
Expand Down