Commit d5c8aadf authored by James Laver's avatar James Laver

enable resolve_pages to automatically upgrade to batched queries for multiple...

enable resolve_pages to automatically upgrade to batched queries for multiple parents if the fetcher/2 exists
parent fed79cdb
......@@ -12,11 +12,12 @@ defmodule MoodleNet.GraphQL.ResolvePages do
single_opts: %{default_limit: 5, max_limit: 15},
batch_opts: %{default_limit: 3, max_limit: 5},
deep_opts: %{default_limit: 3, max_limit: 3},
getter_fn: &Fields.getter/1,
]
alias MoodleNet.GraphQL
alias MoodleNet.GraphQL.ResolvePages
import Absinthe.Resolution.Helpers, only: [async: 1]
import Absinthe.Resolution.Helpers, only: [async: 1, batch: 3]
def run(%ResolvePages{info: info}=rp) do
run(GraphQL.list_depth(info), Map.take(info, [:context]), rp)
......@@ -27,24 +28,36 @@ defmodule MoodleNet.GraphQL.ResolvePages do
defp run(_other, info, rp), do: run_limit(rp, info, rp.deep_opts)
defp run_full(rp, info, opts) do
GraphQL.full_page_opts(rp.page_opts, rp.cursor_validators, opts)
|> run_callback(info, rp)
with {:ok, opts} <- GraphQL.full_page_opts(rp.page_opts, rp.cursor_validators, opts) do
async fn ->
apply(rp.module, rp.fetcher, [opts, Map.take(info, [:context]), rp.context])
end
end
end
defp run_limit(rp, info, opts) do
async(fn ->
GraphQL.limit_page_opts(rp.page_opts, opts)
|> run_callback(info, rp)
end)
cond do
function_exported?(rp.module, rp.fetcher, 2) -> batch_limit(rp, info, opts)
function_exported?(rp.module, rp.fetcher, 3) -> unbatch_limit(rp, info, opts)
true -> throw {:missing_fetcher, {rp.module, rp.fetcher, [2,3]}}
end
end
defp run_callback({:ok, page_opts}, info, rp) do
apply(rp.module, rp.fetcher, [page_opts, info, rp.context])
# this is the fast path, we've actually written the scary query
defp batch_limit(rp, info, opts) do
with {:ok, opts} <- GraphQL.limit_page_opts(rp.page_opts, opts) do
batch {rp.module, rp.fetcher, {opts, Map.take(info, [:context])}},
rp.context, rp.getter_fn.(rp.context)
end
end
defp run_callback(other, _, _) do
IO.inspect(other: other)
other
# this is the slow path, we should write the scary query
defp unbatch_limit(rp, info, opts) do
with {:ok, opts} <- GraphQL.limit_page_opts(rp.page_opts, opts) do
async fn ->
apply(rp.module, rp.fetcher, [opts, Map.take(info, [:context]), rp.context])
end
end
end
end
......@@ -103,21 +103,21 @@ defmodule MoodleNetWeb.GraphQL.CommunitiesResolver do
# Flow.pages(__MODULE__, :fetch_collections_edge, page_opts, id, info, opts)
# end
def fetch_collections_edge({page_opts, info}, ids) do
user = GraphQL.current_user(info)
FetchPages.run(
%FetchPages{
queries: Collections.Queries,
query: Collection,
cursor_fn: Collections.cursor(:followers),
group_fn: &(&1.community_id),
page_opts: page_opts,
base_filters: [community: ids, user: user],
data_filters: [:default, page: [desc: [followers: page_opts]]],
count_filters: [group_count: :community_id]
}
)
end
# def fetch_collections_edge({page_opts, info}, ids) do
# user = GraphQL.current_user(info)
# FetchPages.run(
# %FetchPages{
# queries: Collections.Queries,
# query: Collection,
# cursor_fn: Collections.cursor(:followers),
# group_fn: &(&1.community_id),
# page_opts: page_opts,
# base_filters: [community: ids, user: user],
# data_filters: [:default, page: [desc: [followers: page_opts]]],
# count_filters: [group_count: :community_id],
# }
# )
# end
def fetch_collections_edge(page_opts, info, ids) do
user = GraphQL.current_user(info)
......
......@@ -64,21 +64,21 @@ defmodule MoodleNetWeb.GraphQL.FlagsResolver do
end
end
def fetch_flags_edge({page_opts, info}, ids) do
user = GraphQL.current_user(info)
FetchPages.run(
%FetchPages{
queries: Flags.Queries,
query: Flag,
cursor_fn: &[&1.id],
group_fn: &(&1.context_id),
page_opts: page_opts,
base_filters: [deleted: false, user: user, creator: ids],
data_filters: [page: [desc: [created: page_opts]]],
count_filters: [group_count: :creator_id],
}
)
end
# def fetch_flags_edge({page_opts, info}, ids) do
# user = GraphQL.current_user(info)
# FetchPages.run(
# %FetchPages{
# queries: Flags.Queries,
# query: Flag,
# cursor_fn: &[&1.id],
# group_fn: &(&1.context_id),
# page_opts: page_opts,
# base_filters: [deleted: false, user: user, creator: ids],
# data_filters: [page: [desc: [created: page_opts]]],
# count_filters: [group_count: :creator_id],
# }
# )
# end
def fetch_flags_edge(page_opts, info, ids) do
user = GraphQL.current_user(info)
......
......@@ -93,20 +93,20 @@ defmodule MoodleNetWeb.GraphQL.FollowsResolver do
)
end
def fetch_follows_edge({page_opts, info}, ids) do
user = GraphQL.current_user(info)
FetchPages.run(
%FetchPages{
queries: Follows.Queries,
query: Follow,
group_fn: &(&1.creator_id),
page_opts: page_opts,
base_filters: [creator: ids, user: user],
data_filters: [page: [desc: [created: page_opts]]],
count_filters: [group_count: :context_id],
}
)
end
# def fetch_follows_edge({page_opts, info}, ids) do
# user = GraphQL.current_user(info)
# FetchPages.run(
# %FetchPages{
# queries: Follows.Queries,
# query: Follow,
# group_fn: &(&1.creator_id),
# page_opts: page_opts,
# base_filters: [creator: ids, user: user],
# data_filters: [page: [desc: [created: page_opts]]],
# count_filters: [group_count: :context_id],
# }
# )
# end
def fetch_follows_edge(page_opts, info, ids) do
user = GraphQL.current_user(info)
......@@ -159,20 +159,20 @@ defmodule MoodleNetWeb.GraphQL.FollowsResolver do
)
end
def fetch_followers_edge({page_opts, info}, ids) do
user = GraphQL.current_user(info)
FetchPages.run(
%FetchPages{
queries: Follows.Queries,
query: Follow,
group_fn: &(&1.context_id),
page_opts: page_opts,
base_filters: [context: ids, user: user],
data_filters: [page: [desc: [created: page_opts]]],
count_filters: [group_count: :context_id],
}
)
end
# def fetch_followers_edge({page_opts, info}, ids) do
# user = GraphQL.current_user(info)
# FetchPages.run(
# %FetchPages{
# queries: Follows.Queries,
# query: Follow,
# group_fn: &(&1.context_id),
# page_opts: page_opts,
# base_filters: [context: ids, user: user],
# data_filters: [page: [desc: [created: page_opts]]],
# count_filters: [group_count: :context_id],
# }
# )
# end
def fetch_followers_edge(page_opts, info, ids) do
user = GraphQL.current_user(info)
......
......@@ -64,21 +64,21 @@ defmodule MoodleNetWeb.GraphQL.LikesResolver do
)
end
def fetch_likers_edge({page_opts, info}, ids) do
user = GraphQL.current_user(info)
FetchPages.run(
%FetchPages{
queries: Likes.Queries,
query: Like,
cursor_fn: &[&1.id],
group_fn: &(&1.context_id),
page_opts: page_opts,
base_filters: [deleted: false, user: user, context: ids],
data_filters: [page: [desc: [created: page_opts]]],
count_filters: [group_count: :context_id],
}
)
end
# def fetch_likers_edge({page_opts, info}, ids) do
# user = GraphQL.current_user(info)
# FetchPages.run(
# %FetchPages{
# queries: Likes.Queries,
# query: Like,
# cursor_fn: &[&1.id],
# group_fn: &(&1.context_id),
# page_opts: page_opts,
# base_filters: [deleted: false, user: user, context: ids],
# data_filters: [page: [desc: [created: page_opts]]],
# count_filters: [group_count: :context_id],
# }
# )
# end
def fetch_likers_edge(page_opts, info, ids) do
user = GraphQL.current_user(info)
......@@ -131,20 +131,20 @@ defmodule MoodleNetWeb.GraphQL.LikesResolver do
)
end
def fetch_likes_edge({page_opts, info}, ids) do
user = GraphQL.current_user(info)
FetchPages.run(
%FetchPages{
queries: Likes.Queries,
query: Like,
group_fn: &(&1.context_id),
page_opts: page_opts,
base_filters: [deleted: false, user: user, creator: ids],
data_filters: [page: [desc: [created: page_opts]]],
count_filters: [group_count: :creator_id],
}
)
end
# def fetch_likes_edge({page_opts, info}, ids) do
# user = GraphQL.current_user(info)
# FetchPages.run(
# %FetchPages{
# queries: Likes.Queries,
# query: Like,
# group_fn: &(&1.context_id),
# page_opts: page_opts,
# base_filters: [deleted: false, user: user, creator: ids],
# data_filters: [page: [desc: [created: page_opts]]],
# count_filters: [group_count: :creator_id],
# }
# )
# end
def fetch_likes_edge(page_opts, info, ids) do
user = GraphQL.current_user(info)
......
......@@ -42,21 +42,21 @@ defmodule MoodleNetWeb.GraphQL.ThreadsResolver do
)
end
def fetch_threads_edge({page_opts, info}, ids) do
user = GraphQL.current_user(info)
FetchPages.run(
%FetchPages{
queries: Threads.Queries,
query: Thread,
cursor_fn: Threads.cursor(:followers),
group_fn: &(&1.context_id),
page_opts: page_opts,
base_filters: [user: user, context: ids],
data_filters: [page: [desc: [followers: page_opts]]],
count_filters: [group_count: :context_id],
}
)
end
# def fetch_threads_edge({page_opts, info}, ids) do
# user = GraphQL.current_user(info)
# FetchPages.run(
# %FetchPages{
# queries: Threads.Queries,
# query: Thread,
# cursor_fn: Threads.cursor(:followers),
# group_fn: &(&1.context_id),
# page_opts: page_opts,
# base_filters: [user: user, context: ids],
# data_filters: [page: [desc: [followers: page_opts]]],
# count_filters: [group_count: :context_id],
# }
# )
# end
def fetch_threads_edge(page_opts, info, ids) do
user = GraphQL.current_user(info)
......
......@@ -77,4 +77,5 @@ defmodule MoodleNetWeb.GraphQL.UploadResolver do
def content_upload(%Content{content_upload: upload}, _, _info), do: {:ok, upload}
def content_mirror(%Content{content_mirror: mirror}, _, _info), do: {:ok, mirror}
end
......@@ -67,21 +67,21 @@ defmodule MoodleNetWeb.GraphQL.UsersResolver do
)
end
def fetch_comments_edge({page_opts, info}, ids) do
user = GraphQL.current_user(info)
FetchPages.run(
%FetchPages{
queries: CommentsQueries,
query: Comment,
cursor_fn: &(&1.id),
group_fn: &(&1.creator_id),
page_opts: page_opts,
base_filters: [user: user, creator: ids],
data_filters: [order: :timeline_desc],
count_filters: [group_count: :creator_id],
}
)
end
# def fetch_comments_edge({page_opts, info}, ids) do
# user = GraphQL.current_user(info)
# FetchPages.run(
# %FetchPages{
# queries: CommentsQueries,
# query: Comment,
# cursor_fn: &(&1.id),
# group_fn: &(&1.creator_id),
# page_opts: page_opts,
# base_filters: [user: user, creator: ids],
# data_filters: [order: :timeline_desc],
# count_filters: [group_count: :creator_id],
# }
# )
# end
def fetch_comments_edge(page_opts, info, ids) do
user = GraphQL.current_user(info)
......@@ -118,20 +118,20 @@ defmodule MoodleNetWeb.GraphQL.UsersResolver do
)
end
def fetch_collection_follows_edge({page_opts, info}, ids) do
user = GraphQL.current_user(info)
FetchPages.run(
%FetchPages{
queries: Follows.Queries,
query: Follows.Follow,
group_fn: &(&1.creator_id),
page_opts: page_opts,
base_filters: [user: user, creator: ids, join: :context, table: Collection],
data_filters: [page: [desc: [created: page_opts]], preload: :context],
count_filters: [group_count: :context_id],
}
)
end
# def fetch_collection_follows_edge({page_opts, info}, ids) do
# user = GraphQL.current_user(info)
# FetchPages.run(
# %FetchPages{
# queries: Follows.Queries,
# query: Follows.Follow,
# group_fn: &(&1.creator_id),
# page_opts: page_opts,
# base_filters: [user: user, creator: ids, join: :context, table: Collection],
# data_filters: [page: [desc: [created: page_opts]], preload: :context],
# count_filters: [group_count: :context_id],
# }
# )
# end
def fetch_collection_follows_edge(page_opts, info, ids) do
user = GraphQL.current_user(info)
......@@ -161,20 +161,20 @@ defmodule MoodleNetWeb.GraphQL.UsersResolver do
)
end
def fetch_community_follows_edge({page_opts, info}, ids) do
user = GraphQL.current_user(info)
FetchPages.run(
%FetchPages{
queries: Follows.Queries,
query: Follows.Follow,
group_fn: &(&1.creator_id),
page_opts: page_opts,
base_filters: [user: user, creator: ids, join: :context, table: Community],
data_filters: [page: [desc: [created: page_opts]], preload: :context],
count_filters: [group_count: :context_id]
}
)
end
# def fetch_community_follows_edge({page_opts, info}, ids) do
# user = GraphQL.current_user(info)
# FetchPages.run(
# %FetchPages{
# queries: Follows.Queries,
# query: Follows.Follow,
# group_fn: &(&1.creator_id),
# page_opts: page_opts,
# base_filters: [user: user, creator: ids, join: :context, table: Community],
# data_filters: [page: [desc: [created: page_opts]], preload: :context],
# count_filters: [group_count: :context_id]
# }
# )
# end
def fetch_community_follows_edge(page_opts, info, ids) do
user = GraphQL.current_user(info)
......@@ -206,21 +206,21 @@ defmodule MoodleNetWeb.GraphQL.UsersResolver do
)
end
def fetch_user_follows_edge({page_opts, info}, ids) do
user = GraphQL.current_user(info)
FetchPages.run(
%FetchPages{
queries: Follows.Queries,
query: Follow,
cursor_fn: &[&1.id],
group_fn: &(&1.creator_id),
page_opts: page_opts,
base_filters: [user: user, creator: ids, join: :context, table: User],
data_filters: [page: [desc: [created: page_opts]], preload: :context],
count_filters: [group_count: :context_id]
}
)
end
# def fetch_user_follows_edge({page_opts, info}, ids) do
# user = GraphQL.current_user(info)
# FetchPages.run(
# %FetchPages{
# queries: Follows.Queries,
# query: Follow,
# cursor_fn: &[&1.id],
# group_fn: &(&1.creator_id),
# page_opts: page_opts,
# base_filters: [user: user, creator: ids, join: :context, table: User],
# data_filters: [page: [desc: [created: page_opts]], preload: :context],
# count_filters: [group_count: :context_id]
# }
# )
# end
def fetch_user_follows_edge(page_opts, info, ids) do
user = GraphQL.current_user(info)
......
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