From 42f8d4e9a4e4f6f6c06165d127aa7444c04f97bd Mon Sep 17 00:00:00 2001 From: Marco Metz Date: Wed, 21 Dec 2022 12:01:03 +0100 Subject: [PATCH] Muster zur Performancesteigerung der GraphQL Anfragen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In einer Query können beliebige "nested resources" mit abgefragt werden. Diese werden bisher jedoch im ActiveRecord scope nicht mit includes berücksichtigt, sodass für jeden Treffer zusätzliche SQL anfragen notwendig werden. Pauschal immer alle möglichen Ressourcen zusätzlich zu laden ist jedoch auch keine Lösung. Passend dafür wäre es also gut schon beim scope zu wissen welche Ressourcen zusätzlich benötigt werden. Hierfür kann "extras: [:lookahead]" verwendet werden. dies gibt dem Context zusätzliches Wissen mit, welches im Scope verwendet werden kann. Beispielsweise wie hier um passende :includes an den scope ketten. Durch das hinzuladen der weiteren Models sind dann weitere Besonderheiten zu beachten, wie hier zum Beispiel die klare Benennung der Sortierargumente, da diese nun nicht mehr eindeutig sind. --- app/graphql/resolvers/event_records_search.rb | 27 ++++++++++++------- app/graphql/types/query_type.rb | 2 +- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/graphql/resolvers/event_records_search.rb b/app/graphql/resolvers/event_records_search.rb index f52d2c38..f8329da3 100644 --- a/app/graphql/resolvers/event_records_search.rb +++ b/app/graphql/resolvers/event_records_search.rb @@ -5,8 +5,17 @@ class Resolvers::EventRecordsSearch include SearchObject.module(:graphql) + extras [:lookahead] - scope { EventRecord.upcoming(context[:current_user]) } + scope { + lookahead = context[:current_arguments][:lookahead] + event_records = EventRecord.upcoming(context[:current_user]) + event_records = event_records.includes(:addresses) if lookahead.selects?(:addresses) + event_records = event_records.includes(:categories) if lookahead.selects?(:categories) + event_records = event_records.includes(:dates) if lookahead.selects?(:list_date) || lookahead.selects?(:dates) + + event_records + } type types[Types::QueryTypes::EventRecordType] @@ -88,35 +97,35 @@ def apply_location(scope, value) end def apply_order_with_created_at_desc(scope) - scope.order("created_at DESC") + scope.order("event_records.created_at DESC") end def apply_order_with_created_at_asc(scope) - scope.order("created_at ASC") + scope.order("event_records.created_at ASC") end def apply_order_with_updated_at_desc(scope) - scope.order("updated_at DESC") + scope.order("event_records.updated_at DESC") end def apply_order_with_updated_at_asc(scope) - scope.order("updated_at ASC") + scope.order("event_records.updated_at ASC") end def apply_order_with_id_desc(scope) - scope.order("id DESC") + scope.order("event_records.id DESC") end def apply_order_with_id_asc(scope) - scope.order("id ASC") + scope.order("event_records.id ASC") end def apply_order_with_title_desc(scope) - scope.order("title DESC") + scope.order("event_records.title DESC") end def apply_order_with_title_asc(scope) - scope.order("title ASC") + scope.order("event_records.title ASC") end def apply_order_with_list_date_desc(scope) diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 2a04763e..f6690f16 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -19,7 +19,7 @@ class QueryType < Types::BaseObject argument :id, ID, required: true end - field :event_records, function: Resolvers::EventRecordsSearch + field :event_records, [QueryTypes::EventRecordType], resolver: Resolvers::EventRecordsSearch, extras: [:lookahead] field :event_record, QueryTypes::EventRecordType, null: false do argument :id, ID, required: true end