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
* 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]>
  • Loading branch information
abhimanyusinghgaur and aman-bansal authored May 11, 2021
1 parent 9e82009 commit 042d35c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/dgraph-io/dgo/v210 v210.0.0-20210407152819-261d1c2a6987
github.com/dgraph-io/gqlgen v0.13.2
github.com/dgraph-io/gqlparser/v2 v2.2.0
github.com/dgraph-io/graphql-transport-ws v0.0.0-20210223074046-e5b8b80bb4ed
github.com/dgraph-io/graphql-transport-ws v0.0.0-20210511143556-2cef522f1f15
github.com/dgraph-io/ristretto v0.0.4-0.20210504190834-0bf2acd73aa3
github.com/dgraph-io/roaring v0.5.6-0.20210227175938-766b897233a5
github.com/dgraph-io/simdjson-go v0.3.0
Expand All @@ -43,7 +43,6 @@ require (
github.com/google/uuid v1.0.0
github.com/gorilla/websocket v1.4.2
github.com/graph-gophers/graphql-go v0.0.0-20200309224638-dae41bde9ef9
github.com/graph-gophers/graphql-transport-ws v0.0.0-20190611222414-40c048432299 // indirect
github.com/hashicorp/vault/api v1.0.4
github.com/minio/minio-go/v6 v6.0.55
github.com/mitchellh/panicwrap v1.0.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ github.com/dgraph-io/gqlgen v0.13.2/go.mod h1:iCOrOv9lngN7KAo+jMgvUPVDlYHdf7qDws
github.com/dgraph-io/gqlparser/v2 v2.1.1/go.mod h1:MYS4jppjyx8b9tuUtjV7jU1UFZK6P9fvO8TsIsQtRKU=
github.com/dgraph-io/gqlparser/v2 v2.2.0 h1:fKSCW8OxoMogjDwUhO9OrFvrgIA0UZspTDbcm0QGk9M=
github.com/dgraph-io/gqlparser/v2 v2.2.0/go.mod h1:MYS4jppjyx8b9tuUtjV7jU1UFZK6P9fvO8TsIsQtRKU=
github.com/dgraph-io/graphql-transport-ws v0.0.0-20210223074046-e5b8b80bb4ed h1:pgGMBoTtFhR+xkyzINaToLYRurHn+6pxMYffIGmmEPc=
github.com/dgraph-io/graphql-transport-ws v0.0.0-20210223074046-e5b8b80bb4ed/go.mod h1:7z3c/5w0sMYYZF5bHsrh8IH4fKwG5O5Y70cPH1ZLLRQ=
github.com/dgraph-io/graphql-transport-ws v0.0.0-20210511143556-2cef522f1f15 h1:X2NRsgAtVUAp2nmTPCq+x+wTcRRrj74CEpy7E0Unsl4=
github.com/dgraph-io/graphql-transport-ws v0.0.0-20210511143556-2cef522f1f15/go.mod h1:7z3c/5w0sMYYZF5bHsrh8IH4fKwG5O5Y70cPH1ZLLRQ=
github.com/dgraph-io/ristretto v0.0.4-0.20210504190834-0bf2acd73aa3 h1:jU/wpYsEL+8JPLf/QcjkQKI5g0dOjSuwcMjkThxt5x0=
github.com/dgraph-io/ristretto v0.0.4-0.20210504190834-0bf2acd73aa3/go.mod h1:fux0lOrBhrVCJd3lcTHsIJhq1T2rokOu6v9Vcb3Q9ug=
github.com/dgraph-io/roaring v0.5.6-0.20210227175938-766b897233a5 h1:9t3OKcvsQlxU9Cu0U55tgvNtaRYVGDr6rUb95P8cSbg=
Expand Down Expand Up @@ -279,8 +279,6 @@ github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0U
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/graph-gophers/graphql-go v0.0.0-20200309224638-dae41bde9ef9 h1:kLnsdud6Fl1/7ZX/5oD23cqYAzBfuZBhNkGr2NvuEsU=
github.com/graph-gophers/graphql-go v0.0.0-20200309224638-dae41bde9ef9/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc=
github.com/graph-gophers/graphql-transport-ws v0.0.0-20190611222414-40c048432299 h1:BdXUpuP9yOGKcwD/mhhZ+6EeAmrPrAQA3yeq4YEOHL4=
github.com/graph-gophers/graphql-transport-ws v0.0.0-20190611222414-40c048432299/go.mod h1:mA6VjyefgI7zd37tC3zopb51mW39gSfnEoWvtnxsqKQ=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
Expand Down
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 042d35c

Please sign in to comment.