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
|
||
|
|
}
|