graphqlws

package
v0.0.0-...-6b7534b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthenticateFunc

type AuthenticateFunc func(data map[string]any, conn Connection) (context.Context, error)

AuthenticateFunc is a function that resolves an auth token into a user (or returns an error if that isn't possible).

type Connection

type Connection interface {
	// ID returns the unique ID of the connection.
	ID() string

	// Context returns the context for the connection
	Context() context.Context

	// WS the websocket
	WS() *websocket.Conn

	// SendData sends results of executing an operation (typically a
	// subscription) to the client.
	SendData(string, *DataMessagePayload)

	// SendError sends an error to the client.
	SendError(error)
}

Connection is an interface to represent GraphQL WebSocket connections. Each connection is associated with an ID that is unique to the server.

func NewConnection

func NewConnection(ws *websocket.Conn, config ConnectionConfig) Connection

NewConnection establishes a GraphQL WebSocket connection. It implements the GraphQL WebSocket protocol by managing its internal state and handling the client-server communication.

type ConnectionConfig

type ConnectionConfig struct {
	Logger        logger.Logger
	Authenticate  AuthenticateFunc
	EventHandlers ConnectionEventHandlers
}

ConnectionConfig defines the configuration parameters of a GraphQL WebSocket connection.

type ConnectionEventHandlers

type ConnectionEventHandlers struct {
	// Close is called whenever the connection is closed, regardless of
	// whether this happens because of an error or a deliberate termination
	// by the client.
	Close func(Connection)

	// StartOperation is called whenever the client demands that a GraphQL
	// operation be started (typically a subscription). Event handlers
	// are expected to take the necessary steps to register the operation
	// and send data back to the client with the results eventually.
	StartOperation func(Connection, string, *StartMessagePayload) []error

	// StopOperation is called whenever the client stops a previously
	// started GraphQL operation (typically a subscription). Event handlers
	// are expected to unregister the operation and stop sending result
	// data to the client.
	StopOperation func(Connection, string)
}

ConnectionEventHandlers define the event handlers for a connection. Event handlers allow other system components to react to events such as the connection closing or an operation being started or stopped.

type DataMessagePayload

type DataMessagePayload struct {
	Data   any     `json:"data"`
	Errors []error `json:"errors"`
}

DataMessagePayload defines the result data of an operation.

type InitMessagePayload

type InitMessagePayload struct {
	AuthToken     string `json:"authToken"`
	Authorization string `json:"Authorization"`
}

InitMessagePayload defines the parameters of a connection init message.

type OperationMessage

type OperationMessage struct {
	ID      string `json:"id"`
	Type    string `json:"type"`
	Payload any    `json:"payload"`
}

OperationMessage represents a GraphQL WebSocket message.

func (OperationMessage) String

func (msg OperationMessage) String() string

type StartMessagePayload

type StartMessagePayload struct {
	Query         string         `json:"query"`
	Variables     map[string]any `json:"variables"`
	OperationName string         `json:"operationName"`
}

StartMessagePayload defines the parameters of an operation that a client requests to be started.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL