Last active
September 10, 2018 10:43
-
-
Save zacck-zz/028d19b0cc57ff47ccbedbdc545dab29 to your computer and use it in GitHub Desktop.
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
# set up a subscription | |
ref = push_doc(socket, subscription) | |
assert_reply(ref, :ok, %{subscriptionId: subscription_id}) | |
# run a mutation | |
assert %{data: %{"signup" => %{"username" => u_name, "id" => id}}} = | |
run!(mutation, variables: variables) | |
expected = %{ | |
result: %{ data: %{ "newUser" => %{ "username" => "#{u_name}", "id" => "#{id}"}}}, | |
subscriptionId: subscription_id | |
} | |
assert_push "subscription:data", push | |
assert expected == push | |
# fail | |
1) test New User Subscription broadcasts new user events (MenanaweApiWeb.Schema.Subscripion.NewUserTest) | |
test/lib/menanawe_api_web/subscriptions/new_user_test.exs:11 | |
No message matching %Phoenix.Socket.Message{event: "subscription:data", payload: push} after 100ms. | |
The process mailbox is empty. | |
code: assert_push "subscription:data", push | |
stacktrace: | |
test/lib/menanawe_api_web/subscriptions/new_user_test.exs:48: (test) |
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
# publish sub | |
@spec sign_up(any(), map(), any()) :: {:ok, User.t()} | {:error, String.t()} | |
def sign_up(_, %{input: params}, _) do | |
with {:ok, %User{} = usr} <- Accounts.create_user(params) do | |
ApiWeb.Endpoint | |
|> Absinthe.Subscription.publish(usr, new_user: "*") | |
{:ok, usr} | |
end | |
end | |
# subscription field | |
subscription do | |
field :new_user, :user do | |
config fn _args, _info -> | |
{:ok, topic: "*"} | |
end | |
resolve fn root, _, _ -> | |
{:ok, root} | |
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
# subscription | |
subscription do | |
field :new_user, :user do | |
config fn _args, _info -> | |
{:ok, topic: "*"} | |
end | |
trigger [:signup], topic: fn | |
%{id: id} -> [id] |> IO.inspect() | |
_ -> [] | |
end | |
resolve fn root, _, _ -> | |
{:ok, root} | |
end | |
end | |
end | |
# resolver | |
# publish sub | |
@spec sign_up(any(), map(), any()) :: {:ok, User.t()} | {:error, String.t()} | |
def sign_up(_, %{input: params}, _) do | |
with {:ok, %User{} = usr} <- Accounts.create_user(params) do | |
{:ok, usr} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment