Skip to content

Commit

Permalink
Allow custom websocket upgrader
Browse files Browse the repository at this point in the history
  • Loading branch information
foreverest committed Sep 1, 2021
1 parent 9a214e8 commit 7d41d0b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
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
5 changes: 4 additions & 1 deletion 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,6 +51,9 @@ 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)
)

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

0 comments on commit 7d41d0b

Please sign in to comment.