Skip to content

Commit

Permalink
refactor graphql.NoCache by specifying cache key type string (#3332)
Browse files Browse the repository at this point in the history
  • Loading branch information
adomaskizogian authored Oct 15, 2024
1 parent 96429c1 commit f9eac5c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
12 changes: 8 additions & 4 deletions graphql/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
6 changes: 3 additions & 3 deletions graphql/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion graphql/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down

0 comments on commit f9eac5c

Please sign in to comment.