Last active
May 12, 2016 22:37
-
-
Save craigbeck/ef30f8dd6b158098473ce978b2aa18a1 to your computer and use it in GitHub Desktop.
extra ids in query
This file contains hidden or 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
query RootQueryType { | |
recentConversations { | |
id | |
...F2 | |
} | |
} | |
fragment F0 on Conversation { | |
id | |
unreadCount | |
phoneNumber | |
} | |
fragment F1 on ConversationConnection { | |
pageInfo { | |
hasNextPage | |
hasPreviousPage | |
startCursor | |
endCursor | |
} | |
edges { | |
node { | |
id | |
...F0 | |
} | |
cursor | |
} | |
} | |
fragment F2 on RecentConversations { | |
_conversationsHZiXO: conversations(first: 5) { | |
...F1 | |
} | |
id | |
} |
This file contains hidden or 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
query do | |
field :profile, :operator do | |
resolve fn _, %{context: %{current_user: current_user}} -> | |
Logger.debug "current_user #{inspect current_user}" | |
{:ok, Map.get(@operators, current_user.id)} | |
end | |
end | |
field :conversation, :conversation do | |
arg :id, non_null(:id) | |
resolve &ElixirGraphqlDemo.ConversationResolver.get/2 | |
end | |
field :recent_conversations, :recent_conversations do | |
resolve fn args, _ -> | |
conversations = DataRepo.all :conversation | |
{:ok, conversations} | |
end | |
end | |
node field do | |
resolve fn | |
%{type: node_type, id: id}, _ -> | |
result = DataRepo.get(node_type, id) | |
case result do | |
result -> {:ok, result} | |
nil -> {:ok, nil} | |
end | |
_, _ -> | |
Logger.debug "node resolve nil!" | |
{:ok, nil} | |
end | |
end | |
end |
This file contains hidden or 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
node object :operator do | |
field :username, :string | |
field :name, :string | |
field :email, :string | |
end | |
node object :message do | |
field :body, :string | |
field :inbound, :boolean | |
field :unread, :boolean | |
end | |
def message_from_id(id) do | |
DataRepo.get(:messages, id) | |
end | |
def get_summary(convo) do | |
unread_count = convo.message_ids | |
|> Enum.reduce(0, fn(id, acc) -> | |
case message_from_id(id).unread do | |
true -> acc + 1 | |
false -> acc | |
end | |
end) | |
convo | |
|> Map.put(:unread_count, unread_count) | |
end | |
node object :conversation do | |
field :fingerprint, :string | |
field :unread_count, :integer | |
field :phone_number, :string | |
connection field :messages, node_type: :message do | |
resolve fn | |
pagination_args, %{source: conversation} -> | |
Logger.debug "conversation #{inspect conversation} #{inspect pagination_args}" | |
{:ok, Connection.from_list( | |
Enum.map(conversation.message_ids, &message_from_id(&1)), | |
pagination_args | |
)} | |
end | |
end | |
end | |
node object :recent_conversations do | |
connection field :conversations, node_type: :conversation do | |
resolve fn | |
pagination_args, _ -> | |
Logger.debug "types.recent_conversations #{inspect pagination_args}" | |
{:ok, Connection.from_list( | |
Enum.map(DataRepo.all(:conversation), &get_summary(&1)), | |
pagination_args | |
)} | |
end | |
end | |
end | |
connection node_type: :message | |
connection node_type: :conversation | |
node interface do | |
resolve_type fn | |
%{body: _}, _ -> | |
Logger.debug "resolve_type :message" | |
:message | |
%{fingerprint: _}, _ -> | |
Logger.debug "resolve_type :conversation" | |
:conversation | |
_, _ -> | |
nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment