Skip to content
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

Bypass complexity limit on __Schema queries. #1581

Merged
merged 2 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions complexity/complexity.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func (cw complexityWalker) selectionSetComplexity(selectionSet ast.SelectionSet)
switch s := selection.(type) {
case *ast.Field:
fieldDefinition := cw.schema.Types[s.Definition.Type.Name()]

if fieldDefinition.Name == "__Schema" {
continue
}

var childComplexity int
switch fieldDefinition.Kind {
case ast.Object, ast.Interface, ast.Union:
Expand Down
10 changes: 10 additions & 0 deletions graphql/handler/extension/complexity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ func TestFixedComplexity(t *testing.T) {
require.Equal(t, 2, stats.ComplexityLimit)
require.Equal(t, 4, stats.Complexity)
})

t.Run("bypass __schema field", func(t *testing.T) {
h.SetCalculatedComplexity(4)
resp := doRequest(h, "POST", "/graphql", `{ "operationName":"IntrospectionQuery", "query":"query IntrospectionQuery { __schema { queryType { name } mutationType { name }}}"}`)
require.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
require.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())

require.Equal(t, 2, stats.ComplexityLimit)
require.Equal(t, 0, stats.Complexity)
})
}

func doRequest(handler http.Handler, method string, target string, body string) *httptest.ResponseRecorder {
Expand Down