Documentation ¶
Index ¶
- Constants
- func ErrorsFromGraphQLErrors(errors []gqlerrors.FormattedError) []error
- func NewHandler(config HandlerConfig) http.Handler
- func NewLogger(prefix string) *log.Entry
- func SubscriptionFieldNamesFromDocument(doc *ast.Document) []string
- func ValidateSubscription(s SubscriptionInterface) []error
- type AuthenticateFunc
- type Connection
- type ConnectionConfig
- type ConnectionControlMessageHandlers
- type ConnectionEventHandlers
- type ConnectionFactory
- type ConnectionSubscriptions
- type DataMessagePayload
- type HandlerConfig
- type InitMessagePayload
- type MultipleTopic
- type OperationMessage
- type StartMessagePayload
- type StringTopic
- type Subscriber
- type Subscription
- func (s *Subscription) GetConnection() Connection
- func (s *Subscription) GetDocument() *ast.Document
- func (s *Subscription) GetFields() []string
- func (s *Subscription) GetID() string
- func (s *Subscription) GetOperationName() string
- func (s *Subscription) GetQuery() string
- func (s *Subscription) GetSendData() SubscriptionSendDataFunc
- func (s *Subscription) GetVariables() map[string]interface{}
- func (s *Subscription) MatchesField(field string) bool
- func (s *Subscription) SetDocument(value *ast.Document)
- func (s *Subscription) SetFields(value []string)
- type SubscriptionField
- type SubscriptionInterface
- type SubscriptionManager
- type SubscriptionSendDataFunc
- type Subscriptions
- type Topic
Constants ¶
const PUBLISHED_DATA = "PUBLISHED_DATA"
Variables ¶
This section is empty.
Functions ¶
func ErrorsFromGraphQLErrors ¶
func ErrorsFromGraphQLErrors(errors []gqlerrors.FormattedError) []error
ErrorsFromGraphQLErrors convert from GraphQL errors to regular errors.
func NewHandler ¶
func NewHandler(config HandlerConfig) http.Handler
NewHandler creates a WebSocket handler for GraphQL WebSocket connections. This handler takes a SubscriptionManager and adds/removes subscriptions as they are started/stopped by the client.
func NewLogger ¶
NewLogger returns a beautiful logger that logs messages with a given prefix (typically the name of a system component / subsystem).
func ValidateSubscription ¶
func ValidateSubscription(s SubscriptionInterface) []error
Types ¶
type AuthenticateFunc ¶
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 // Conn is the direct websocket connection with the client. Conn() *websocket.Conn // User returns the user associated with the connection (or nil). User() interface{} // 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 { Authenticate AuthenticateFunc EventHandlers ConnectionEventHandlers ControlMessageHandlers ConnectionControlMessageHandlers ReadLimit int64 }
ConnectionConfig defines the configuration parameters of a GraphQL WebSocket connection.
type ConnectionControlMessageHandlers ¶
type ConnectionControlMessageHandlers struct { // CloseHandler is the handler for close messages received from the peer. // The code argument to h is the received close code or CloseNoStatusReceived // if the close message is empty. The default close handler sends a close message back to the peer. // // The handler function is called from the NextReader, ReadMessage and message // reader Read methods. The application must read the connection to process close // messages as described in the section on Control Messages above. // // The connection read methods return a CloseError when a close message is received. // Most applications should handle close messages as part of their normal error handling. // Applications should only set a close handler when the application must perform some // action before sending a close message back to the peer. CloseHandler func(Connection, int, string) error // PingHandler is the handler for ping messages received from the peer. The appData // argument to h is the PING message application data. The default ping handler sends // a pong to the peer. // // The handler function is called from the NextReader, ReadMessage and message reader // Read methods. The application must read the connection to process ping messages as // described in the section on Control Messages above. PingHandler func(Connection, string) error // PongHandler is the handler for pong messages received from the peer. The appData // argument to h is the PONG message application data. The default pong handler // does nothing. // // The handler function is called from the NextReader, ReadMessage and message reader // Read methods. The application must read the connection to process pong messages as // described in the section on Control Messages above. PongHandler func(Connection, string) error }
ConnectionControlMessageHandlers define the control message handlers for a connection. The WebSocket protocol defines three types of control messages: close, ping and pong.
type ConnectionEventHandlers ¶
type ConnectionEventHandlers struct { // Init is called whenever the connection is initilized. Init func(Connection) // 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 ConnectionFactory ¶
type ConnectionFactory interface {
Create(*websocket.Conn, ConnectionEventHandlers) Connection
}
ConnectionFactory is an interface that creates concrete connections. It is used by the handler and enables it to create different "instances" of connections, depending on the need of the developer.
An use case for this is to add new PubSub system (e.g. Redis PubSub, ...).
func NewConnectionFactory ¶
func NewConnectionFactory(connConfig ConnectionConfig, sManager SubscriptionManager, logger log.Logger) ConnectionFactory
type ConnectionSubscriptions ¶
type ConnectionSubscriptions map[string]SubscriptionInterface
ConnectionSubscriptions defines a map of all subscriptions of a connection by their IDs.
type DataMessagePayload ¶
type DataMessagePayload struct { Data interface{} `json:"data"` Errors []error `json:"errors"` }
DataMessagePayload defines the result data of an operation.
type HandlerConfig ¶
type HandlerConfig struct { Schema *graphql.Schema SubscriptionManager SubscriptionManager ConnectionFactory ConnectionFactory Authenticate AuthenticateFunc ConnectionEvents ConnectionEventHandlers }
HandlerConfig stores the configuration of a GraphQL WebSocket handler.
type InitMessagePayload ¶
type InitMessagePayload struct {
AuthToken string `json:"authToken"`
}
InitMessagePayload defines the parameters of a connection init message.
type MultipleTopic ¶
type MultipleTopic []interface{}
func (MultipleTopic) ID ¶
func (topic MultipleTopic) ID() interface{}
type OperationMessage ¶
type OperationMessage struct { ID string `json:"id"` Type string `json:"type"` Payload interface{} `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]interface{} `json:"variables"` OperationName string `json:"operationName"` }
StartMessagePayload defines the parameters of an operation that a client requests to be started.
type StringTopic ¶
type StringTopic string
StringTopic is a simple implementaiton of `Topic` for those PubSub systems that use simple strings as topics.
func (StringTopic) ID ¶
func (topic StringTopic) ID() interface{}
type Subscriber ¶
type Subscriber interface { // Subscription the subscription this subscriber is representing. Subscription() SubscriptionInterface // Topics returns the array of topics subscribed. // It is designed for accumulating subscriptions before applying it to a // connection. Topics() []Topic // Subscribe does a subcription, or accumulate it (depends on the // implementation). Subscribe(topic Topic) error }
SubscriptionSubscriber does subscriptions in behalf a single Subscription
func NewInMemorySubscriber ¶
func NewInMemorySubscriber(subscription SubscriptionInterface) Subscriber
type Subscription ¶
type Subscription struct { ID string Query string Variables map[string]interface{} OperationName string Document *ast.Document Fields []string Connection Connection SendData SubscriptionSendDataFunc }
Subscription holds all information about a GraphQL subscription made by a client, including a function to send data back to the client when there are updates to the subscription query result.
func (*Subscription) GetConnection ¶
func (s *Subscription) GetConnection() Connection
func (*Subscription) GetDocument ¶
func (s *Subscription) GetDocument() *ast.Document
func (*Subscription) GetFields ¶
func (s *Subscription) GetFields() []string
func (*Subscription) GetID ¶
func (s *Subscription) GetID() string
func (*Subscription) GetOperationName ¶
func (s *Subscription) GetOperationName() string
func (*Subscription) GetQuery ¶
func (s *Subscription) GetQuery() string
func (*Subscription) GetSendData ¶
func (s *Subscription) GetSendData() SubscriptionSendDataFunc
func (*Subscription) GetVariables ¶
func (s *Subscription) GetVariables() map[string]interface{}
func (*Subscription) MatchesField ¶
func (s *Subscription) MatchesField(field string) bool
MatchesField returns true if the subscription is for data that belongs to the given field.
func (*Subscription) SetDocument ¶
func (s *Subscription) SetDocument(value *ast.Document)
func (*Subscription) SetFields ¶
func (s *Subscription) SetFields(value []string)
type SubscriptionField ¶
type SubscriptionField struct { graphql.Field Subscribe func(subscriber Subscriber) error }
type SubscriptionInterface ¶
type SubscriptionInterface interface { GetID() string GetQuery() string GetVariables() map[string]interface{} GetOperationName() string GetDocument() *ast.Document GetFields() []string GetConnection() Connection GetSendData() SubscriptionSendDataFunc SetFields(document []string) SetDocument(document *ast.Document) }
type SubscriptionManager ¶
type SubscriptionManager interface { // AddSubscription adds a new subscription to the manager. AddSubscription(Connection, SubscriptionInterface) []error // RemoveSubscription removes a subscription from the manager. RemoveSubscription(Connection, string) // RemoveSubscriptions removes all subscriptions of a client connection. RemoveSubscriptions(Connection) CreateSubscriptionSubscriber(subscription SubscriptionInterface) Subscriber Publish(topic Topic, data interface{}) error Subscribe(Subscriber) error }
SubscriptionManager provides a high-level interface to managing and accessing the subscriptions made by GraphQL WS clients.
func NewInMemorySubscriptionManager ¶
func NewInMemorySubscriptionManager(schema *graphql.Schema) SubscriptionManager
NewSubscriptionManager creates a new subscription manager.
func NewSubscriptionManagerWithLogger ¶
func NewSubscriptionManagerWithLogger(schema *graphql.Schema, logger *log.Entry) SubscriptionManager
type SubscriptionSendDataFunc ¶
type SubscriptionSendDataFunc func(*DataMessagePayload)
SubscriptionSendDataFunc is a function that sends updated data for a specific subscription to the corresponding subscriber.
type Subscriptions ¶
type Subscriptions map[Connection]ConnectionSubscriptions
Subscriptions defines a map of connections to a map of subscription IDs to subscriptions.
type Topic ¶
type Topic interface { // ID will return the structure ID of the topic on the technology used for // that purpose. For example, using a Redis PubSub system, this method would // return a string containing identifier of the channel. ID() interface{} }
Topic represents a custom interface that represents a topic that will be used along with a PubSub system.