danielvladco / go-proto-gql

Protobuf plugins for generating graphql schema and golang to graphql bindings. Also supports a graphql gateway (Alpha)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it possible to forward headers in the unified Gateway?

ashwin153 opened this issue · comments

I would like to forward authentication headers from my unified GraphQL gateway to my backend gRPC services. Normally, I would register a request middleware in Nautilus. Will this library translate the HTTP headers to gRPC metadata?

For this particular purpose without changing the library, you may try inject HTTP request header using metadata.NewOutgoingContext, through a HTTP middleware:

func authForwardHandler(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		next.ServeHTTP(w, r.WithContext(
			metadata.NewOutgoingContext(r.Context(), map[string][]string{
				"authorization": {r.Header.Get("Authorization")},
			}),
		))
		return
	})
}
...
mux.Handle("/graphql", authForwardHandler(handler.NewDefaultServer(...))