apollows

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package apollows provides implementation of GraphQL over WebSocket Protocol as defined by https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md [GWS] https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md [GTWS]

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data struct {
	Value interface{}
	json.RawMessage
}

Data encapsulates both client and server json payload, combining json.RawMessage for decoding and arbitrary interface{} type for encoding

func (Data) MarshalJSON

func (payload Data) MarshalJSON() (bs []byte, err error)

MarshalJSON marshals either provided or deserialized Value as json

func (*Data) ReadPayloadData

func (payload *Data) ReadPayloadData() (*PayloadDataResponse, error)

ReadPayloadData client-side method to parse server response

func (*Data) ReadPayloadError added in v1.3.0

func (payload *Data) ReadPayloadError() (*PayloadError, error)

ReadPayloadError client-side method to parse server error response

func (*Data) ReadPayloadErrors added in v1.3.0

func (payload *Data) ReadPayloadErrors() (pds []*PayloadError, err error)

ReadPayloadErrors client-side method to parse server error response

func (*Data) UnmarshalJSON

func (payload *Data) UnmarshalJSON(bs []byte) (err error)

UnmarshalJSON stores provided json as a RawMessage, as well as initializing Value to same RawMessage to support both identity re-serialization and modification of Value after initialization

type Error added in v1.3.0

type Error interface {
	error
	EventMessageType() MessageType
}

Error providing MessageType to close websocket with

func NewSubscriberAlreadyExistsError added in v1.3.0

func NewSubscriberAlreadyExistsError(id string) Error

NewSubscriberAlreadyExistsError constructs new Error using subscriber id as part of the message

func WrapError added in v1.3.0

func WrapError(err error, messageType MessageType) Error

WrapError wraps provided error into Error

type Message

type Message struct {
	ID      string    `json:"id,omitempty"`
	Type    Operation `json:"type"`
	Payload Data      `json:"payload,omitempty"`
}

Message encapsulates every message within apollows protocol in both directions

type MessageType added in v1.3.0

type MessageType int

MessageType websocket message types / status codes used to indicate protocol-level events following closing the websocket

const (
	// EventCloseNormal standard websocket message type
	EventCloseNormal MessageType = 1000

	// EventCloseError standard websocket message type
	EventCloseError MessageType = 1006

	// EventInvalidMessage indicates invalid protocol message
	EventInvalidMessage MessageType = 4400

	// EventUnauthorized indicated attempt to subscribe to an operation before receiving OperationConnectionAck
	EventUnauthorized MessageType = 4401

	// EventInitializationTimeout indicates timeout occurring before client sending OperationConnectionInit
	EventInitializationTimeout MessageType = 4408

	// EventTooManyInitializationRequests indicates receiving more than one OperationConnectionInit
	EventTooManyInitializationRequests MessageType = 4429

	// EventSubscriberAlreadyExists indicates subscribed operation ID already being in use
	// (not yet terminated by either OperationComplete or OperationError)
	EventSubscriberAlreadyExists MessageType = 4409
)

func (MessageType) Error added in v1.3.0

func (m MessageType) Error() string

Error implementation

func (MessageType) EventMessageType added in v1.3.0

func (m MessageType) EventMessageType() MessageType

EventMessageType implementation

type Operation

type Operation string

Operation type is used to enumerate possible apollo message types

const (
	// OperationConnectionInit [GWS,GTWS]
	// is set by the connecting client to initialize the websocket state with connection params (if any)
	OperationConnectionInit Operation = "connection_init"

	// OperationStart [GWS]
	// client request initiates new operation, each operation may have 0-N OperationData responses before being
	// terminated by either OperationComplete or OperationError
	OperationStart Operation = "start"

	// OperationSubscribe [GTWS]
	// client request initiates new operation, each operation may have 0-N OperationNext responses before being
	// terminated by either OperationComplete or OperationError
	OperationSubscribe Operation = "subscribe"

	// OperationTerminate [GWS]
	// client request to gracefully close the connection, equialent to closing the websocket
	OperationTerminate Operation = "connection_terminate"

	// OperationConnectionError [GWS]
	// server response to unsuccessful OperationConnectionInit attempt
	OperationConnectionError Operation = "connection_error"

	// OperationConnectionAck [GWS,GTWS]
	// server response to successful OperationConnectionInit attempt
	OperationConnectionAck Operation = "connection_ack"

	// OperationData [GWS]
	// server response to previously initiated operation with OperationStart may be multiple within same operation,
	// specifically for subscriptions
	OperationData Operation = "data"

	// OperationNext [GTWS]
	// server response to previously initiated operation with OperationSubscribe may be multiple within same operation,
	// specifically for subscriptions
	OperationNext Operation = "next"

	// OperationError [GWS,GTWS]
	// server response to previously initiated operation with OperationStart/OperationSubscribe
	OperationError Operation = "error"

	// OperationStop [GWS]
	// client request to stop previously initiated operation with OperationStart
	OperationStop Operation = "stop"

	// OperationComplete [GWS,GTWS]
	// GWS: server response indicating previously initiated operation is complete
	// GTWS: server response indicating previously initiated operation is complete
	// GTWS: client request to stop previously initiated operation with OperationSubscribe
	OperationComplete Operation = "complete"

	// OperationKeepAlive [GWS]
	// server response sent periodically to maintain websocket connection open
	OperationKeepAlive Operation = "ka"

	// OperationPing [GTWS]
	// sever/client request for OperationPong response
	OperationPing Operation = "ping"

	// OperationPong [GTWS]
	// sever/client response for OperationPing request
	// can be sent at any time (without prior OperationPing) to maintain wesocket connection
	OperationPong Operation = "pong"
)

type PayloadData

type PayloadData struct {
	Data Data `json:"data,omitempty"`

	// see https://github.com/graph-gophers/graphql-go#custom-errors
	// for adding custom error attributes
	Errors []error `json:"errors,omitempty"`
}

PayloadData provides server-side response for previously started operation

type PayloadDataResponse

type PayloadDataResponse struct {
	Data   map[string]interface{} `json:"data,omitempty"`
	Errors []PayloadError         `json:"errors,omitempty"`
}

PayloadDataResponse provides client-side payload representation

type PayloadError

type PayloadError struct {
	Extensions map[string]interface{} `json:"extensions"`
	Message    string                 `json:"message"`
	Locations  []PayloadErrorLocation `json:"locations"`
	Path       []string               `json:"path"`
}

PayloadError client-side error representation

type PayloadErrorLocation

type PayloadErrorLocation struct {
	Line   int `json:"line"`
	Column int `json:"column"`
}

PayloadErrorLocation error location in originating request

type PayloadInit

type PayloadInit map[string]interface{}

PayloadInit provides connection params

type PayloadOperation

type PayloadOperation struct {
	Variables     map[string]interface{} `json:"variables"`
	Extensions    map[string]interface{} `json:"extensions"`
	Query         string                 `json:"query"`
	OperationName string                 `json:"operationName"`
}

PayloadOperation provides description for client-side operation initiation

type Protocol added in v1.3.0

type Protocol string

Protocol websocket subprotocol defining server behavior

const (
	// WebsocketSubprotocolGraphqlWS websocket subprotocol expected by subscriptions-transport-ws implementations
	WebsocketSubprotocolGraphqlWS Protocol = "graphql-ws"

	// WebsocketSubprotocolGraphqlTransportWS websocket subprotocol exepected by graphql-ws implementations
	WebsocketSubprotocolGraphqlTransportWS Protocol = "graphql-transport-ws"
)

Jump to

Keyboard shortcuts

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