SubscriptionsTransportWS.SocketTest (SubscriptionsTransportWS v1.0.1) View Source
Helper module for testing socket behaviours.
Usage
# Example socket
defmodule GraphqlSocket do
use SubscriptionsTransportWS.Socket, schema: TestSchema, keep_alive: 10
@impl true
def connect(params, socket) do
{:ok, socket}
end
@impl true
def gql_connection_init(message, socket) do
{:ok, socket}
end
end
# Endpoint routes to the socket
defmodule YourApp.Endpoint do
use Phoenix.Endpoint, otp_app: :subscription_transport_ws
use Absinthe.Phoenix.Endpoint
socket("/ws", GraphqlSocket, websocket: [subprotocols: ["graphql-ws"]])
# ... rest of your endpoint
end
# Test suite
defmodule SomeTest do
use ExUnit.Case
import SubscriptionsTransportWS.SocketTest
@endpoint YourApp.Endpoint
test "a test" do
socket(GraphqlSocket, TestSchema)
# Push query over socket and receive response
assert {:ok, %{"data" => %{"posts" => [%{"body" => "body1", "id" => "aa"}]}}, _socket} = push_doc(socket, "query {
posts {
id
body
}
}", variables: %{limit: 10})
# Subscribe to subscription
{:ok, socket} = push_doc(socket, "subscription {
postAdded{
id
body
title
}
}", variables: %{})
end
end
Link to this section Summary
Functions
Helper function to receive subscription data over the socket
Initiates a transport connection for the socket handler.
Useful for testing UserSocket authentication. Returns
the result of the handler's connect/3
callback.
Helper function for the connection_init
message in the subscriptions-transport-ws
protocol. Calls the gql_connection_init(message, socket)
on the socket handler.
Helper function to push a GraphQL document to a socket.
Helper function to build a socket.
Link to this section Functions
assert_receive_subscription(payload, timeout \\ Application.fetch_env!(:ex_unit, :assert_receive_timeout))
View Source (macro)Helper function to receive subscription data over the socket
Example
push_doc(socket, "mutation submitPost($title: String, $body: String){
submitPost(title: $title, body: $body){
id
body
title
}
}", variables: %{title: "test title", body: "test body"})
assert_receive_subscription %{
"data" => %{
"postAdded" => %{"body" => "test body", "id" => "1", "title" => "test title"}
}
}
Initiates a transport connection for the socket handler.
Useful for testing UserSocket authentication. Returns
the result of the handler's connect/3
callback.
Helper function for the connection_init
message in the subscriptions-transport-ws
protocol. Calls the gql_connection_init(message, socket)
on the socket handler.
Specs
push_doc( socket :: SubscriptionsTransportWS.Socket.t(), document :: String.t(), opts :: [{:variables, map()}] ) :: {:ok, SubscriptionsTransportWS.Socket.t()} | {:ok, result :: map(), SubscriptionsTransportWS.Socket.t()}
Helper function to push a GraphQL document to a socket.
The only option that is used is opts[:variables]
- all other options are
ignored.
When you push a query/mutation it will return with {:ok, result, socket}
. For
subscriptions it will return an {:ok, socket}
tuple.
Example of synchronous response
# Push query over socket and receive response
push_doc(socket, "query {
posts {
id
body
}
}", variables: %{limit: 10})
{:ok, %{"data" => %{"posts" => [%{"body" => "body1", "id" => "aa"}]}}, _socket}
Example of asynchronous response
# Subscribe to subscription
push_doc(socket, "subscription {
postAdded{
id
body
title
}
}", variables: %{})
# The submitPost mutation triggers the postAdded subscription publication
push_doc(socket, "mutation submitPost($title: String, $body: String){
submitPost(title: $title, body: $body){
id
body
title
}
}", variables: %{title: "test title", body: "test body"})
assert_receive_subscription(%{
"data" => %{
"postAdded" => %{"body" => "test body", "id" => "1", "title" => "test title"}
}
})
Helper function to build a socket.
Example
iex> socket = socket(GraphqlSocket, TestSchema)