View Source GraphQLWSClient (GraphQL Websocket Client v2.0.2)
An extensible client for connecting to GraphQL websockets that are implemented following the graphql-ws conventions.
Example
Send a query or mutation and return the result immediately:
{:ok, result} = GraphQLWSClient.query(socket, "query GetPost { ... }")
Register a subscription to listen for events:
{:ok, subscription_id} = GraphQLWSClient.subscribe(
socket,
"subscription PostCreated { ... }"
)
GraphQLWSClient.query!(socket, "mutation CreatePost { ... }")
receive do
%GraphQLWSClient.Event{type: :error, id: ^subscription_id, payload: error} ->
IO.inspect(error, label: "error")
%GraphQLWSClient.Event{type: :next, id: ^subscription_id, payload: result} ->
IO.inspect(result)
%GraphQLWSClient.Event{type: :complete, id: ^subscription_id} ->
IO.puts("Stream closed")
end
GraphQLClient.close(socket)
You would usually put this inside of a custom GenServer
and handle the events
in handle_info/3
.
Alternatively, you can create a stream of results:
socket
|> GraphQLWSClient.stream!("subscription PostCreated { ... }")
|> Stream.each(fn result ->
IO.inspect(result)
end)
|> Stream.run()
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.
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 returns an event stream.
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.
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.
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
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
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
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
The first argument accept options as specified in
GraphQLWSClient.Config.new/1
. The second argument accepts
GenServer.options/0
.
stream!(client, query, variables \\ %{}, opts \\ [])
View Source (since 2.0.0)Sends a GraphQL subscription to the websocket and returns an event stream.
Options
:buffer_size
- Sets the buffer size. If the buffer is exceeded, the oldest events are discarded first. Although it is not recommended, you may also set the value to:infinity
to set no limit. Defaults to1000
.
Example
iex> stream = GraphQLWSClient.stream!(
...> client,
...> """
...> subscription CommentAdded($postId: ID!) {
...> commentAdded(postId: $postId) { body }
...> }
...> """,
...> %{"postId" => 1337}
...> )
Print events as they come in:
iex> stream
...> |> Stream.each(fn result -> IO.inspect(result) end)
...> |> Stream.run()
Wait for a fixed number of events to come in and then turn them into a list:
iex> stream |> Stream.take(3) |> Enum.to_list()
@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
iex> GraphQLWSClient.subscribe(
...> client,
...> """
...> subscription CommentAdded($postId: ID!) {
...> commentAdded(postId: $postId) { body }
...> }
...> """,
...> %{"postId" => 1337}
...> )
{:ok, "767f051c-63b7-4dd1-8d7a-af91f0ccaa71"}
Sends a GraphQL subscription to the websocket and registers a listener process to retrieve events. Raises on error.
Example
iex> GraphQLWSClient.subscribe!(
...> client,
...> """
...> subscription CommentAdded($postId: ID!) {
...> commentAdded(postId: $postId) { body }
...> }
...> """,
...> %{"postId" => 1337}
...> )
"3f59edba-27ae-4015-9740-cfb73c91c270"
@spec unsubscribe(client(), subscription_id()) :: :ok | {:error, Exception.t()}
Removes a subscription.
Example
iex> GraphQLWSClient.unsubscribe(client, "3f05c357-33a5-47ae-81d8-8da41b126126")
:ok
@spec unsubscribe!(client(), subscription_id()) :: :ok | no_return()
Removes a subscription. Raises on error.
Example
iex> GraphQLWSClient.unsubscribe!(client, "0c50dab3-1f3e-45e1-9537-bb7561a2bca3")
:ok