View Source GraphQLWSClient (GraphQL Websocket Client v1.0.1)
An extensible client for connecting to GraphQL websockets that are implemented following the graphql-ws conventions.
example
Example
{:ok, socket} = GraphQLWSClient.start_link(url: "ws://localhost:4000/socket")
{:ok, subscription_id} = GraphQLWSClient.subscribe(
socket,
"subscription PostCreated { ... }"
)
{:ok, _} = GraphQLWSClient.query(socket, "mutation CreatePost { ... }")
receive do
%GraphQLWSClient.Event{} = event ->
IO.inspect(event)
end
GraphQLClient.close(socket)
custom-client
Custom Client
If you want to run the client as part of a supervision tree in your
application, you can also use GraphQLWSClient
to create your own client.
defmodule MyClient do
use GraphQLWSClient, otp_app: :my_app
end
Then, you can configure your client using a config file:
import Config
config :my_app, MyClient,
url: "ws://localhost:4000/socket"
See GraphQLWSClient.Config.new/1
for a list of available options.
Link to this section Summary
Types
Type for a client process.
Type for a query, mutation or subscription string.
Type for a subscription ID.
Type for variables that are interpolated into the query.
Functions
The default child specification that can be used to run the client under a supervisor.
Closes the connection to the websocket.
Indicates whether the client is connected to the Websocket.
Opens the connection to the websocket.
Opens the connection to the websocket. Raises on error.
Opens the connection to the websocket using a custom payload.
Opens the connection to the websocket using a custom payload. Raises on error.
Sends a GraphQL query or mutation to the websocket and returns the result.
Sends a GraphQL query or mutation to the websocket and returns the result. Raises on error.
Starts a GraphQL Websocket client.
Starts a GraphQL Websocket client using the given config and GenServer
options.
Sends a GraphQL subscription to the websocket and registers a listener process to retrieve events.
Sends a GraphQL subscription to the websocket and registers a listener process to retrieve events. Raises on error.
Removes a subscription.
Removes a subscription. Raises on error.
Link to this section Types
@type client() :: GenServer.server()
Type for a client process.
@type query() :: String.t()
Type for a query, mutation or subscription string.
@type subscription_id() :: String.t()
Type for a subscription ID.
Type for variables that are interpolated into the query.
Link to this section Functions
@spec child_spec(term()) :: Supervisor.child_spec()
The default child specification that can be used to run the client under a supervisor.
@spec close(client()) :: :ok
Closes the connection to the websocket.
Indicates whether the client is connected to the Websocket.
@spec open(client()) :: :ok | {:error, Exception.t()}
Opens the connection to the websocket.
Opens the connection to the websocket. Raises on error.
@spec open_with(client(), any()) :: :ok | {:error, Exception.t()}
Opens the connection to the websocket using a custom payload.
Opens the connection to the websocket using a custom payload. Raises on error.
@spec query(client(), query(), variables(), nil | timeout()) :: {:ok, any()} | {:error, Exception.t()}
Sends a GraphQL query or mutation to the websocket and returns the result.
example
Example
iex> GraphQLWSClient.query(
...> client,
...> "query GetPost($id: ID!) { post(id: $id) { body } }",
...> %{"id" => 1337}
...> )
{:ok, %{"data" => %{"posts" => %{"body" => "Lorem Ipsum"}}}}
Sends a GraphQL query or mutation to the websocket and returns the result. Raises on error.
example
Example
iex> GraphQLWSClient.query!(
...> client,
...> "query GetPost($id: ID!) { post(id: $id) { body } }",
...> %{"id" => 1337}
...> )
%{"data" => %{"posts" => %{"body" => "Lorem Ipsum"}}}
@spec start_link( GraphQLWSClient.Config.t() | Keyword.t() | map() | GenServer.options() ) :: GenServer.on_start()
Starts a GraphQL Websocket client.
options
Options
See GraphQLWSClient.Config.new/1
for a list of available options.
Additionally, you may pass GenServer.options/0
.
@spec start_link( GraphQLWSClient.Config.t() | Keyword.t() | map(), GenServer.options() ) :: GenServer.on_start()
Starts a GraphQL Websocket client using the given config and GenServer
options.
options
Options
The first argument accept options as specified in
GraphQLWSClient.Config.new/1
. The second argument accepts
GenServer.options/0
.
@spec subscribe(client(), query(), variables(), pid()) :: {:ok, subscription_id()} | {:error, Exception.t()}
Sends a GraphQL subscription to the websocket and registers a listener process to retrieve events.
example
Example
iex> GraphQLWSClient.subscribe(
...> client,
...> """
...> subscription CommentAdded($postId: ID!) {
...> commentAdded(postId: $postId) { body }
...> }
...> """,
...> %{"postId" => 1337}
...> )
{:ok, "935ebe19-10f9-4f0d-a48f-4f6e68b76e94"}
Sends a GraphQL subscription to the websocket and registers a listener process to retrieve events. Raises on error.
example
Example
iex> GraphQLWSClient.subscribe!(
...> client,
...> """
...> subscription CommentAdded($postId: ID!) {
...> commentAdded(postId: $postId) { body }
...> }
...> """,
...> %{"postId" => 1337}
...> )
"4e6f65ca-de99-4f8e-9b4c-c2b6c85c4941"
@spec unsubscribe(client(), subscription_id()) :: :ok | {:error, Exception.t()}
Removes a subscription.
example
Example
iex> GraphQLWSClient.unsubscribe(client, "98c8e603-58d4-4059-90a9-b5988c1dedc7")
:ok
@spec unsubscribe!(client(), subscription_id()) :: :ok | no_return()
Removes a subscription. Raises on error.
example
Example
iex> GraphQLWSClient.unsubscribe!(client, "c58e0406-6c54-43e5-a32e-be7aed2eeea1")
:ok