Skip to content

Commit

Permalink
chore: cr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Karol Nowak committed Dec 15, 2023
1 parent 460b86e commit cc9b991
Showing 1 changed file with 4 additions and 32 deletions.
36 changes: 4 additions & 32 deletions batchMiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@ const (
func BatchMiddleware(
cfg *struct {
SameOperationsThreshold int `inject:"config:graphql.batchMiddleware.sameOperationsThreshold,optional"`
AllOperationsThreshold int `inject:"config:graphql.batchMiddleware.sameOperationsThreshold,optional"`
AllOperationsThreshold int `inject:"config:graphql.batchMiddleware.allOperationsThreshold,optional"`
},
) func(ctx context.Context, next gql.OperationHandler) gql.ResponseHandler {
return func(ctx context.Context, next gql.OperationHandler) gql.ResponseHandler {
var sameOperationsThreshold int
sameOperationsThreshold := sameOperationsDefaultThreshold
allOperationsThreshold := allOperationsDefaultThreshold

var allOperationsThreshold int

if cfg == nil {
sameOperationsThreshold = sameOperationsDefaultThreshold
allOperationsThreshold = allOperationsDefaultThreshold
} else {
if cfg != nil {
sameOperationsThreshold = cfg.SameOperationsThreshold
allOperationsThreshold = cfg.AllOperationsThreshold
}
Expand All @@ -35,8 +31,6 @@ func BatchMiddleware(

occurrences := countTopLevelGraphQLOperations(req.Operation.SelectionSet)

countGraphqlFunctionsCalled(occurrences, req.Operation.SelectionSet)

if isAboveThreshold(sameOperationsThreshold, allOperationsThreshold, occurrences) {
return func(ctx context.Context) *gql.Response {
return gql.ErrorResponse(ctx, "request not allowed")
Expand Down Expand Up @@ -66,28 +60,6 @@ func countTopLevelGraphQLOperations(definition []ast.Selection) map[string]int {
return mapOfOccurrences
}

func countGraphqlFunctionsCalled(mapOfOccurrences map[string]int, definition []ast.Selection) {
for _, set := range definition {
field, ok := set.(*ast.Field)
if !ok {
continue
}

// counting arguments is the only way to tell if this field is a function call
if len(field.Arguments) != 0 {
if _, exists := mapOfOccurrences[field.Name]; !exists {
mapOfOccurrences[field.Name] = 0
}

mapOfOccurrences[field.Name]++
}

if len(field.SelectionSet) > 0 {
countGraphqlFunctionsCalled(mapOfOccurrences, field.SelectionSet)
}
}
}

func isAboveThreshold(threshold, operationsThreshold int, operations map[string]int) bool {
if len(operations) > operationsThreshold {
return true
Expand Down

0 comments on commit cc9b991

Please sign in to comment.