From 74d2535b5431cb5fdbeb0afe4448d5bbc1a3bfc7 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Thu, 16 May 2019 13:20:04 +0200 Subject: [PATCH] cache graphql edge counts. #274 --- Gemfile | 2 +- Gemfile.lock | 11 ++++++++--- app/graphql/types/client_connection_with_meta_type.rb | 5 +++-- app/graphql/types/funder_connection_with_meta_type.rb | 5 +++-- .../types/funder_dataset_connection_with_meta_type.rb | 5 +++-- .../funder_publication_connection_with_meta_type.rb | 5 +++-- .../funder_software_connection_with_meta_type.rb | 5 +++-- app/graphql/types/query_type.rb | 6 ++---- .../researcher_dataset_connection_with_meta_type.rb | 5 +++-- ...esearcher_publication_connection_with_meta_type.rb | 5 +++-- .../researcher_software_connection_with_meta_type.rb | 5 +++-- 11 files changed, 35 insertions(+), 24 deletions(-) diff --git a/Gemfile b/Gemfile index 56ce5a9d0..3ba275990 100644 --- a/Gemfile +++ b/Gemfile @@ -59,7 +59,7 @@ gem 'git', '~> 1.5' gem 'graphql', '~> 1.9', '>= 1.9.4' gem 'graphql-errors', '~> 0.3.0' gem 'graphql-batch', '~> 0.4.0' -gem 'graphql-cache', '~> 0.6.0' +gem 'graphql-cache', '~> 0.6.0', git: "https://github.com/stackshareio/graphql-cache" group :development, :test do gem 'rspec-rails', '~> 3.8', '>= 3.8.2' diff --git a/Gemfile.lock b/Gemfile.lock index 1c1e246cb..9f74b4d5f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,10 @@ +GIT + remote: https://github.com/stackshareio/graphql-cache + revision: 3c3f378ab8061326537ff6be203f6fd46fdf0acd + specs: + graphql-cache (0.6.0) + graphql (~> 1, > 1.8) + GEM remote: https://rubygems.org/ specs: @@ -236,8 +243,6 @@ GEM graphql-batch (0.4.0) graphql (>= 1.3, < 2) promise.rb (~> 0.7.2) - graphql-cache (0.6.0) - graphql (~> 1, > 1.8) graphql-errors (0.3.0) graphql (>= 1.6.0, < 2) haml (5.0.4) @@ -568,7 +573,7 @@ DEPENDENCIES git (~> 1.5) graphql (~> 1.9, >= 1.9.4) graphql-batch (~> 0.4.0) - graphql-cache (~> 0.6.0) + graphql-cache (~> 0.6.0)! graphql-errors (~> 0.3.0) iso8601 (~> 0.9.0) json (~> 1.8, >= 1.8.5) diff --git a/app/graphql/types/client_connection_with_meta_type.rb b/app/graphql/types/client_connection_with_meta_type.rb index 8b0414f20..4b2a78de0 100644 --- a/app/graphql/types/client_connection_with_meta_type.rb +++ b/app/graphql/types/client_connection_with_meta_type.rb @@ -2,8 +2,9 @@ class ClientConnectionWithMetaType < BaseConnection edge_type(ClientEdgeType) - - field :total_count, Integer, null: false + field_class GraphQL::Cache::Field + + field :total_count, Integer, null: false, cache: true def total_count object.nodes.size diff --git a/app/graphql/types/funder_connection_with_meta_type.rb b/app/graphql/types/funder_connection_with_meta_type.rb index 506de7d34..4f5538cd7 100644 --- a/app/graphql/types/funder_connection_with_meta_type.rb +++ b/app/graphql/types/funder_connection_with_meta_type.rb @@ -2,8 +2,9 @@ class FunderConnectionWithMetaType < BaseConnection edge_type(FunderEdgeType) - - field :total_count, Integer, null: false + field_class GraphQL::Cache::Field + + field :total_count, Integer, null: false, cache: true def total_count args = object.arguments diff --git a/app/graphql/types/funder_dataset_connection_with_meta_type.rb b/app/graphql/types/funder_dataset_connection_with_meta_type.rb index 2b1aee408..f22768139 100644 --- a/app/graphql/types/funder_dataset_connection_with_meta_type.rb +++ b/app/graphql/types/funder_dataset_connection_with_meta_type.rb @@ -2,8 +2,9 @@ class FunderDatasetConnectionWithMetaType < BaseConnection edge_type(EventDataEdgeType, edge_class: EventDataEdge) - - field :total_count, Integer, null: false + field_class GraphQL::Cache::Field + + field :total_count, Integer, null: false, cache: true def total_count Event.query(nil, obj_id: object.parent[:id], citation_type: "Dataset-Funder").dig(:meta, "total").to_i diff --git a/app/graphql/types/funder_publication_connection_with_meta_type.rb b/app/graphql/types/funder_publication_connection_with_meta_type.rb index 79989c5a7..d176c4b7b 100644 --- a/app/graphql/types/funder_publication_connection_with_meta_type.rb +++ b/app/graphql/types/funder_publication_connection_with_meta_type.rb @@ -2,8 +2,9 @@ class FunderPublicationConnectionWithMetaType < BaseConnection edge_type(EventDataEdgeType, edge_class: EventDataEdge) - - field :total_count, Integer, null: false + field_class GraphQL::Cache::Field + + field :total_count, Integer, null: false, cache: true def total_count Event.query(nil, obj_id: object.parent[:id], citation_type: "Funder-JournalArticle").dig(:meta, "total").to_i diff --git a/app/graphql/types/funder_software_connection_with_meta_type.rb b/app/graphql/types/funder_software_connection_with_meta_type.rb index 9e5f419b9..c29a4dd18 100644 --- a/app/graphql/types/funder_software_connection_with_meta_type.rb +++ b/app/graphql/types/funder_software_connection_with_meta_type.rb @@ -2,8 +2,9 @@ class FunderSoftwareConnectionWithMetaType < BaseConnection edge_type(EventDataEdgeType, edge_class: EventDataEdge) - - field :total_count, Integer, null: false + field_class GraphQL::Cache::Field + + field :total_count, Integer, null: false, cache: true def total_count Event.query(nil, obj_id: object.parent[:id], citation_type: "Funder-SoftwareSourceCode").dig(:meta, "total").to_i diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 28f879232..f8bee6ea8 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -6,8 +6,7 @@ class QueryType < BaseObject end def providers(query: nil) - ids = Provider.query(query, page: { number: 1, size: 250 }).results.to_a.map { |p| p.uid } - ElasticsearchLoader.for(Provider).load_many(ids) + Provider.query(query, page: { number: 1, size: 250 }).records.to_a end field :provider, ProviderType, null: false do @@ -25,8 +24,7 @@ def provider(id:) end def clients(query: nil, year: nil, software: nil) - ids = Client.query(query, year: year, software: software, page: { number: 1, size: 2000 }).results.to_a.map { |p| p.uid } - ElasticsearchLoader.for(Client).load_many(ids) + Client.query(query, year: year, software: software, page: { number: 1, size: 2000 }).records.to_a end field :client, ClientType, null: false do diff --git a/app/graphql/types/researcher_dataset_connection_with_meta_type.rb b/app/graphql/types/researcher_dataset_connection_with_meta_type.rb index 245954fba..4a47ec268 100644 --- a/app/graphql/types/researcher_dataset_connection_with_meta_type.rb +++ b/app/graphql/types/researcher_dataset_connection_with_meta_type.rb @@ -2,8 +2,9 @@ class ResearcherDatasetConnectionWithMetaType < BaseConnection edge_type(EventDataEdgeType, edge_class: EventDataEdge) - - field :total_count, Integer, null: false + field_class GraphQL::Cache::Field + + field :total_count, Integer, null: false, cache: true def total_count Event.query(nil, obj_id: https_to_http(object.parent[:id]), citation_type: "Dataset-Person").dig(:meta, "total").to_i diff --git a/app/graphql/types/researcher_publication_connection_with_meta_type.rb b/app/graphql/types/researcher_publication_connection_with_meta_type.rb index 3ab4cdf1d..f9508ea20 100644 --- a/app/graphql/types/researcher_publication_connection_with_meta_type.rb +++ b/app/graphql/types/researcher_publication_connection_with_meta_type.rb @@ -2,8 +2,9 @@ class ResearcherPublicationConnectionWithMetaType < BaseConnection edge_type(EventDataEdgeType, edge_class: EventDataEdge) - - field :total_count, Integer, null: false + field_class GraphQL::Cache::Field + + field :total_count, Integer, null: false, cache: true def total_count Event.query(nil, obj_id: https_to_http(object.parent[:id]), citation_type: "Person-ScholarlyArticle").dig(:meta, "total").to_i diff --git a/app/graphql/types/researcher_software_connection_with_meta_type.rb b/app/graphql/types/researcher_software_connection_with_meta_type.rb index 6294ad7ee..45765a7e9 100644 --- a/app/graphql/types/researcher_software_connection_with_meta_type.rb +++ b/app/graphql/types/researcher_software_connection_with_meta_type.rb @@ -2,8 +2,9 @@ class ResearcherSoftwareConnectionWithMetaType < BaseConnection edge_type(EventDataEdgeType, edge_class: EventDataEdge) - - field :total_count, Integer, null: false + field_class GraphQL::Cache::Field + + field :total_count, Integer, null: false, cache: true def total_count Event.query(nil, obj_id: https_to_http(object.parent[:id]), citation_type: "Person-SoftwareSourceCode").dig(:meta, "total").to_i