Skip to content

Commit

Permalink
[TT-5766] Call GraphQL execution engine to run introspection query (T…
Browse files Browse the repository at this point in the history
…ykTechnologies#4443)

* [TT-5766] Call GraphQL execution engine to run introspection query
  • Loading branch information
buraksezer authored Nov 30, 2022
1 parent cf54fe5 commit 1a34080
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions gateway/mw_graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestGraphQLMiddleware_RequestValidation(t *testing.T) {

t.Run("Introspection query with custom query type should successfully work", func(t *testing.T) {
spec.GraphQL.Schema = "schema { query: query_root } type query_root { hello: word } type word { numOfLetters: Int }"
spec.GraphQL.Version = apidef.GraphQLConfigVersion2
g.Gw.LoadAPI(spec)

request := gql.Request{
Expand Down
36 changes: 29 additions & 7 deletions gateway/reverse_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ func (p *ReverseProxy) handleGraphQL(roundTripper *TykRoundTripper, outreq *http
}

if isIntrospection {
res, err = p.handleGraphQLIntrospection()
res, err = p.handleGraphQLIntrospection(gqlRequest)
return
}
if needEngine {
Expand All @@ -983,14 +983,36 @@ func (p *ReverseProxy) handleGraphQL(roundTripper *TykRoundTripper, outreq *http
return
}

func (p *ReverseProxy) handleGraphQLIntrospection() (res *http.Response, err error) {
result, err := graphql.SchemaIntrospection(p.TykAPISpec.GraphQLExecutor.Schema)
if err != nil {
func (p *ReverseProxy) handleGraphQLIntrospection(gqlRequest *graphql.Request) (res *http.Response, err error) {
switch p.TykAPISpec.GraphQL.Version {
case apidef.GraphQLConfigVersion2:
if p.TykAPISpec.GraphQLExecutor.EngineV2 == nil {
err = errors.New("execution engine is nil")
return
}

reqCtx := context.Background()
resultWriter := graphql.NewEngineResultWriter()
err = p.TykAPISpec.GraphQLExecutor.EngineV2.Execute(reqCtx, gqlRequest, &resultWriter)
if err != nil {
return
}

httpStatus := http.StatusOK
headers := make(http.Header)
headers.Set("Content-Type", "application/json")
res = resultWriter.AsHTTPResponse(httpStatus, headers)
return
}
default:
var result *graphql.ExecutionResult
result, err = graphql.SchemaIntrospection(p.TykAPISpec.GraphQLExecutor.Schema)
if err != nil {
return
}

res = result.GetAsHTTPResponse()
return
res = result.GetAsHTTPResponse()
return
}
}

func (p *ReverseProxy) handleGraphQLEngineWebsocketUpgrade(roundTripper *TykRoundTripper, r *http.Request, w http.ResponseWriter) (res *http.Response, hijacked bool, err error) {
Expand Down

0 comments on commit 1a34080

Please sign in to comment.