67 lines
1.8 KiB
Go
67 lines
1.8 KiB
Go
package ctl
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/Khan/genqlient/graphql"
|
|
)
|
|
|
|
//go:generate go run github.com/Khan/genqlient@main ./genqlient.yaml
|
|
|
|
func Publish(apiKey, schemaRef, service, sdl string, url, wsUrl *url.URL, schemasUrl url.URL) (*SubGraph, error) {
|
|
client := graphql.NewClient(schemasUrl.String(), &http.Client{Transport: NewTransport(http.DefaultTransport, apiKey)})
|
|
var urlString *string
|
|
if url != nil {
|
|
temp := url.String()
|
|
urlString = &temp
|
|
}
|
|
var wsUrlString *string
|
|
if wsUrl != nil {
|
|
temp := wsUrl.String()
|
|
wsUrlString = &temp
|
|
}
|
|
response, err := UpdateSubGraph(context.Background(), client, &InputSubGraph{
|
|
Ref: schemaRef,
|
|
Service: service,
|
|
Url: urlString,
|
|
WsUrl: wsUrlString,
|
|
Sdl: sdl,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &SubGraph{
|
|
Service: response.UpdateSubGraph.Service,
|
|
URL: response.UpdateSubGraph.Url,
|
|
WSUrl: response.UpdateSubGraph.WsUrl,
|
|
ChangedBy: response.UpdateSubGraph.ChangedBy,
|
|
ChangedAt: response.UpdateSubGraph.ChangedAt,
|
|
}, nil
|
|
}
|
|
|
|
func List(apiKey, schemaRef, service string, schemasUrl url.URL) ([]*SubGraph, error) {
|
|
client := graphql.NewClient(schemasUrl.String(), &http.Client{Transport: NewTransport(http.DefaultTransport, apiKey)})
|
|
response, err := SubGraphs(context.Background(), client, schemaRef)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var subGraphs []*SubGraph
|
|
if s, ok := response.Supergraph.(*SubGraphsSupergraphSubGraphs); ok {
|
|
subGraphs = make([]*SubGraph, len(s.SubGraphs))
|
|
for i, subGraph := range s.SubGraphs {
|
|
subGraphs[i] = &SubGraph{
|
|
Service: subGraph.Service,
|
|
URL: subGraph.Url,
|
|
WSUrl: subGraph.WsUrl,
|
|
ChangedBy: subGraph.ChangedBy,
|
|
ChangedAt: subGraph.ChangedAt,
|
|
}
|
|
}
|
|
return subGraphs, nil
|
|
}
|
|
return nil, fmt.Errorf("unexpected response type")
|
|
}
|