2023-04-27 09:03:40 +08:00
|
|
|
package writer
|
|
|
|
|
|
|
|
import (
|
2023-05-10 02:37:58 +08:00
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/msgpatch"
|
|
|
|
"strings"
|
2023-04-27 09:03:40 +08:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/common"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"nhooyr.io/websocket/wsjson"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HasuraConnectionWriter
|
|
|
|
// process messages (middleware to hasura)
|
2023-12-15 01:01:47 +08:00
|
|
|
func HasuraConnectionWriter(hc *common.HasuraConnection, fromBrowserToHasuraChannel *common.SafeChannel, wg *sync.WaitGroup) {
|
2023-04-27 09:03:40 +08:00
|
|
|
log := log.WithField("_routine", "HasuraConnectionWriter")
|
|
|
|
|
|
|
|
browserConnection := hc.Browserconn
|
|
|
|
|
|
|
|
log = log.WithField("browserConnectionId", browserConnection.Id).WithField("hasuraConnectionId", hc.Id)
|
|
|
|
|
|
|
|
defer wg.Done()
|
|
|
|
defer hc.ContextCancelFunc()
|
2023-09-07 22:54:27 +08:00
|
|
|
defer log.Debugf("finished")
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
RangeLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-hc.Context.Done():
|
|
|
|
break RangeLoop
|
2023-12-15 01:01:47 +08:00
|
|
|
case fromBrowserMessage := <-fromBrowserToHasuraChannel.ReceiveChannel():
|
2023-04-27 09:03:40 +08:00
|
|
|
{
|
|
|
|
if fromBrowserMessage == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var fromBrowserMessageAsMap = fromBrowserMessage.(map[string]interface{})
|
|
|
|
|
|
|
|
if fromBrowserMessageAsMap["type"] == "start" {
|
|
|
|
var queryId = fromBrowserMessageAsMap["id"].(string)
|
|
|
|
|
2023-05-10 02:37:58 +08:00
|
|
|
//Identify type based on query string
|
|
|
|
messageType := common.Query
|
2024-01-24 02:28:32 +08:00
|
|
|
streamCursorField := ""
|
|
|
|
streamCursorVariableName := ""
|
|
|
|
var streamCursorInitialValue interface{}
|
2023-05-10 02:37:58 +08:00
|
|
|
payload := fromBrowserMessageAsMap["payload"].(map[string]interface{})
|
2024-01-24 02:28:32 +08:00
|
|
|
|
2023-05-10 02:37:58 +08:00
|
|
|
query, ok := payload["query"].(string)
|
|
|
|
if ok {
|
|
|
|
if strings.HasPrefix(query, "subscription") {
|
|
|
|
messageType = common.Subscription
|
|
|
|
|
|
|
|
if strings.Contains(query, "_stream(") && strings.Contains(query, "cursor: {") {
|
|
|
|
messageType = common.Streaming
|
2024-01-24 02:28:32 +08:00
|
|
|
|
|
|
|
browserConnection.ActiveSubscriptionsMutex.RLock()
|
|
|
|
_, queryIdExists := browserConnection.ActiveSubscriptions[queryId]
|
|
|
|
browserConnection.ActiveSubscriptionsMutex.RUnlock()
|
|
|
|
if !queryIdExists {
|
|
|
|
streamCursorField, streamCursorVariableName, streamCursorInitialValue = common.GetStreamCursorPropsFromQuery(payload, query)
|
|
|
|
|
|
|
|
//It's necessary to assure the cursor field will return in the result of the query
|
|
|
|
//To be able to store the last received cursor value
|
|
|
|
payload["query"] = common.PatchQueryIncludingCursorField(query, streamCursorField)
|
|
|
|
fromBrowserMessageAsMap["payload"] = payload
|
|
|
|
}
|
2023-05-10 02:37:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(query, "_aggregate") && strings.Contains(query, "aggregate {") {
|
|
|
|
messageType = common.SubscriptionAggregate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(query, "mutation") {
|
|
|
|
messageType = common.Mutation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 06:31:31 +08:00
|
|
|
//Identify if the client that requested this subscription expects to receive json-patch
|
|
|
|
//Client append `Patched_` to the query operationName to indicate that it supports
|
|
|
|
jsonPatchSupported := false
|
|
|
|
operationName, ok := payload["operationName"].(string)
|
|
|
|
if ok && strings.HasPrefix(operationName, "Patched_") {
|
|
|
|
jsonPatchSupported = true
|
|
|
|
}
|
|
|
|
|
2023-04-27 09:03:40 +08:00
|
|
|
browserConnection.ActiveSubscriptionsMutex.Lock()
|
|
|
|
browserConnection.ActiveSubscriptions[queryId] = common.GraphQlSubscription{
|
|
|
|
Id: queryId,
|
2024-01-24 02:28:32 +08:00
|
|
|
Message: fromBrowserMessageAsMap,
|
|
|
|
OperationName: operationName,
|
|
|
|
StreamCursorField: streamCursorField,
|
|
|
|
StreamCursorVariableName: streamCursorVariableName,
|
|
|
|
StreamCursorCurrValue: streamCursorInitialValue,
|
2023-04-27 09:03:40 +08:00
|
|
|
LastSeenOnHasuraConnetion: hc.Id,
|
2023-05-25 06:31:31 +08:00
|
|
|
JsonPatchSupported: jsonPatchSupported,
|
2023-05-10 02:37:58 +08:00
|
|
|
Type: messageType,
|
2023-04-27 09:03:40 +08:00
|
|
|
}
|
|
|
|
// log.Tracef("Current queries: %v", browserConnection.ActiveSubscriptions)
|
|
|
|
browserConnection.ActiveSubscriptionsMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
if fromBrowserMessageAsMap["type"] == "stop" {
|
|
|
|
var queryId = fromBrowserMessageAsMap["id"].(string)
|
2023-07-06 20:34:48 +08:00
|
|
|
browserConnection.ActiveSubscriptionsMutex.RLock()
|
2023-05-26 04:08:04 +08:00
|
|
|
jsonPatchSupported := browserConnection.ActiveSubscriptions[queryId].JsonPatchSupported
|
2023-07-06 20:34:48 +08:00
|
|
|
browserConnection.ActiveSubscriptionsMutex.RUnlock()
|
2023-05-26 04:08:04 +08:00
|
|
|
if jsonPatchSupported {
|
2023-05-10 02:37:58 +08:00
|
|
|
msgpatch.RemoveConnSubscriptionCacheFile(browserConnection, queryId)
|
|
|
|
}
|
2023-04-27 09:03:40 +08:00
|
|
|
browserConnection.ActiveSubscriptionsMutex.Lock()
|
|
|
|
delete(browserConnection.ActiveSubscriptions, queryId)
|
|
|
|
// log.Tracef("Current queries: %v", browserConnection.ActiveSubscriptions)
|
|
|
|
browserConnection.ActiveSubscriptionsMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
if fromBrowserMessageAsMap["type"] == "connection_init" {
|
2024-01-24 02:28:32 +08:00
|
|
|
browserConnection.ConnectionInitMessage = fromBrowserMessageAsMap
|
2023-04-27 09:03:40 +08:00
|
|
|
}
|
|
|
|
|
2024-01-24 02:28:32 +08:00
|
|
|
log.Tracef("sending to hasura: %v", fromBrowserMessageAsMap)
|
|
|
|
err := wsjson.Write(hc.Context, hc.Websocket, fromBrowserMessageAsMap)
|
2023-04-27 09:03:40 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error on write (we're disconnected from hasura): %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|