Created
April 10, 2017 23:44
-
-
Save LawJolla/600ee13f42c9dfd9beda449c05a5ebed to your computer and use it in GitHub Desktop.
Absinthe Root Query Pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule WkGraphql.Schema do | |
use Absinthe.Schema | |
import_types WkGraphql.Schema.Types | |
query do | |
@desc "Get a user" | |
field :user, :user do | |
arg :id, non_null(:integer) | |
resolve &WkGraphql.UserResolver.find/2 | |
end | |
@desc "Get all users" | |
field :users, list_of(:user) do | |
resolve &WkGraphql.UsersResolver.all/2 | |
end | |
# field :threads_list, :threads do | |
# resolve &WkGraphql.MessageResolver.find_all_threads/2 | |
# end | |
# @desc "Get all message threads" | |
# field :threads, list_of(:thread) do | |
# resolve &WkGraphql.MessageResolver.find_all_threads/2 | |
# end | |
connection field :threads, node_type: :thread do | |
arg :first, non_null(:integer) | |
arg :last, :string | |
resolve fn | |
p_args, %{source: thread, context: %{uuid: uuid, current_user: user}} -> | |
thread_id = get_thread_id(thread) | |
with thread <- Messages.find_all_threads_query(thread, user, uuid), | |
connection <- Absinthe.Relay.Connection.from_query(thread, &WkGraphql.Repo.all/1, p_args) do | |
{:ok, connection} | |
else | |
{:error, error} -> {:error, error} | |
_ -> {:error, "Error fetching Threads"} | |
end | |
end | |
end | |
@desc "Find a thread" | |
field :thread, :thread do | |
arg :thread_id, non_null(:integer) | |
resolve &WkGraphql.MessageResolver.find_thread_by_id/2 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solved! I have to return Absinthe.Relay.Connection.from_query(thread, &WkGraphql.Repo.all/1, p_args) directly
It is worth noting
source: thread
is unecessary because you are list the root object. You have to use source when you are listing associated object ( https://hexdocs.pm/absinthe_relay/Absinthe.Relay.Connection.html#module-connection )