Skip to content

Instantly share code, notes, and snippets.

@zacck-zz
Last active September 7, 2018 08:47
Show Gist options
  • Save zacck-zz/9b6cdd438dae2f1a388905985961bd61 to your computer and use it in GitHub Desktop.
Save zacck-zz/9b6cdd438dae2f1a388905985961bd61 to your computer and use it in GitHub Desktop.
# subscription
subscription do
field :new_user, :user do
config fn _args, _info ->
{:ok, topic: "*"}
end
end
end
# publisher
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
# test
describe "New User Subscription" do
@subscription """
subscription {
newUser{
id
username
}
}
"""
@mutation """
mutation($input: SignUpInput!){
signup(input: $input) {
username
}
}
"""
test "broadcasts new user events", %{socket: socket} do
variables = %{
"input" => %{
"username" => "Zacck Osiemo",
"password" => "password"
}
}
# set up a subscription
ref = push_doc socket, @subscription
assert_reply ref, :ok, %{subscriptionId: subscription_id}
# run a mutation
ref = push_doc socket, @mutation, variables: variables
assert_reply ref, :ok, reply
assert %{data: %{"signup" => %{"username" => u_name}}} = reply
end
end
end
# test fail
test New User Subscription broadcasts new user events (ApiWeb.Schema.Subscripion.NewUserTest)
test/lib/api_web/subscriptions/new_user_test.exs:26
No message matching %Phoenix.Socket.Reply{ref: ^ref, status: :ok, payload: reply} after 100ms.
The process mailbox is empty.
code: assert_reply ref, :ok, reply
stacktrace:
test/lib/api_web/subscriptions/new_user_test.exs:43: (test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment