80daed081d
Creates a new `GenerateCosmoRouterConfig` function to build and serialize a Cosmo Router configuration from subgraphs. Implements PubSub mechanism for managing schema updates, allowing subscription to updates. Adds Subscription resolver and updates existing structures to accommodate new functionalities. This enhances the system's capabilities for dynamic updates and configuration management.
55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package graph
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"gitlab.com/unboundsoftware/schemas/graph/model"
|
|
)
|
|
|
|
// GenerateCosmoRouterConfig generates a Cosmo Router execution config from subgraphs
|
|
func GenerateCosmoRouterConfig(subGraphs []*model.SubGraph) (string, error) {
|
|
// Build the Cosmo router config structure
|
|
// This is a simplified version - you may need to adjust based on actual Cosmo requirements
|
|
config := map[string]interface{}{
|
|
"version": "1",
|
|
"subgraphs": convertSubGraphsToCosmo(subGraphs),
|
|
// Add other Cosmo-specific configuration as needed
|
|
}
|
|
|
|
// Marshal to JSON
|
|
configJSON, err := json.MarshalIndent(config, "", " ")
|
|
if err != nil {
|
|
return "", fmt.Errorf("marshal cosmo config: %w", err)
|
|
}
|
|
|
|
return string(configJSON), nil
|
|
}
|
|
|
|
func convertSubGraphsToCosmo(subGraphs []*model.SubGraph) []map[string]interface{} {
|
|
cosmoSubgraphs := make([]map[string]interface{}, 0, len(subGraphs))
|
|
|
|
for _, sg := range subGraphs {
|
|
cosmoSg := map[string]interface{}{
|
|
"name": sg.Service,
|
|
"sdl": sg.Sdl,
|
|
}
|
|
|
|
if sg.URL != nil {
|
|
cosmoSg["routing_url"] = *sg.URL
|
|
}
|
|
|
|
if sg.WsURL != nil {
|
|
cosmoSg["subscription"] = map[string]interface{}{
|
|
"url": *sg.WsURL,
|
|
"protocol": "ws",
|
|
"websocket_subprotocol": "graphql-ws",
|
|
}
|
|
}
|
|
|
|
cosmoSubgraphs = append(cosmoSubgraphs, cosmoSg)
|
|
}
|
|
|
|
return cosmoSubgraphs
|
|
}
|