forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmw_graphql_transport.go
98 lines (80 loc) · 2.75 KB
/
mw_graphql_transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package gateway
import (
"context"
"net/http"
"github.com/TykTechnologies/tyk/apidef"
)
type GraphQLEngineTransportType int
const (
GraphQLEngineTransportTypeProxyOnly GraphQLEngineTransportType = iota
GraphQLEngineTransportTypeMultiUpstream
)
func DetermineGraphQLEngineTransportType(apiSpec *APISpec) GraphQLEngineTransportType {
switch apiSpec.GraphQL.ExecutionMode {
case apidef.GraphQLExecutionModeSubgraph:
fallthrough
case apidef.GraphQLExecutionModeProxyOnly:
return GraphQLEngineTransportTypeProxyOnly
}
return GraphQLEngineTransportTypeMultiUpstream
}
type GraphQLProxyOnlyContext struct {
context.Context
forwardedRequest *http.Request
upstreamResponse *http.Response
ignoreForwardedHeaders map[string]bool
}
func NewGraphQLProxyOnlyContext(ctx context.Context, forwardedRequest *http.Request) *GraphQLProxyOnlyContext {
return &GraphQLProxyOnlyContext{
Context: ctx,
forwardedRequest: forwardedRequest,
ignoreForwardedHeaders: map[string]bool{
http.CanonicalHeaderKey("date"): true,
http.CanonicalHeaderKey("content-type"): true,
http.CanonicalHeaderKey("content-length"): true,
},
}
}
func (g *GraphQLProxyOnlyContext) Response() *http.Response {
return g.upstreamResponse
}
type GraphQLEngineTransport struct {
originalTransport http.RoundTripper
transportType GraphQLEngineTransportType
}
func NewGraphQLEngineTransport(transportType GraphQLEngineTransportType, originalTransport http.RoundTripper) *GraphQLEngineTransport {
return &GraphQLEngineTransport{
originalTransport: originalTransport,
transportType: transportType,
}
}
func (g *GraphQLEngineTransport) RoundTrip(request *http.Request) (res *http.Response, err error) {
switch g.transportType {
case GraphQLEngineTransportTypeProxyOnly:
proxyOnlyCtx, ok := request.Context().(*GraphQLProxyOnlyContext)
if ok {
return g.handleProxyOnly(proxyOnlyCtx, request)
}
}
return g.originalTransport.RoundTrip(request)
}
func (g *GraphQLEngineTransport) handleProxyOnly(proxyOnlyCtx *GraphQLProxyOnlyContext, request *http.Request) (*http.Response, error) {
request.Method = proxyOnlyCtx.forwardedRequest.Method
g.setProxyOnlyHeaders(proxyOnlyCtx, request)
response, err := g.originalTransport.RoundTrip(request)
if err != nil {
return nil, err
}
proxyOnlyCtx.upstreamResponse = response
return response, err
}
func (g *GraphQLEngineTransport) setProxyOnlyHeaders(proxyOnlyCtx *GraphQLProxyOnlyContext, r *http.Request) {
for forwardedHeaderKey, forwardedHeaderValues := range proxyOnlyCtx.forwardedRequest.Header {
if proxyOnlyCtx.ignoreForwardedHeaders[forwardedHeaderKey] {
continue
}
for _, forwardedHeaderValue := range forwardedHeaderValues {
r.Header.Add(forwardedHeaderKey, forwardedHeaderValue)
}
}
}