Absinthe.GraphqlWS.Socket behaviour (AbsintehGrahqlWS v0.1.0) View Source

This module is used by a custom websocket, which can then handle connections from a client implementing the GraphQL over WebSocket protocol

Options

  • schema - required - The Absinthe schema for the current application (example: MyAppWeb.Schema)
  • keepalive - optional - Interval in milliseconds to send :ping control frames over the websocket. Defaults to 30_000 (30 seconds).
  • pipeline - optional - A {module, function} tuple defining how to generate an Absinthe pipeline for each incoming message. Defaults to {Absinthe.GraphqlWS.Socket, :absinthe_pipeline}.

Example

defmodule MyAppWeb.GraphqlSocket do
  use Absinthe.GraphqlWS.Socket, schema: MyAppWeb.Schema

  def handle_message(_msg, socket) do
    {:ok, socket}
  end
end

Link to this section Summary

Types

Opcode atoms for messages handled by handle_control/2. Used by server-side keepalive messages.

Opcode atoms for messages returned by handle_in/2.

Valid replies from handle_message/2

t()

A socket that holds information necessary for parsing incoming messages as well as outgoing subscription data.

Functions

Provides a stub implementation that allows the socket to start. Phoenix.Socket.Transport expects a child spec that starts a process, so we do so with a noop Task.

When a client connects to this websocket, this function is called to initialize the socket.

Provides the default absinthe pipeline

Callbacks

Handles messages that are sent to this process through send/2, which have not been caught by the default implementation.

Link to this section Types

Specs

control() :: :ping | :pong

Opcode atoms for messages handled by handle_control/2. Used by server-side keepalive messages.

Specs

opcode() :: :text | :binary | control()

Opcode atoms for messages returned by handle_in/2.

Specs

reply() ::
  {:ok, t()}
  | {:reply, :ok, {opcode(), term()}, t()}
  | {:reply, :error, {opcode(), term()}, t()}
  | {:stop, term(), t()}

Valid replies from Absinthe.GraphqlWS.Transport.handle_in/2

Specs

response() ::
  {:ok, t()} | {:push, {opcode(), term()}, t()} | {:stop, term(), t()}

Valid replies from handle_message/2

Specs

t() :: %Absinthe.GraphqlWS.Socket{
  absinthe: map(),
  assigns: map(),
  connect_info: map(),
  endpoint: module(),
  handler: term(),
  keepalive: integer(),
  pubsub: term(),
  subscriptions: map()
}

A socket that holds information necessary for parsing incoming messages as well as outgoing subscription data.

Link to this section Functions

Link to this function

__child_spec__(module, opts, socket_opts)

View Source

Provides a stub implementation that allows the socket to start. Phoenix.Socket.Transport expects a child spec that starts a process, so we do so with a noop Task.

Link to this function

__connect__(module, socket, options)

View Source

When a client connects to this websocket, this function is called to initialize the socket.

Link to this function

absinthe_pipeline(schema, options)

View Source

Provides the default absinthe pipeline

Link to this section Callbacks

Link to this callback

handle_message(params, t)

View Source (optional)

Specs

handle_message(params :: map(), t()) :: response()

Handles messages that are sent to this process through send/2, which have not been caught by the default implementation.

Example

def handle_message({:thing, thing}, socket) do
  {:ok, assign(socket, :thing, thing)}
end

def handle_message(_msg, socket) do
  {:ok, socket}
end