View Source GraphQLWSClient (GraphQL-over-Websocket Client v0.1.1)
A client for connecting to GraphQL websockets that are implemented following the graphql-ws conventions.
example
Example
iex> client = GraphQLWSClient.start_link(url: "ws://localhost:4000/socket")
...>
...> {:ok, subscription_id} = GraphQLWSClient.subscribe(
...> client,
...> "subscription PostCreated { ... }"
...> )
...>
...> {:ok, _} = GraphQLWSClient.query(
...> client,
...> "mutation CreatePost { ... }"
...> )
...>
...> receive do
...> %GraphQLWSClient.Event{} = event ->
...> IO.inspect(event)
...> end
...>
...> GraphQLClient.close(client)
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.
Functions
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.
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-ws client.
Starts a graphql-ws 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.
Link to this section Functions
Closes the connection to the websocket.
Indicates whether the client is connected to the Websocket.
@spec open(client(), timeout()) :: :ok | {:error, Exception.t()}
Opens the connection to the websocket.
Opens the connection to the websocket. Raises on error.
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-ws 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-ws 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
.
subscribe(client, query, variables \\ %{}, listener \\ self(), timeout \\ 5000)
View Source@spec subscribe(client(), query(), map(), pid(), timeout()) :: {: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, "2cb5b721-5172-4ed5-84e3-2810dc2ae892"}
subscribe!(client, query, variables \\ %{}, listener \\ self(), timeout \\ 5000)
View SourceSends 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}
...> )
"ec1d38c0-e6cc-4969-994b-67d5975240aa"
@spec unsubscribe(client(), subscription_id(), timeout()) :: :ok | {:error, Exception.t()}
Removes a subscription.
example
Example
iex> GraphQLWSClient.unsubscribe(client, "db809e6f-2575-4ceb-b1af-bc7b7d04e3fd")
:ok
@spec unsubscribe!(client(), subscription_id(), timeout()) :: :ok | no_return()
Removes a subscription. Raises on error.
example
Example
iex> GraphQLWSClient.unsubscribe!(client, "c5a3cc39-ee73-4cdb-a553-ca4aab0d2b7f")
:ok