Skip to content

Commit

Permalink
Pagination and sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
caiosba committed Feb 18, 2024
1 parent b53a36b commit dd33e4e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
14 changes: 11 additions & 3 deletions app/graph/types/feed_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions app/models/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/relay.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
50 changes: 50 additions & 0 deletions public/relay.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/graphql_controller_12_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dd33e4e

Please sign in to comment.