From dd33e4edb45e0fda86557e824e5c9b31ba7635d6 Mon Sep 17 00:00:00 2001 From: Caio <117518+caiosba@users.noreply.github.com> Date: Sun, 18 Feb 2024 19:47:44 -0300 Subject: [PATCH] Pagination and sorting --- app/graph/types/feed_type.rb | 14 ++++-- app/models/feed.rb | 4 ++ lib/relay.idl | 4 ++ public/relay.json | 50 +++++++++++++++++++ .../controllers/graphql_controller_12_test.rb | 2 +- 5 files changed, 70 insertions(+), 4 deletions(-) diff --git a/app/graph/types/feed_type.rb b/app/graph/types/feed_type.rb index fbdbb664aa..3a562fa470 100644 --- a/app/graph/types/feed_type.rb +++ b/app/graph/types/feed_type.rb @@ -54,9 +54,17 @@ def feed_invitations field :feed_teams, FeedTeamType.connection_type, null: false field :data_points, [GraphQL::Types::Int, null: true], null: true - field :clusters, ClusterType.connection_type, null: true + field :clusters_count, GraphQL::Types::Int, null: true - def clusters - object.clusters.order('id ASC') + field :clusters, ClusterType.connection_type, null: true do + argument :offset, GraphQL::Types::Int, required: false, default_value: 0 + argument :sort, GraphQL::Types::String, required: false, default_value: 'id' + argument :sort_type, GraphQL::Types::String, required: false, camelize: false, default_value: 'DESC' + end + + def clusters(offset:, sort:, sort_type:) + order = [:id, :media_count, :requests_count, :fact_checks_count, :last_request_date, :last_fact_check_date, :last_item_at, :first_item_at].include?(sort.downcase.to_sym) ? sort.downcase.to_sym : :id + order_type = sort_type.downcase.to_sym == :asc ? :asc : :desc + object.clusters.offset(offset).order(order => order_type) end end diff --git a/app/models/feed.rb b/app/models/feed.rb index 6ee6f4e43e..8b3d655af3 100755 --- a/app/models/feed.rb +++ b/app/models/feed.rb @@ -130,6 +130,10 @@ def search(args = {}) query.order(sort => sort_type).offset(args[:offset].to_i) end + def clusters_count + self.clusters.count + end + # This takes some time to run because it involves external HTTP requests and writes to the database: # 1) If the query contains a media URL, it will be downloaded... if it contains some other URL, it will be sent to Pender # 2) Requests will be made to Alegre in order to index the request media and to look for similar requests diff --git a/lib/relay.idl b/lib/relay.idl index 2b1b2b7edf..4edce3f79a 100644 --- a/lib/relay.idl +++ b/lib/relay.idl @@ -8068,7 +8068,11 @@ type Feed implements Node { Returns the last _n_ elements from the list. """ last: Int + offset: Int = 0 + sort: String = "id" + sort_type: String = "DESC" ): ClusterConnection + clusters_count: Int created_at: String current_feed_team: FeedTeam data_points: [Int] diff --git a/public/relay.json b/public/relay.json index ee82fa1d72..0974d2c6da 100644 --- a/public/relay.json +++ b/public/relay.json @@ -43812,6 +43812,42 @@ "defaultValue": null, "isDeprecated": false, "deprecationReason": null + }, + { + "name": "offset", + "description": null, + "type": { + "kind": "SCALAR", + "name": "Int", + "ofType": null + }, + "defaultValue": "0", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "sort", + "description": null, + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "defaultValue": "\"id\"", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "sort_type", + "description": null, + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "defaultValue": "\"DESC\"", + "isDeprecated": false, + "deprecationReason": null } ], "type": { @@ -43822,6 +43858,20 @@ "isDeprecated": false, "deprecationReason": null }, + { + "name": "clusters_count", + "description": null, + "args": [ + + ], + "type": { + "kind": "SCALAR", + "name": "Int", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { "name": "created_at", "description": null, diff --git a/test/controllers/graphql_controller_12_test.rb b/test/controllers/graphql_controller_12_test.rb index c6d3943773..b58add8ea2 100644 --- a/test/controllers/graphql_controller_12_test.rb +++ b/test/controllers/graphql_controller_12_test.rb @@ -316,7 +316,7 @@ def teardown n.times { create_cluster feed: f, team_ids: [@t.id], project_media: create_project_media(team: @t) } authenticate_with_user(@u) - query = 'query { feed(id: "' + f.id.to_s + '") { clusters(first: 10) { edges { node { id, dbid, first_item_at, last_item_at, last_request_date, last_fact_check_date, center { id }, teams(first: 10) { edges { node { name, avatar } } } } } } } }' + query = 'query { feed(id: "' + f.id.to_s + '") { clusters_count, clusters(first: 10) { edges { node { id, dbid, first_item_at, last_item_at, last_request_date, last_fact_check_date, center { id }, teams(first: 10) { edges { node { name, avatar } } } } } } } }' assert_queries 20, '<=' do post :create, params: { query: query } end