From ebbb1c53f5f361ceecbabbc61886d48a79ae3733 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Thu, 15 Feb 2024 10:21:51 +0000 Subject: [PATCH] Micro fixes. --- cache.go | 4 ++++ graphql.go | 3 ++- graphql_test.go | 15 +++++++++++++++ server.go | 5 +++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/cache.go b/cache.go index 4bf53e8..be8f398 100644 --- a/cache.go +++ b/cache.go @@ -23,3 +23,7 @@ func cacheLookup(hash string) []byte { } return nil } + +func cacheDelete(hash string) { + cfg.Cache.CacheClient.Delete(hash) +} diff --git a/graphql.go b/graphql.go index 67de270..8ecbfb3 100644 --- a/graphql.go +++ b/graphql.go @@ -75,6 +75,7 @@ type parseGraphQLQueryResult struct { operationName string cacheRequest bool cacheTime int + cacheRefresh bool shouldBlock bool shouldIgnore bool } @@ -144,7 +145,7 @@ func parseGraphQLQuery(c *fiber.Ctx) (res *parseGraphQLQueryResult) { } } if arg.Name.Value == "refresh" { - res.cacheRequest = arg.Value.GetValue().(bool) + res.cacheRefresh = arg.Value.GetValue().(bool) } } } diff --git a/graphql_test.go b/graphql_test.go index 4c86089..a135947 100644 --- a/graphql_test.go +++ b/graphql_test.go @@ -119,6 +119,21 @@ func (suite *Tests) Test_parseGraphQLQuery() { }, }, + { + name: "test valid query with op name, force refreshed cache", + suppliedQuery: queries{ + body: "{\"query\":\"query MyQuery @cached(refresh: true) { tg_users(where: {handle: {_eq: \\\"tozuo\\\"}}) { id __typename } }\", \"variables\": {\"id\": \"1\"}}", + }, + wantResults: results{ + is_cached: true, + cached_ttl: 0, + shouldBlock: false, + shouldIgnore: false, + op_name: "MyQuery", + op_type: "query", + }, + }, + { name: "test valid query with op name, cache and INVALID ttl", suppliedQuery: queries{ diff --git a/server.go b/server.go index 08813a4..959ba49 100644 --- a/server.go +++ b/server.go @@ -136,6 +136,11 @@ func processGraphQLRequest(c *fiber.Ctx) error { wasCached := false + if parsedResult.cacheRefresh { + cfg.Logger.Debug("Cache refresh requested via query", map[string]interface{}{"user_id": extractedUserID, "request_uuid": c.Locals("request_uuid")}) + cacheDelete(calculateHash(c)) + } + // Handling Cache Logic if parsedResult.cacheRequest || cfg.Cache.CacheEnable { cfg.Logger.Debug("Cache enabled", map[string]interface{}{"via_query": parsedResult.cacheRequest, "via_env": cfg.Cache.CacheEnable})