Skip to content

Commit

Permalink
Add context.Context to graphql.Cache interface's methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Jan 20, 2020
1 parent a6c7aaf commit 1de22b1
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
14 changes: 8 additions & 6 deletions graphql/cache.go
@@ -1,27 +1,29 @@
package graphql

import "context"

// Cache is a shared store for APQ and query AST caching
type Cache interface {
// Get looks up a key's value from the cache.
Get(key string) (value interface{}, ok bool)
Get(ctx context.Context, key string) (value interface{}, ok bool)

// Add adds a value to the cache.
Add(key string, value interface{})
Add(ctx context.Context, key string, value interface{})
}

// MapCache is the simplest implementation of a cache, because it can not evict it should only be used in tests
type MapCache map[string]interface{}

// Get looks up a key's value from the cache.
func (m MapCache) Get(key string) (value interface{}, ok bool) {
func (m MapCache) Get(ctx context.Context, key string) (value interface{}, ok bool) {
v, ok := m[key]
return v, ok
}

// Add adds a value to the cache.
func (m MapCache) Add(key string, value interface{}) { m[key] = value }
func (m MapCache) Add(ctx context.Context, key string, value interface{}) { m[key] = value }

type NoCache struct{}

func (n NoCache) Get(key string) (value interface{}, ok bool) { return nil, false }
func (n NoCache) Add(key string, value interface{}) {}
func (n NoCache) Get(ctx context.Context, key string) (value interface{}, ok bool) { return nil, false }
func (n NoCache) Add(ctx context.Context, key string, value interface{}) {}
3 changes: 2 additions & 1 deletion graphql/executable_schema_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions graphql/handler/executor.go
Expand Up @@ -185,7 +185,7 @@ func (e executor) DispatchError(ctx context.Context, list gqlerror.List) *graphq
func (e executor) parseQuery(ctx context.Context, stats *graphql.Stats, query string) (*ast.QueryDocument, gqlerror.List) {
stats.Parsing.Start = graphql.Now()

if doc, ok := e.server.queryCache.Get(query); ok {
if doc, ok := e.server.queryCache.Get(ctx, query); ok {
now := graphql.Now()

stats.Parsing.End = now
Expand All @@ -209,7 +209,7 @@ func (e executor) parseQuery(ctx context.Context, stats *graphql.Stats, query st
return nil, listErr
}

e.server.queryCache.Add(query, doc)
e.server.queryCache.Add(ctx, query, doc)

return doc, nil
}
4 changes: 2 additions & 2 deletions graphql/handler/extension/apq.go
Expand Up @@ -72,7 +72,7 @@ func (a AutomaticPersistedQuery) MutateOperationParameters(ctx context.Context,
fullQuery := false
if rawParams.Query == "" {
// client sent optimistic query hash without query string, get it from the cache
query, ok := a.Cache.Get(extension.Sha256)
query, ok := a.Cache.Get(ctx, extension.Sha256)
if !ok {
err := gqlerror.Errorf(errPersistedQueryNotFound)
errcode.Set(err, errPersistedQueryNotFoundCode)
Expand All @@ -84,7 +84,7 @@ func (a AutomaticPersistedQuery) MutateOperationParameters(ctx context.Context,
if computeQueryHash(rawParams.Query) != extension.Sha256 {
return gqlerror.Errorf("provided APQ hash does not match query")
}
a.Cache.Add(extension.Sha256, rawParams.Query)
a.Cache.Add(ctx, extension.Sha256, rawParams.Query)
fullQuery = true
}

Expand Down
6 changes: 4 additions & 2 deletions graphql/handler/lru/lru.go
@@ -1,6 +1,8 @@
package lru

import (
"context"

"github.com/99designs/gqlgen/graphql"
lru "github.com/hashicorp/golang-lru"
)
Expand All @@ -21,10 +23,10 @@ func New(size int) *LRU {
return &LRU{cache}
}

func (l LRU) Get(key string) (value interface{}, ok bool) {
func (l LRU) Get(ctx context.Context, key string) (value interface{}, ok bool) {
return l.lru.Get(key)
}

func (l LRU) Add(key string, value interface{}) {
func (l LRU) Add(ctx context.Context, key string, value interface{}) {
l.lru.Add(key, value)
}
7 changes: 4 additions & 3 deletions graphql/handler/server_test.go
Expand Up @@ -93,6 +93,7 @@ func TestServer(t *testing.T) {
})

t.Run("query caching", func(t *testing.T) {
ctx := context.Background()
cache := &graphql.MapCache{}
srv.SetQueryCache(cache)
qry := `query Foo {name}`
Expand All @@ -102,21 +103,21 @@ func TestServer(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())

cacheDoc, ok := cache.Get(qry)
cacheDoc, ok := cache.Get(ctx, qry)
require.True(t, ok)
require.Equal(t, "Foo", cacheDoc.(*ast.QueryDocument).Operations[0].Name)
})

t.Run("cache hits use document from cache", func(t *testing.T) {
doc, err := parser.ParseQuery(&ast.Source{Input: `query Bar {name}`})
require.Nil(t, err)
cache.Add(qry, doc)
cache.Add(ctx, qry, doc)

resp := get(srv, "/foo?query="+url.QueryEscape(qry))
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())

cacheDoc, ok := cache.Get(qry)
cacheDoc, ok := cache.Get(ctx, qry)
require.True(t, ok)
require.Equal(t, "Bar", cacheDoc.(*ast.QueryDocument).Operations[0].Name)
})
Expand Down
8 changes: 4 additions & 4 deletions handler/handler.go
Expand Up @@ -226,11 +226,11 @@ type apqAdapter struct {
PersistedQueryCache
}

func (a apqAdapter) Get(key string) (value interface{}, ok bool) {
return a.PersistedQueryCache.Get(context.Background(), key)
func (a apqAdapter) Get(ctx context.Context, key string) (value interface{}, ok bool) {
return a.PersistedQueryCache.Get(ctx, key)
}
func (a apqAdapter) Add(key string, value interface{}) {
a.PersistedQueryCache.Add(context.Background(), key, value.(string))
func (a apqAdapter) Add(ctx context.Context, key string, value interface{}) {
a.PersistedQueryCache.Add(ctx, key, value.(string))
}

type PersistedQueryCache interface {
Expand Down

0 comments on commit 1de22b1

Please sign in to comment.