9
9
log "github.com/sirupsen/logrus"
10
10
)
11
11
12
- // Convert from validation errors to regular errors
13
- func translateValidationErrors (errors []gqlerrors.FormattedError ) []error {
12
+ // ErrorsFromGraphQLErrors convert from GraphQL errors to regular errors.
13
+ func ErrorsFromGraphQLErrors (errors []gqlerrors.FormattedError ) []error {
14
14
out := make ([]error , len (errors ))
15
15
for i := range errors {
16
16
out [i ] = errors [i ]
@@ -33,12 +33,20 @@ type Subscription struct {
33
33
SendData SubscriptionSendDataFunc
34
34
}
35
35
36
+ // ConnectionSubscriptions defines a map of all subscriptions of
37
+ // a connection by their IDs.
38
+ type ConnectionSubscriptions map [string ]* Subscription
39
+
40
+ // Subscriptions defines a map of connections to a map of
41
+ // subscription IDs to subscriptions.
42
+ type Subscriptions map [Connection ]ConnectionSubscriptions
43
+
36
44
// SubscriptionManager provides a high-level interface to managing
37
45
// and accessing the subscriptions made by GraphQL WS clients.
38
46
type SubscriptionManager interface {
39
47
// Subscriptions returns all registered subscriptions, grouped
40
48
// by connection.
41
- Subscriptions () map [ Connection ] map [ string ] * Subscription
49
+ Subscriptions () Subscriptions
42
50
43
51
// AddSubscription adds a new subscription to the manager.
44
52
AddSubscription (Connection , * Subscription ) []error
@@ -55,21 +63,21 @@ type SubscriptionManager interface {
55
63
*/
56
64
57
65
type subscriptionManager struct {
58
- subscriptions map [ Connection ] map [ string ] * Subscription
66
+ subscriptions Subscriptions
59
67
schema * graphql.Schema
60
68
logger * log.Entry
61
69
}
62
70
63
71
// NewSubscriptionManager creates a new subscription manager.
64
72
func NewSubscriptionManager (schema * graphql.Schema ) SubscriptionManager {
65
73
manager := new (subscriptionManager )
66
- manager .subscriptions = make (map [ Connection ] map [ string ] * Subscription )
74
+ manager .subscriptions = make (Subscriptions )
67
75
manager .logger = NewLogger ("subscriptions" )
68
76
manager .schema = schema
69
77
return manager
70
78
}
71
79
72
- func (m * subscriptionManager ) Subscriptions () map [ Connection ] map [ string ] * Subscription {
80
+ func (m * subscriptionManager ) Subscriptions () Subscriptions {
73
81
return m .subscriptions
74
82
}
75
83
@@ -97,13 +105,13 @@ func (m *subscriptionManager) AddSubscription(
97
105
m .logger .WithFields (log.Fields {
98
106
"errors" : validation .Errors ,
99
107
}).Warn ("Failed to validate subscription query" )
100
- return translateValidationErrors (validation .Errors )
108
+ return ErrorsFromGraphQLErrors (validation .Errors )
101
109
}
102
110
103
111
// Allocate the connection's map of subscription IDs to
104
112
// subscriptions on demand
105
113
if m .subscriptions [conn ] == nil {
106
- m .subscriptions [conn ] = make (map [ string ] * Subscription )
114
+ m .subscriptions [conn ] = make (ConnectionSubscriptions )
107
115
}
108
116
109
117
// Add the subscription if it hasn't already been added
0 commit comments