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

SSE Transport must be inserted before POST, so add convenience for using the DefaultServer #3278

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions graphql/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type (
Transport interface {
Supports(r *http.Request) bool
Do(w http.ResponseWriter, r *http.Request, exec GraphExecutor)
String() string
}
)

Expand Down
26 changes: 23 additions & 3 deletions graphql/handler/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net/http"
"slices"
"time"

"github.com/vektah/gqlparser/v2/ast"
Expand Down Expand Up @@ -39,6 +40,8 @@ func NewDefaultServer(es graphql.ExecutableSchema) *Server {
})
srv.AddTransport(transport.Options{})
srv.AddTransport(transport.GET{})
// To enable Server-Sent-Events, must insert here:
// srv.AddTransport(transport.SSE{})
srv.AddTransport(transport.POST{})
srv.AddTransport(transport.MultipartForm{})

Expand All @@ -56,6 +59,22 @@ func (s *Server) AddTransport(transport graphql.Transport) {
s.transports = append(s.transports, transport)
}

func (s *Server) PrependTransport(transport graphql.Transport) {
s.transports = append([]graphql.Transport{transport}, s.transports...)
}

// AddPrePostTransport is primarily for adding experimental transports like
// SSE so that the usual POST transport doesn't respond
func (s *Server) AddPrePostTransport(transport graphql.Transport) {
postIndex := slices.IndexFunc(s.transports, func(n graphql.Transport) bool {
return n.String() == "POST"
})

if postIndex != -1 {
s.transports = slices.Insert(s.transports, postIndex, transport)
}
}

func (s *Server) SetErrorPresenter(f graphql.ErrorPresenterFunc) {
s.exec.SetErrorPresenter(f)
}
Expand Down Expand Up @@ -96,6 +115,7 @@ func (s *Server) AroundResponses(f graphql.ResponseMiddleware) {
s.exec.AroundResponses(f)
}

// Transport choice is first acceptable
func (s *Server) getTransport(r *http.Request) graphql.Transport {
for _, t := range s.transports {
if t.Supports(r) {
Expand All @@ -119,13 +139,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {

r = r.WithContext(graphql.StartOperationTrace(r.Context()))

transport := s.getTransport(r)
if transport == nil {
gqlTransport := s.getTransport(r)
if gqlTransport == nil {
sendErrorf(w, http.StatusBadRequest, "transport not supported")
return
}

transport.Do(w, r, s.exec)
gqlTransport.Do(w, r, s.exec)
}

func sendError(w http.ResponseWriter, code int, errors ...*gqlerror.Error) {
Expand Down
6 changes: 6 additions & 0 deletions graphql/handler/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ func TestErrorServer(t *testing.T) {

type panicTransport struct{}

var _ graphql.Transport = panicTransport{}

func (t panicTransport) String() string {
return "panicTransport"
}

func (t panicTransport) Supports(r *http.Request) bool {
return true
}
Expand Down
4 changes: 4 additions & 0 deletions graphql/handler/transport/http_form_multipart.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type MultipartForm struct {

var _ graphql.Transport = MultipartForm{}

func (f MultipartForm) String() string {
return "MultipartForm"
}

func (f MultipartForm) Supports(r *http.Request) bool {
if r.Header.Get("Upgrade") != "" {
return false
Expand Down
4 changes: 4 additions & 0 deletions graphql/handler/transport/http_form_urlencoded.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type UrlEncodedForm struct {

var _ graphql.Transport = UrlEncodedForm{}

func (h UrlEncodedForm) String() string {
return "UrlEncodedForm"
}

func (h UrlEncodedForm) Supports(r *http.Request) bool {
if r.Header.Get("Upgrade") != "" {
return false
Expand Down
4 changes: 4 additions & 0 deletions graphql/handler/transport/http_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type GET struct {

var _ graphql.Transport = GET{}

func (h GET) String() string {
return "GET"
}

func (h GET) Supports(r *http.Request) bool {
if r.Header.Get("Upgrade") != "" {
return false
Expand Down
4 changes: 4 additions & 0 deletions graphql/handler/transport/http_graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type GRAPHQL struct {

var _ graphql.Transport = GRAPHQL{}

func (h GRAPHQL) String() string {
return "GRAPHQL"
}

func (h GRAPHQL) Supports(r *http.Request) bool {
if r.Header.Get("Upgrade") != "" {
return false
Expand Down
4 changes: 4 additions & 0 deletions graphql/handler/transport/http_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type POST struct {

var _ graphql.Transport = POST{}

func (h POST) String() string {
return "POST"
}

func (h POST) Supports(r *http.Request) bool {
if r.Header.Get("Upgrade") != "" {
return false
Expand Down
4 changes: 4 additions & 0 deletions graphql/handler/transport/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type Options struct {

var _ graphql.Transport = Options{}

func (o Options) String() string {
return "Options"
}

func (o Options) Supports(r *http.Request) bool {
return r.Method == "HEAD" || r.Method == "OPTIONS"
}
Expand Down
4 changes: 4 additions & 0 deletions graphql/handler/transport/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type SSE struct{}

var _ graphql.Transport = SSE{}

func (t SSE) String() string {
return "SSE"
}

func (t SSE) Supports(r *http.Request) bool {
if !strings.Contains(r.Header.Get("Accept"), "text/event-stream") {
return false
Expand Down
4 changes: 4 additions & 0 deletions graphql/handler/transport/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ var (
_ error = WebsocketError{}
)

func (t Websocket) String() string {
return "Websocket"
}

func (t Websocket) Supports(r *http.Request) bool {
return r.Header.Get("Upgrade") != ""
}
Expand Down
Loading