Created
April 22, 2019 03:55
-
-
Save sean-clayton/d8f6b9ab5d5fbb1770722bba65220e1a to your computer and use it in GitHub Desktop.
Absinthe Relay count
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
diff --git a/lib/guildship_web/resolvers/guilds.ex b/lib/guildship_web/resolvers/guilds.ex | |
index 92501df..7a0d34e 100644 | |
--- a/lib/guildship_web/resolvers/guilds.ex | |
+++ b/lib/guildship_web/resolvers/guilds.ex | |
@@ -1,11 +1,22 @@ | |
defmodule GuildshipWeb.Resolvers.Guilds do | |
- alias Guildship.Guilds | |
+ import Ecto.Query | |
+ alias Guildship.{Repo, Guilds} | |
def guilds(_, args, _) do | |
- Absinthe.Relay.Connection.from_query( | |
- Guilds.Guild, | |
- &Guildship.Repo.all/1, | |
- args | |
- ) | |
+ query = from(g in Guilds.Guild) | |
+ count = Repo.one!(from guild in query, select: count(guild.id)) | |
+ | |
+ {:ok, result} = | |
+ Absinthe.Relay.Connection.from_query( | |
+ Guilds.Guild, | |
+ &Repo.all/1, | |
+ args | |
+ ) | |
+ | |
+ result = | |
+ result | |
+ |> Map.put(:total_count, count) | |
+ | |
+ {:ok, result} | |
end | |
end | |
diff --git a/lib/guildship_web/schema/schema.ex b/lib/guildship_web/schema/schema.ex | |
index 40ddba8..5121e5a 100644 | |
--- a/lib/guildship_web/schema/schema.ex | |
+++ b/lib/guildship_web/schema/schema.ex | |
@@ -6,7 +6,13 @@ defmodule GuildshipWeb.Schema do | |
alias GuildshipWeb.Resolvers | |
import_types Absinthe.Type.Custom | |
- connection(node_type: :guild) | |
+ | |
+ connection node_type: :guild do | |
+ field :total_count, non_null(:integer) | |
+ | |
+ edge do | |
+ end | |
+ end | |
node object(:guild) do | |
field :display_name, :string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment