Skip to content

Commit

Permalink
fix(GraphQL): Pass on HTTP request headers for subscriptions (#7806) (#…
Browse files Browse the repository at this point in the history
…7807)

* return as soon as error is encountered

* fix comment

* add more logs

* fixing the http request header to be set in the subscription

* fixing header type cast

Co-authored-by: aman-bansal <[email protected]>

Co-authored-by: Abhimanyu Singh Gaur <[email protected]>
(cherry picked from commit dbd5744)
  • Loading branch information
aman-bansal authored and all-seeing-code committed Jan 4, 2023
1 parent 8c72479 commit a1d7411
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions graphql/admin/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ func (gs *graphqlSubscription) Subscribe(
variableValues map[string]interface{}) (payloads <-chan interface{},
err error) {

reqHeader := http.Header{}
// library (graphql-transport-ws) passes the headers which are part of the INIT payload to us
// in the context. We are extracting those headers and passing them along.
headerPayload, _ := ctx.Value("Header").(json.RawMessage)
reqHeader := http.Header{}
if len(headerPayload) > 0 {
headers := make(map[string]interface{})
if err = json.Unmarshal(headerPayload, &headers); err != nil {
Expand All @@ -175,14 +175,31 @@ func (gs *graphqlSubscription) Subscribe(
}
}

// Earlier the graphql-transport-ws library was ignoring the http headers in the request.
// The library was relying upon the information present in the request payload. This was
// blocker for the cloud team because the only control cloud has is over the HTTP headers.
// This fix ensures that we are setting the request headers if not provided in the payload.
httpHeaders, _ := ctx.Value("RequestHeader").(http.Header)
if len(httpHeaders) > 0 {
for k := range httpHeaders {
if len(strings.TrimSpace(reqHeader.Get(k))) == 0 {
reqHeader.Set(k, httpHeaders.Get(k))
}
}
}

req := &schema.Request{
OperationName: operationName,
Query: document,
Variables: variableValues,
Header: reqHeader,
}
namespace := x.ExtractNamespaceHTTP(&http.Request{Header: reqHeader})
LazyLoadSchema(namespace) // first load the schema, then do anything else
glog.Infof("namespace: %d. Got GraphQL request over websocket.", namespace)
// first load the schema, then do anything else
if err = LazyLoadSchema(namespace); err != nil {
return nil, err
}
if err = gs.isValid(namespace); err != nil {
glog.Errorf("namespace: %d. graphqlSubscription not initialized: %s", namespace, err)
return nil, errors.New(resolve.ErrInternal)
Expand Down Expand Up @@ -220,6 +237,7 @@ func (gh *graphqlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer span.End()

ns, _ := strconv.ParseUint(r.Header.Get("resolver"), 10, 64)
glog.Infof("namespace: %d. Got GraphQL request over HTTP.", ns)
if err := gh.isValid(ns); err != nil {
glog.Errorf("namespace: %d. graphqlHandler not initialised: %s", ns, err)
WriteErrorResponse(w, r, errors.New(resolve.ErrInternal))
Expand Down

0 comments on commit a1d7411

Please sign in to comment.