View Source GraphQLWSClient (GraphQL-over-Websocket Client v0.1.4)

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.

Type for variables that are interpolated into the query.

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. 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 variables() :: %{optional(atom() | String.t()) => any()} | Keyword.t()

Type for variables that are interpolated into the query.

Link to this section Functions

Link to this function

close(client, timeout \\ 5000)

View Source
@spec close(client(), timeout()) :: :ok

Closes the connection to the websocket.

Link to this function

connected?(client, timeout \\ 5000)

View Source
@spec connected?(client(), timeout()) :: boolean()

Indicates whether the client is connected to the Websocket.

Link to this function

open(client, timeout \\ 5000)

View Source
@spec open(client(), timeout()) :: :ok | {:error, Exception.t()}

Opens the connection to the websocket.

Link to this function

open!(client, timeout \\ 5000)

View Source
@spec open!(client(), timeout()) :: :ok | no_return()

Opens the connection to the websocket. Raises on error.

Link to this function

query(client, query, variables \\ %{}, timeout \\ 5000)

View Source
@spec query(client(), query(), variables(), 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"}}}}
Link to this function

query!(client, query, variables \\ %{}, timeout \\ 5000)

View Source
@spec query!(client(), query(), variables(), timeout()) :: any() | no_return()

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"}}}

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.

Link to this function

start_link(config, opts)

View Source

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.

Link to this function

subscribe(client, query, variables \\ %{}, listener \\ self(), timeout \\ 5000)

View Source
@spec subscribe(client(), query(), variables(), 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, "8e869d5f-8bb5-430c-9b93-6743992ec093"}
Link to this function

subscribe!(client, query, variables \\ %{}, listener \\ self(), timeout \\ 5000)

View Source
@spec subscribe!(client(), query(), variables(), pid(), timeout()) ::
  subscription_id() | no_return()

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}
...> )
"8e81f8df-393e-4280-bd4f-7f9b1bc9b838"
Link to this function

unsubscribe(client, subscription_id, timeout \\ 5000)

View Source
@spec unsubscribe(client(), subscription_id(), timeout()) ::
  :ok | {:error, Exception.t()}

Removes a subscription.

example

Example

iex> GraphQLWSClient.unsubscribe(client, "986ee1eb-f8f9-4d29-9770-e30bc64a10e1")
:ok
Link to this function

unsubscribe!(client, subscription_id, timeout \\ 5000)

View Source
@spec unsubscribe!(client(), subscription_id(), timeout()) :: :ok | no_return()

Removes a subscription. Raises on error.

example

Example

iex> GraphQLWSClient.unsubscribe!(client, "28dbdfb0-9ff9-46a9-bc42-e85dfdcf809b")
:ok