Skip to content

fix(GraphQL): Pass on HTTP request headers for subscriptions #7806

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

Merged
merged 5 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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 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 Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ github.com/dgraph-io/gqlparser/v2 v2.2.0 h1:fKSCW8OxoMogjDwUhO9OrFvrgIA0UZspTDbc
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
26 changes: 24 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,35 @@ 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.
headerPayload, _ = ctx.Value("RequestHeader").(json.RawMessage)
if len(headerPayload) > 0 {
headers := http.Header{}
if err = json.Unmarshal(headerPayload, &headers); err != nil {
return nil, err
}
for k := range headers {
if len(strings.TrimSpace(reqHeader.Get(k))) == 0 {
reqHeader.Set(k, headers.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 +241,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