Documentation ¶
Index ¶
- func ErrorsFromGraphQLErrors(errors []gqlerrors.FormattedError) []error
- func NewHandler(config HandlerConfig) http.Handler
- func NewLogger(prefix string) *log.Entry
- type AuthenticateFunc
- type Connection
- type ConnectionConfig
- type ConnectionEventHandlers
- type ConnectionSubscriptions
- type DataMessagePayload
- type HandlerConfig
- type InitMessagePayload
- type OperationMessage
- type StartMessagePayload
- type Subscription
- type SubscriptionManager
- type SubscriptionSendDataFunc
- type Subscriptions
Constants ¶
This section is empty.
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.
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 // 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 }
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 ConnectionSubscriptions ¶
type ConnectionSubscriptions map[string]*Subscription
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 { SubscriptionManager SubscriptionManager Authenticate AuthenticateFunc }
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 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 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) MatchesField ¶
func (s *Subscription) MatchesField(field string) bool
MatchesField returns true if the subscription is for data that belongs to the given field.
type SubscriptionManager ¶
type SubscriptionManager interface { // Subscriptions returns all registered subscriptions, grouped // by connection. Subscriptions() Subscriptions // AddSubscription adds a new subscription to the manager. AddSubscription(Connection, *Subscription) []error // RemoveSubscription removes a subscription from the manager. RemoveSubscription(Connection, *Subscription) // RemoveSubscriptions removes all subscriptions of a client connection. RemoveSubscriptions(Connection) }
SubscriptionManager provides a high-level interface to managing and accessing the subscriptions made by GraphQL WS clients.
func NewSubscriptionManager ¶
func NewSubscriptionManager(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.