Skip to content

Commit

Permalink
Merge pull request #172 from movio/171-enablingdisabling-schema-intro…
Browse files Browse the repository at this point in the history
…spection

Allow disabling of introspection via config
  • Loading branch information
pkqk authored Sep 21, 2022
2 parents 8b8e104 + 185ee08 commit 6938209
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type PluginConfig struct {
type Config struct {
IdFieldName string `json:"id-field-name"`
GatewayListenAddress string `json:"gateway-address"`
DisableIntrospection bool `json:"disable-introspection"`
MetricsListenAddress string `json:"metrics-address"`
PrivateListenAddress string `json:"private-address"`
GatewayPort int `json:"gateway-port"`
Expand Down
1 change: 1 addition & 0 deletions config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"poll-interval": "5s",
"max-requests-per-query": 50,
"max-client-response-size": 1048576,
"disable-introspection": false,
"plugins": [
{
"name": "admin-ui"
Expand Down
18 changes: 10 additions & 8 deletions executable_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,16 @@ func (s *ExecutableSchema) ExecuteQuery(ctx context.Context) *graphql.Response {
errs = append(errs, result.Errors...)
}

introspectionData := resolveIntrospectionFields(ctx, operation.SelectionSet, filteredSchema)
if len(introspectionData) > 0 {
results = append([]executionResult{
{
ServiceURL: internalServiceName,
Data: introspectionData,
},
}, results...)
if !operationCtx.DisableIntrospection {
introspectionData := resolveIntrospectionFields(ctx, operation.SelectionSet, filteredSchema)
if len(introspectionData) > 0 {
results = append([]executionResult{
{
ServiceURL: internalServiceName,
Data: introspectionData,
},
}, results...)
}
}

timings["execution"] = time.Since(executionStart).Round(time.Millisecond).String()
Expand Down
6 changes: 4 additions & 2 deletions gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (g *Gateway) UpdateSchemas(interval time.Duration) {
}

// Router returns the public http handler
func (g *Gateway) Router() http.Handler {
func (g *Gateway) Router(cfg *Config) http.Handler {
mux := http.NewServeMux()

// Duplicated from `handler.NewDefaultServer` minus
Expand All @@ -46,7 +46,9 @@ func (g *Gateway) Router() http.Handler {
gatewayHandler.AddTransport(transport.GET{})
gatewayHandler.AddTransport(transport.POST{})
gatewayHandler.AddTransport(transport.MultipartForm{})
gatewayHandler.Use(extension.Introspection{})
if !cfg.DisableIntrospection {
gatewayHandler.Use(extension.Introspection{})
}

mux.Handle("/query",
applyMiddleware(
Expand Down
8 changes: 4 additions & 4 deletions gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestGatewayQuery(t *testing.T) {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Accept", "application/json; charset=utf-8")

gtw.Router().ServeHTTP(rec, req)
gtw.Router(&Config{}).ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
assert.JSONEq(t, `{"data": { "test": "Hello" }}`, rec.Body.String())
}
Expand All @@ -74,7 +74,7 @@ func TestRequestJSONBodyLogging(t *testing.T) {
logrusLock.Lock()
defer logrusLock.Unlock()

server := NewGateway(NewExecutableSchema(nil, 50, nil), nil).Router()
server := NewGateway(NewExecutableSchema(nil, 50, nil), nil).Router(&Config{})

body := map[string]interface{}{
"foo": "bar",
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestRequestInvalidJSONBodyLogging(t *testing.T) {
logrusLock.Lock()
defer logrusLock.Unlock()

server := NewGateway(nil, nil).Router()
server := NewGateway(nil, nil).Router(&Config{})

body := `{ "invalid": "json`
jr, jw := io.Pipe()
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestRequestTextBodyLogging(t *testing.T) {
logrusLock.Lock()
defer logrusLock.Unlock()

server := NewGateway(nil, nil).Router()
server := NewGateway(nil, nil).Router(&Config{})

body := `the request body`
jr, jw := io.Pipe()
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Main() {

go runHandler(ctx, &wg, "metrics", cfg.MetricAddress(), NewMetricsHandler())
go runHandler(ctx, &wg, "private", cfg.PrivateAddress(), gtw.PrivateRouter())
go runHandler(ctx, &wg, "public", cfg.GatewayAddress(), gtw.Router())
go runHandler(ctx, &wg, "public", cfg.GatewayAddress(), gtw.Router(cfg))

wg.Wait()
}
Expand Down

0 comments on commit 6938209

Please sign in to comment.