graphqlws

package
v0.0.0-...-3478735 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: MIT Imports: 8 Imported by: 0

README

graphqlws

This is the implementation of the "graphql-ws" protocol defined by apollographql/subscriptions-transport-ws.

Documentation

Index

Constants

View Source
const WebSocketSubprotocol = "graphql-ws"

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	Handler ConnectionHandler
	// contains filtered or unexported fields
}

Connection represents a server-side GraphQL-WS connection.

func (*Connection) Close

func (c *Connection) Close() error

Close closes the connection. This must not be called from handler functions.

func (*Connection) SendComplete

func (c *Connection) SendComplete(ctx context.Context, id string) error

SendComplete sends the "complete" message to the client. This should be done after queries are executed or subscriptions are stopped.

func (*Connection) SendData

func (c *Connection) SendData(ctx context.Context, id string, response *graphql.Response) error

SendData sends the given GraphQL response to the client.

func (*Connection) Serve

func (c *Connection) Serve(conn *websocket.Conn)

Serve takes ownership of the given connection and begins reading / writing to it.

type ConnectionHandler

type ConnectionHandler interface {
	// Called when the server receives the init message. If an error is returned, it will be sent to
	// the client and the connection will be closed.
	HandleInit(parameters json.RawMessage) error

	// Called when the client wants to start an operation. If the operation is a query or mutation,
	// the handler should immediately call SendData followed by SendComplete. If the operation is a
	// subscription, the handler should call SendData to send events and SendComplete if/when the
	// event stream ends.
	HandleStart(id string, query string, variables map[string]interface{}, operationName string)

	// Called when the client wants to stop an operation. The handler should unsubscribe them from
	// the corresponding subscription.
	HandleStop(id string)

	// Called when an unexpected error occurs. The connection will perform the appropriate response,
	// but you may want to log it.
	LogError(err error)

	// Called when the connection begins closing and all in-flight operations should be canceled.
	Cancel()

	// Called when the connection is closed.
	HandleClose()
}

ConnectionHandler methods may be invoked on a separate goroutine, but invocations will never be made concurrently.

type Message

type Message struct {
	Id      string          `json:"id,omitempty"`
	Type    MessageType     `json:"type"`
	Payload json.RawMessage `json:"payload,omitempty"`
}

Message represents a GraphQL-WS message. This can be used for both client and server messages.

type MessageType

type MessageType string

MessageType represents a GraphQL-WS message type.

const (
	MessageTypeConnectionInit      MessageType = "connection_init"
	MessageTypeConnectionError     MessageType = "connection_error"
	MessageTypeConnectionKeepAlive MessageType = "ka"
	MessageTypeConnectionTerminate MessageType = "connection_terminate"
	MessageTypeConnectionAck       MessageType = "connection_ack"
	MessageTypeComplete            MessageType = "complete"
	MessageTypeData                MessageType = "data"
	MessageTypeStart               MessageType = "start"
	MessageTypeStop                MessageType = "stop"
	MessageTypeError               MessageType = "error"
)

MessageType represents a GraphQL-WS message type.

Jump to

Keyboard shortcuts

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