From fe415e0c4629a89773cb07284ff2315d3b19e4d3 Mon Sep 17 00:00:00 2001 From: kjgarza Date: Sat, 21 Aug 2021 07:16:24 +0200 Subject: [PATCH 1/3] cache single item graphql queries --- app/controllers/graphql_controller.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/app/controllers/graphql_controller.rb b/app/controllers/graphql_controller.rb index 4182805cc..9b6482b39 100644 --- a/app/controllers/graphql_controller.rb +++ b/app/controllers/graphql_controller.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true class GraphqlController < ApplicationController + include Cacheable before_action :authenticate_user! def execute @@ -11,12 +12,8 @@ def execute tracing_enabled: ApolloFederation::Tracing.should_add_traces(headers), current_user: current_user, } - result = - LupoSchema.execute( - query, - variables: variables, context: context, operation_name: operation_name, - ) - render json: ApolloFederation::Tracing.attach_trace_to_result(result) + result = cached_graphql_response(query, variables, context, operation_name) + render json: result rescue StandardError => e raise e unless Rails.env.development? From 22d4fd83e8c62f5551aed2161b9b5ec22ca58000 Mon Sep 17 00:00:00 2001 From: kjgarza Date: Mon, 23 Aug 2021 08:45:24 +0200 Subject: [PATCH 2/3] cache responses when graphQL request for a single resource --- app/models/concerns/cacheable.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/models/concerns/cacheable.rb b/app/models/concerns/cacheable.rb index b82bd0e7c..33f01f986 100644 --- a/app/models/concerns/cacheable.rb +++ b/app/models/concerns/cacheable.rb @@ -65,6 +65,23 @@ def cached_prefix_response(prefix, _options = {}) end end + def cached_graphql_response(query, variables, context, operation_name) + hash = Base64.encode64(query.to_json+variables.to_json+context.to_json+operation_name.to_json) + if Rails.application.config.action_controller.perform_caching && !variables[:id].nil? + Rails.cache.fetch("akita_response/#{hash}", expires_in: 24.hours) do + LupoSchema.execute( + query, + variables: variables, context: context, operation_name: operation_name, + ).to_json + end + else + LupoSchema.execute( + query, + variables: variables, context: context, operation_name: operation_name, + ) + end + end + def cached_resource_type_response(id) Rails.cache.fetch("resource_type_response/#{id}", expires_in: 1.month) do resource_type = ResourceType.where(id: id) From ba1e2e46df6155718809e296e9738269b3153c7d Mon Sep 17 00:00:00 2001 From: kjgarza Date: Tue, 7 Sep 2021 12:35:23 +0200 Subject: [PATCH 3/3] lint fix --- app/models/concerns/cacheable.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/concerns/cacheable.rb b/app/models/concerns/cacheable.rb index 33f01f986..132a55c7d 100644 --- a/app/models/concerns/cacheable.rb +++ b/app/models/concerns/cacheable.rb @@ -66,7 +66,7 @@ def cached_prefix_response(prefix, _options = {}) end def cached_graphql_response(query, variables, context, operation_name) - hash = Base64.encode64(query.to_json+variables.to_json+context.to_json+operation_name.to_json) + hash = Base64.encode64(query.to_json + variables.to_json + context.to_json + operation_name.to_json) if Rails.application.config.action_controller.perform_caching && !variables[:id].nil? Rails.cache.fetch("akita_response/#{hash}", expires_in: 24.hours) do LupoSchema.execute(