From f9eac5c040f084424d13ea2edc068c0cda46da9c Mon Sep 17 00:00:00 2001 From: Adomas Kizogian <100681941+adomaskizogian@users.noreply.github.com> Date: Tue, 15 Oct 2024 20:02:14 +0300 Subject: [PATCH] refactor graphql.NoCache by specifying cache key type string (#3332) --- graphql/cache.go | 12 ++++++++---- graphql/cache_test.go | 6 +++--- graphql/executor/executor.go | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/graphql/cache.go b/graphql/cache.go index 8804cfe04f4..836d98682d9 100644 --- a/graphql/cache.go +++ b/graphql/cache.go @@ -23,9 +23,13 @@ func (m MapCache[T]) Get(_ context.Context, key string) (value T, ok bool) { // Add adds a value to the cache. func (m MapCache[T]) Add(_ context.Context, key string, value T) { m[key] = value } -type NoCache[T any, T2 *T] struct{} +type NoCache[T any] struct{} -var _ Cache[*string] = (*NoCache[string, *string])(nil) +var _ Cache[string] = (*NoCache[string])(nil) -func (n NoCache[T, T2]) Get(_ context.Context, _ string) (value T2, ok bool) { return nil, false } -func (n NoCache[T, T2]) Add(_ context.Context, _ string, _ T2) {} +func (n NoCache[T]) Get(_ context.Context, _ string) (value T, ok bool) { + var val T + return val, false +} + +func (n NoCache[T]) Add(_ context.Context, _ string, _ T) {} diff --git a/graphql/cache_test.go b/graphql/cache_test.go index 35dce368c4c..bb5e5f31e6c 100644 --- a/graphql/cache_test.go +++ b/graphql/cache_test.go @@ -114,7 +114,7 @@ func TestMapCacheEdgeCases(t *testing.T) { func TestNoCache(t *testing.T) { t.Run("Add and Get", func(t *testing.T) { - cache := NoCache[string, *string]{} + cache := NoCache[*string]{} ctx := context.Background() key := "testKey" value := "testValue" @@ -131,7 +131,7 @@ func TestNoCache(t *testing.T) { func TestNoCacheMultipleEntries(t *testing.T) { t.Run("Multiple Add and Get", func(t *testing.T) { - cache := NoCache[string, *string]{} + cache := NoCache[*string]{} ctx := context.Background() // Define multiple key-value pairs @@ -190,7 +190,7 @@ func TestNoCacheEdgeCases(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - cache := NoCache[string, *string]{} + cache := NoCache[*string]{} ctx := context.Background() // Test Add diff --git a/graphql/executor/executor.go b/graphql/executor/executor.go index 566b04763e4..9c8de3efcc0 100644 --- a/graphql/executor/executor.go +++ b/graphql/executor/executor.go @@ -36,7 +36,7 @@ func New(es graphql.ExecutableSchema) *Executor { es: es, errorPresenter: graphql.DefaultErrorPresenter, recoverFunc: graphql.DefaultRecover, - queryCache: graphql.NoCache[ast.QueryDocument, *ast.QueryDocument]{}, + queryCache: graphql.NoCache[*ast.QueryDocument]{}, ext: processExtensions(nil), parserTokenLimit: parserTokenNoLimit, }