Commit 2d1b233e authored by Alex Castaño's avatar Alex Castaño

All collections pagination

parent 1078107d
......@@ -61,13 +61,19 @@ defmodule MoodleNet do
# Community connections
def list_communities(opts \\ %{}) do
def community_list(opts \\ %{}) do
Query.new()
|> Query.with_type("MoodleNet:Community")
|> Query.paginate(opts)
|> Query.all()
end
def community_count() do
Query.new()
|> Query.with_type("MoodleNet:Community")
|> Query.count()
end
defp community_collection_query(community) do
Query.new()
|> Query.with_type("MoodleNet:Collection")
......@@ -87,6 +93,19 @@ defmodule MoodleNet do
# Collection connections
def collection_list(opts \\ %{}) do
Query.new()
|> Query.with_type("MoodleNet:Collection")
|> Query.paginate(opts)
|> Query.all()
end
def collection_count() do
Query.new()
|> Query.with_type("MoodleNet:Collection")
|> Query.count()
end
defp collection_follower_query(collection) do
Query.new()
|> Query.with_type("Person")
......
......@@ -12,7 +12,7 @@ defmodule MoodleNetWeb.GraphQL.Schema do
arg(:limit, :integer)
arg(:before, :integer)
arg(:after, :integer)
resolve(&MoodleNetSchema.list_communities/2)
resolve(&MoodleNetSchema.community_list/2)
end
@desc "Get a community"
......@@ -21,6 +21,14 @@ defmodule MoodleNetWeb.GraphQL.Schema do
resolve(MoodleNetSchema.resolve_by_id_and_type("MoodleNet:Community"))
end
@desc "Get list of collections"
field :collections, non_null(:collection_page) do
arg(:limit, :integer)
arg(:before, :integer)
arg(:after, :integer)
resolve(&MoodleNetSchema.collection_list/2)
end
@desc "Get a collection"
field :collection, :collection do
arg(:local_id, non_null(:integer))
......
......@@ -55,6 +55,12 @@ defmodule MoodleNetWeb.GraphQL.CollectionSchema do
field(:followed, non_null(:boolean), do: resolve(Resolver.with_bool_join(:follow)))
end
object :collection_page do
field(:page_info, non_null(:page_info))
field(:nodes, non_null(list_of(non_null(:collection))))
field(:total_count, non_null(:integer))
end
object :collection_followers_connection do
field(:page_info, non_null(:page_info))
field(:edges, non_null(list_of(non_null(:collection_followers_edge))))
......
......@@ -49,6 +49,7 @@ defmodule MoodleNetWeb.GraphQL.CommunitySchema do
object :community_page do
field(:page_info, non_null(:page_info))
field(:nodes, non_null(list_of(non_null(:community))))
field(:total_count, non_null(:integer))
end
object :community_collections_connection do
......
......@@ -112,11 +112,7 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchema do
# Community
def list_communities(args, info) do
comms = MoodleNet.list_communities(args)
{:ok, to_page(comms, args, info)}
end
def community_list(args, info), do: to_page(:community, args, info)
def create_community(%{community: attrs}, info) do
attrs = set_icon(attrs)
......@@ -165,6 +161,8 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchema do
### Collection
def collection_list(args, info), do: to_page(:collection, args, info)
def create_collection(%{collection: attrs, community_local_id: comm_id}, info) do
with {:ok, actor} <- current_actor(info),
{:ok, community} <- fetch(comm_id, "MoodleNet:Community"),
......@@ -590,6 +588,13 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchema do
end
end
defp calculate_connection_count(nil, method, fields) do
if "totalCount" in fields do
count_method = String.to_atom("#{method}_count")
apply(MoodleNet, count_method, [])
end
end
defp calculate_connection_count(parent, method, fields) do
if "totalCount" in fields do
count_method = String.to_atom("#{method}_count")
......@@ -597,6 +602,13 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchema do
end
end
defp calculate_connection_entities(nil, method, args, fields) do
if "pageInfo" in fields || "nodes" in fields do
list_method = String.to_atom("#{method}_list")
apply(MoodleNet, list_method, [args])
end
end
defp calculate_connection_entities(parent, method, args, fields) do
if "pageInfo" in fields || "edges" in fields do
list_method = String.to_atom("#{method}_list")
......@@ -626,18 +638,21 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchema do
|> Enum.map(fn {node, entity} -> %{cursor: entity.cursor, node: node} end)
end
defp to_page(entities, args, info) do
fields = requested_fields(info, [:edges, :node])
page_info = MoodleNet.page_info(entities, args)
nodes = prepare(entities, fields)
defp to_page(method, args, info) do
fields = requested_fields(info)
entities = calculate_connection_entities(nil, method, args, fields)
count = calculate_connection_count(nil, method, fields)
page_info = calculate_connection_page_info(entities, args, fields)
%{
page_info: %{
start_cursor: page_info.newer,
end_cursor: page_info.older
},
nodes: nodes
}
node_fields = requested_fields(info, [:nodes])
nodes = prepare(entities, node_fields)
{:ok,
%{
page_info: page_info,
nodes: nodes,
total_count: count
}}
end
def with_assoc(assoc, opts \\ [])
......
......@@ -4,6 +4,59 @@ defmodule MoodleNetWeb.GraphQL.CollectionTest do
import ActivityPub.Entity, only: [local_id: 1]
@moduletag format: :json
@tag :user
test "list", %{conn: conn, actor: actor} do
query = """
{
collections {
pageInfo {
startCursor
endCursor
}
nodes {
id
resources {
totalCount
}
}
totalCount
}
}
"""
assert ret =
conn
|> post("/api/graphql", %{query: query})
|> json_response(200)
|> Map.fetch!("data")
|> Map.fetch!("collections")
assert %{
"pageInfo" => %{"startCursor" => nil, "endCursor" => nil},
"nodes" => [],
"totalCount" => 0
} = ret
comm = Factory.community(actor)
%{id: a_id} = Factory.collection(actor, comm)
%{id: b_id} = Factory.collection(actor, comm)
assert ret =
conn
|> post("/api/graphql", %{query: query})
|> json_response(200)
|> Map.fetch!("data")
|> Map.fetch!("collections")
assert %{
"pageInfo" => %{"startCursor" => nil, "endCursor" => nil},
"nodes" => nodes,
"totalCount" => 2
} = ret
assert [%{"id" => ^b_id}, %{"id" => ^a_id}] = nodes
end
@tag :user
test "create", %{conn: conn, actor: actor} do
community = Factory.community(actor)
......
......@@ -4,6 +4,58 @@ defmodule MoodleNetWeb.GraphQL.CommunityTest do
import ActivityPub.Entity, only: [local_id: 1]
@moduletag format: :json
@tag :user
test "community list", %{conn: conn, actor: actor} do
query = """
{
communities {
pageInfo {
startCursor
endCursor
}
nodes {
id
collections {
totalCount
}
}
totalCount
}
}
"""
assert ret =
conn
|> post("/api/graphql", %{query: query})
|> json_response(200)
|> Map.fetch!("data")
|> Map.fetch!("communities")
assert %{
"pageInfo" => %{"startCursor" => nil, "endCursor" => nil},
"nodes" => [],
"totalCount" => 0
} = ret
%{id: a_id} = Factory.community(actor)
%{id: b_id} = Factory.community(actor)
assert ret =
conn
|> post("/api/graphql", %{query: query})
|> json_response(200)
|> Map.fetch!("data")
|> Map.fetch!("communities")
assert %{
"pageInfo" => %{"startCursor" => nil, "endCursor" => nil},
"nodes" => nodes,
"totalCount" => 2
} = ret
assert [%{"id" => ^b_id}, %{"id" => ^a_id}] = nodes
end
@tag :user
test "create community", %{conn: conn} do
query = """
......@@ -360,7 +412,6 @@ defmodule MoodleNetWeb.GraphQL.CommunityTest do
"edges" => []
} = community_map["members"]
query = """
mutation {
joinCommunity(
......@@ -521,5 +572,4 @@ defmodule MoodleNetWeb.GraphQL.CommunityTest do
assert ret_community == ret_community_2
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment