tyk
tyk copied to clipboard
[TT-10291]: support gql-go-tools verison 2
User description
Description
This PR adds the suppport of the new graphql-go-tools v2 with the use of the version 3-preview in the graphql version of the api definition
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
Type
Enhancement
Description
- Added a series of unit tests across multiple files to ensure the robustness and reliability of the new GraphQL engine configurations and their related functionalities.
- Tests cover the new
EngineV3
processing,UniversalDataGraph
configurations, proxy-only setups, and utility functions in the GraphQL Go Tools V2. - Ensures that new functionalities integrate correctly with existing systems and behave as expected under various scenarios.
Changes walkthrough
Relevant files | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Tests |
|
✨ PR-Agent usage: Comment
/help
on the PR to get a list of all available PR-Agent tools and their descriptions
PR Description updated to latest commit (https://github.com/TykTechnologies/tyk/commit/9d4c650158efcde73d12453bd21dd2579203fc4f)
PR Review
⏱️ Estimated effort to review [1-5] |
4, due to the extensive changes across multiple files, the introduction of new features, and the complexity of integrating GraphQL v2 and v3 configurations. The PR involves updates to core components like schema parsing, data source configuration, and engine setup which require careful review to ensure compatibility and correctness. |
🧪 Relevant tests |
No |
🔍 Possible issues |
Possible Bug: The PR introduces changes to the GraphQL engine configuration and handling that could potentially disrupt existing functionalities or introduce new bugs if not thoroughly tested. For example, changes in schema parsing and error handling might affect how queries are processed. |
Performance Concern: The new configurations and additional layers of processing might introduce performance overhead, especially in high-load environments. It's crucial to benchmark these changes against current performance metrics. | |
🔒 Security concerns |
No |
Code feedback:
relevant file | apidef/adapter/gqlengineadapter/v2/adapter_udg.go |
suggestion |
Consider handling errors from |
relevant line | p.Schema, err = parseSchema(p.ApiDefinition.GraphQL.Schema) |
relevant file | apidef/adapter/gqlengineadapter/v2/adapter_proxy_only.go |
suggestion |
It's recommended to validate the |
relevant line | url = strings.ReplaceAll(url, "tyk://", "http://") |
relevant file | gateway/mw_graphql.go |
suggestion |
Ensure that the new GraphQL engine (V3) initialization includes error handling similar to other engine versions. This would involve logging the error and potentially stopping further initialization if the engine setup fails, which is critical to prevent runtime panics or malfunctions. [important] |
relevant line | log.Errorf("Error creating enginev3: %v", err) |
relevant file | internal/graphengine/engine_v3.go |
suggestion |
For the |
relevant line | return err, http.StatusBadRequest |
✨ Review tool usage guide:
Overview:
The review
tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.
The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.
- When commenting, to edit configurations related to the review tool (
pr_reviewer
section), use the following template:
/review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
- With a configuration file, use the following template:
[pr_reviewer]
some_config1=...
some_config2=...
See the review usage page for a comprehensive guide on using this tool.
PR Code Suggestions
Category | Suggestions |
Best practice |
Add error handling after calling
|
Add error handling for header conversion to ensure robustness.Ensure that the apidef/adapter/gqlengineadapter/adapter_udg.go [106]
| |
Add validation for the URL after modifying its scheme to ensure it's valid.Validate the URL after replacing the "tyk://" scheme to ensure it results in a valid HTTP apidef/adapter/gqlengineadapter/v2/adapter_proxy_only.go [36]
| |
Normalize HTTP header keys using
| |
Add explicit handling for unexpected
| |
Replace
| |
Add error handling for test execution methods.Consider adding error checks for the gateway/mw_graphql_test.go [517]
| |
Enhancement |
Modify
|
Improve URL scheme replacement by using URL parsing.Replace the direct string replacement for URL schemes with a more robust URL parsing and apidef/adapter/gqlengineadapter/v2/adapter_supergraph.go [78-81]
| |
Handle URL scheme changes dynamically for multiple schemes.Use a loop or a map to handle multiple URL schemes dynamically instead of hardcoding only apidef/adapter/gqlengineadapter/v2/adapter_supergraph.go [78-81]
| |
Add logging for errors in setting the merged schema.Add error handling for the apidef/adapter/gqlengineadapter/v2/adapter_supergraph.go [30-32]
| |
Add error handling for data source configuration creation.Implement error handling for the apidef/adapter/gqlengineadapter/v2/adapter_supergraph.go [56-60]
| |
Improve URL parsing to handle cases with multiple '?' characters.The function apidef/adapter/gqlengineadapter/v2/utils.go [19-25]
| |
Add a check to prevent appending empty query values to the query configurations.The function apidef/adapter/gqlengineadapter/v2/utils.go [45-47]
| |
Ensure the HTTP method is normalized to uppercase to avoid method mismatch issues.The function apidef/adapter/gqlengineadapter/v2/utils.go [100]
| |
Performance |
Add a check for empty headers before conversion to optimize processing.Consider adding a check for empty headers before converting them in apidef/adapter/gqlengineadapter/utils_engine_v2.go [39]
|
Maintainability |
Refactor data source configuration creation into a separate function.Refactor the apidef/adapter/gqlengineadapter/v2/adapter_supergraph.go [51-66]
|
Use constants for HTTP status codes.Use a constant for the HTTP status code instead of a hard-coded value to improve code gateway/mw_graphql_test.go [501-506]
| |
Refactor repeated setup code into a helper function.Refactor repeated code blocks into a helper function to reduce code duplication and gateway/mw_graphql_test.go [413-419]
| |
Bug |
Correctly escape regex patterns in
|
✨ Improve tool usage guide:
Overview:
The improve
tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.
- When commenting, to edit configurations related to the improve tool (
pr_code_suggestions
section), use the following template:
/improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
- With a configuration file, use the following template:
[pr_code_suggestions]
some_config1=...
some_config2=...
See the improve usage page for a comprehensive guide on using this tool.
API Changes
--- prev.txt 2024-05-08 07:21:55.521930831 +0000
+++ current.txt 2024-05-08 07:21:52.269909386 +0000
@@ -1262,9 +1262,10 @@
type GraphQLConfigVersion string
const (
- GraphQLConfigVersionNone GraphQLConfigVersion = ""
- GraphQLConfigVersion1 GraphQLConfigVersion = "1"
- GraphQLConfigVersion2 GraphQLConfigVersion = "2"
+ GraphQLConfigVersionNone GraphQLConfigVersion = ""
+ GraphQLConfigVersion1 GraphQLConfigVersion = "1"
+ GraphQLConfigVersion2 GraphQLConfigVersion = "2"
+ GraphQLConfigVersion3Preview GraphQLConfigVersion = "3-preview"
)
type GraphQLEngineConfig struct {
FieldConfigs []GraphQLFieldConfig `bson:"field_configs" json:"field_configs"`
@@ -1291,15 +1292,15 @@
}
type GraphQLEngineDataSourceConfigKafka struct {
- BrokerAddresses []string `bson:"broker_addresses" json:"broker_addresses"`
- Topics []string `bson:"topics" json:"topics"`
- GroupID string `bson:"group_id" json:"group_id"`
- ClientID string `bson:"client_id" json:"client_id"`
- KafkaVersion string `bson:"kafka_version" json:"kafka_version"`
- StartConsumingLatest bool `json:"start_consuming_latest"`
- BalanceStrategy string `json:"balance_strategy"`
- IsolationLevel string `json:"isolation_level"`
- SASL kafka_datasource.SASL `json:"sasl"`
+ BrokerAddresses []string `bson:"broker_addresses" json:"broker_addresses"`
+ Topics []string `bson:"topics" json:"topics"`
+ GroupID string `bson:"group_id" json:"group_id"`
+ ClientID string `bson:"client_id" json:"client_id"`
+ KafkaVersion string `bson:"kafka_version" json:"kafka_version"`
+ StartConsumingLatest bool `json:"start_consuming_latest"`
+ BalanceStrategy string `json:"balance_strategy"`
+ IsolationLevel string `json:"isolation_level"`
+ SASL GraphQLEngineKafkaSASL `json:"sasl"`
}
type GraphQLEngineDataSourceConfigREST struct {
@@ -1312,6 +1313,12 @@
type GraphQLEngineDataSourceKind string
+type GraphQLEngineKafkaSASL struct {
+ Enable bool `json:"enable"`
+ User string `json:"user"`
+ Password string `json:"password"`
+}
+
type GraphQLExecutionMode string
GraphQLExecutionMode is the mode in which the GraphQL Middleware should
operate.
@@ -2009,6 +2016,8 @@
func (g *GraphQLConfigAdapter) EngineConfigV2() (*graphql.EngineV2Configuration, error)
+func (g *GraphQLConfigAdapter) EngineConfigV3() (*gqlv2.EngineV2Configuration, error)
+
type GraphQLConfigAdapterOption func(adapter *GraphQLConfigAdapter)
func WithHttpClient(httpClient *http.Client) GraphQLConfigAdapterOption
@@ -2017,12 +2026,18 @@
func WithStreamingClient(streamingClient *http.Client) GraphQLConfigAdapterOption
+func WithV2Schema(schema *gqlv2.Schema) GraphQLConfigAdapterOption
+
type GraphQLEngineAdapter interface {
EngineConfig() (*graphql.EngineV2Configuration, error)
}
type GraphQLEngineAdapterType int
+type GraphQLEngineAdapterV3 interface {
+ EngineConfigV3() (*gqlv2.EngineV2Configuration, error)
+}
+
type ImportAdapter interface {
Import() (*apidef.APIDefinition, error)
}
@@ -2042,6 +2057,11 @@
ErrGraphQLConfigIsMissingOperation = errors.New("graphql data source config is missing an operation")
)
+FUNCTIONS
+
+func ConvertApiDefinitionHeadersToHttpHeaders(apiDefHeaders map[string]string) http.Header
+func RemoveDuplicateApiDefinitionHeaders(headers ...map[string]string) map[string]string
+
TYPES
type ProxyOnly struct {
@@ -2076,6 +2096,49 @@
func (u *UniversalDataGraph) EngineConfig() (*graphql.EngineV2Configuration, error)
+# Package: ./apidef/adapter/gqlengineadapter/enginev3
+
+package enginev3 // import "github.com/TykTechnologies/tyk/apidef/adapter/gqlengineadapter/enginev3"
+
+
+FUNCTIONS
+
+func ConvertApiDefinitionHeadersToHttpHeaders(apiDefHeaders map[string]string) http.Header
+
+TYPES
+
+type ProxyOnly struct {
+ ApiDefinition *apidef.APIDefinition
+ HttpClient *http.Client
+ StreamingClient *http.Client
+ Schema *graphql.Schema
+
+ // Has unexported fields.
+}
+
+func (p *ProxyOnly) EngineConfigV3() (*graphql.EngineV2Configuration, error)
+
+type Supergraph struct {
+ ApiDefinition *apidef.APIDefinition
+ HttpClient *http.Client
+ StreamingClient *http.Client
+
+ // Has unexported fields.
+}
+
+func (s *Supergraph) EngineConfigV3() (*graphql.EngineV2Configuration, error)
+
+type UniversalDataGraph struct {
+ ApiDefinition *apidef.APIDefinition
+ HttpClient *http.Client
+ StreamingClient *http.Client
+ Schema *graphql.Schema
+
+ // Has unexported fields.
+}
+
+func (u *UniversalDataGraph) EngineConfigV3() (*graphql.EngineV2Configuration, error)
+
# Package: ./apidef/importer
package importer // import "github.com/TykTechnologies/tyk/apidef/importer"
Quality Gate failed
Failed conditions
59.0% Coverage on New Code (required ≥ 80%)
9.6% Duplication on New Code (required ≤ 3%)
C Reliability Rating on New Code (required ≥ A)
See analysis details on SonarCloud
Catch issues before they fail your Quality Gate with our IDE extension
SonarLint
Quality Gate failed
Failed conditions
58.6% Coverage on New Code (required ≥ 80%)
9.6% Duplication on New Code (required ≤ 3%)
C Reliability Rating on New Code (required ≥ A)
See analysis details on SonarCloud
Catch issues before they fail your Quality Gate with our IDE extension
SonarLint