tyk icon indicating copy to clipboard operation
tyk copied to clipboard

POC: implement complexity calculation of engine [TT-10595]

Open kofoworola opened this issue 1 year ago • 5 comments

Description

POC to show the implementation of the complexity calculation algorithm in the engine

Related Issue

Motivation and Context

How This Has Been Tested

Screenshots (if appropriate)

Types of changes

  • [ ] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to change)
  • [ ] Refactoring or add test (improvements in base code or adds test coverage to functionality)

Checklist

  • [ ] I ensured that the documentation is up to date
  • [ ] I explained why this PR updates go.mod in detail with reasoning why it's required
  • [ ] I would like a code coverage CI quality gate exception and have explained why

kofoworola avatar Jan 18 '24 09:01 kofoworola

API Changes

--- prev.txt	2024-01-18 09:11:12.829427376 +0000
+++ current.txt	2024-01-18 09:11:09.741409760 +0000
@@ -805,7 +805,46 @@
                     "required": [
                         "enabled"
                     ]
-                }
+                },
+				 "complexity": {
+          			"type": ["object","null"],
+          			"properties": {
+            			"enabled": {
+              				"type": "boolean"
+            			},
+            			"max_nodes": {
+              				"type": "integer"
+						},
+						"max_depth": {
+							"type": "integer"
+						},
+						"max_complexity": {
+							"type": "integer"
+						},
+						"fields": {
+              				"type": ["array","null"],
+              				"required": ["type_name", "field_name"],
+							"properties": {
+				                "type_name": {
+									"type": "string"
+				                },
+                				"field_name": {
+									"type": "string"
+				                },
+                				"max_nodes": {
+									"type": "integer"
+				                },
+                				"max_depth": {
+				                  "type": "integer"
+                				},
+				                "max_complexity": {
+									"type": "integer"
+				                }
+							}
+			            }
+          			},
+          			"required": ["enabled"]
+				}
             },
             "required": [
                 "enabled"
@@ -1189,9 +1228,6 @@
 	PersistGraphQL          []PersistGraphQLMeta  `bson:"persist_graphql" json:"persist_graphql"`
 }
 
-func (e *ExtendedPathsSet) Clear()
-    Clear omits values that have OAS API definition conversions in place.
-
 type ExternalOAuth struct {
 	Enabled   bool       `bson:"enabled" json:"enabled"`
 	Providers []Provider `bson:"providers" json:"providers"`
@@ -1215,6 +1251,14 @@
 	SymbolName string `bson:"func_name" json:"func_name"`
 }
 
+type GraphQLComplexityConfig struct {
+	Enabled       bool                           `bson:"enabled" json:"enabled"`
+	MaxNodes      int                            `bson:"max_nodes" json:"max_nodes"`
+	MaxDepth      int                            `bson:"max_depth" json:"max_depth"`
+	MaxComplexity int                            `bson:"max_complexity" json:"max_complexity"`
+	Fields        []GraphQLFieldComplexityConfig `bson:"fields" json:"fields"`
+}
+
 type GraphQLConfig struct {
 	// Enabled indicates if GraphQL should be enabled.
 	Enabled bool `bson:"enabled" json:"enabled"`
@@ -1240,6 +1284,8 @@
 	Supergraph GraphQLSupergraphConfig `bson:"supergraph" json:"supergraph"`
 	// Introspection holds the configuration for GraphQL Introspection
 	Introspection GraphQLIntrospectionConfig `bson:"introspection" json:"introspection"`
+	// Complexity holds the configuration for graphql complexity and limitation
+	Complexity GraphQLComplexityConfig `bson:"complexity" json:"complexity"`
 }
     GraphQLConfig is the root config object for a GraphQL API.
 
@@ -1314,6 +1360,14 @@
 	// GraphQLExecutionModeSupergraph is the mode where an API is able to use subgraphs to build a supergraph in GraphQL federation.
 	GraphQLExecutionModeSupergraph GraphQLExecutionMode = "supergraph"
 )
+type GraphQLFieldComplexityConfig struct {
+	TypeName      string `bson:"type_name" json:"type_name"`
+	FieldName     string `bson:"field_name" json:"field_name"`
+	MaxNodes      int    `bson:"max_nodes" json:"max_nodes"`
+	MaxComplexity int    `bson:"max_complexity" json:"max_complexity"`
+	MaxDepth      int    `bson:"max_depth" json:"max_depth"`
+}
+
 type GraphQLFieldConfig struct {
 	TypeName              string   `bson:"type_name" json:"type_name"`
 	FieldName             string   `bson:"field_name" json:"field_name"`
@@ -6731,8 +6785,10 @@
 	ErrNoMatchingKIDFound    = errors.New("no matching KID could be found")
 )
 var (
-	ProxyingRequestFailedErr     = errors.New("there was a problem proxying the request")
-	GraphQLDepthLimitExceededErr = errors.New("depth limit exceeded")
+	ProxyingRequestFailedErr          = errors.New("there was a problem proxying the request")
+	GraphQLDepthLimitExceededErr      = errors.New("depth limit exceeded")
+	GraphQLNodeLimitExceededErr       = errors.New("max number of nodes exceeded")
+	GraphQLComplexityLimitExceededErr = errors.New("max graphql complexity reached")
 )
 var (
 	ErrNoSuitableUserIDClaimFound = errors.New("no suitable claims for user ID were found")

github-actions[bot] avatar Jan 18 '24 09:01 github-actions[bot]

Sweeping

Fixing PR: track the progress here.

I'm currently fixing this PR to address the following:

[Sweep GHA Fix] The GitHub Actions run failed with the following error logs:



Created Pull Request: https://github.com/TykTechnologies/tyk/pull/5954

sweep-ai[bot] avatar Jan 18 '24 09:01 sweep-ai[bot]

Sweeping

Fixing PR: track the progress here.

I'm currently fixing this PR to address the following:

[Sweep GHA Fix] The GitHub Actions run failed with the following error logs:



[!CAUTION]

An error has occurred: Sweep could not find any files to modify (tracking ID: 4d60f486f4)

sweep-ai[bot] avatar Jan 18 '24 09:01 sweep-ai[bot]

Sweeping

Fixing PR: track the progress here.

I'm currently fixing this PR to address the following:

[Sweep GHA Fix] The GitHub Actions run failed with the following error logs:



Created Pull Request: https://github.com/TykTechnologies/tyk/pull/5953

sweep-ai[bot] avatar Jan 18 '24 09:01 sweep-ai[bot]

Sweeping

Fixing PR: track the progress here.

I'm currently fixing this PR to address the following:

[Sweep GHA Fix] The GitHub Actions run failed with the following error logs:



Created Pull Request: https://github.com/TykTechnologies/tyk/pull/5952

sweep-ai[bot] avatar Jan 18 '24 09:01 sweep-ai[bot]