feat: initial commit

This commit is contained in:
2022-10-09 15:23:52 +02:00
commit a1b4d4fc27
39 changed files with 5810 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
query SubGraphs($ref: String!) {
subGraphs(ref: $ref) {
service
url
wsUrl
changedBy
changedAt
}
}
+9
View File
@@ -0,0 +1,9 @@
mutation UpdateSubGraph($input: InputSubGraph!) {
updateSubGraph(input: $input) {
service
url
wsUrl
changedBy
changedAt
}
}
+61
View File
@@ -0,0 +1,61 @@
package ctl
import (
"context"
"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
}
subGraphs := make([]*SubGraph, len(response.SubGraphs))
for i, subGraph := range response.SubGraphs {
subGraphs[i] = &SubGraph{
Service: subGraph.Service,
URL: subGraph.Url,
WSUrl: subGraph.WsUrl,
ChangedBy: subGraph.ChangedBy,
ChangedAt: subGraph.ChangedAt,
}
}
return subGraphs, nil
}
+187
View File
@@ -0,0 +1,187 @@
// Code generated by github.com/Khan/genqlient, DO NOT EDIT.
package ctl
import (
"context"
"time"
"github.com/Khan/genqlient/graphql"
)
type InputSubGraph struct {
Ref string `json:"ref"`
Service string `json:"service"`
Url *string `json:"url"`
WsUrl *string `json:"wsUrl"`
Sdl string `json:"sdl"`
}
// GetRef returns InputSubGraph.Ref, and is useful for accessing the field via an interface.
func (v *InputSubGraph) GetRef() string { return v.Ref }
// GetService returns InputSubGraph.Service, and is useful for accessing the field via an interface.
func (v *InputSubGraph) GetService() string { return v.Service }
// GetUrl returns InputSubGraph.Url, and is useful for accessing the field via an interface.
func (v *InputSubGraph) GetUrl() *string { return v.Url }
// GetWsUrl returns InputSubGraph.WsUrl, and is useful for accessing the field via an interface.
func (v *InputSubGraph) GetWsUrl() *string { return v.WsUrl }
// GetSdl returns InputSubGraph.Sdl, and is useful for accessing the field via an interface.
func (v *InputSubGraph) GetSdl() string { return v.Sdl }
// SubGraphsResponse is returned by SubGraphs on success.
type SubGraphsResponse struct {
SubGraphs []*SubGraphsSubGraphsSubGraph `json:"subGraphs"`
}
// GetSubGraphs returns SubGraphsResponse.SubGraphs, and is useful for accessing the field via an interface.
func (v *SubGraphsResponse) GetSubGraphs() []*SubGraphsSubGraphsSubGraph { return v.SubGraphs }
// SubGraphsSubGraphsSubGraph includes the requested fields of the GraphQL type SubGraph.
type SubGraphsSubGraphsSubGraph struct {
Service string `json:"service"`
Url *string `json:"url"`
WsUrl *string `json:"wsUrl"`
ChangedBy string `json:"changedBy"`
ChangedAt time.Time `json:"changedAt"`
}
// GetService returns SubGraphsSubGraphsSubGraph.Service, and is useful for accessing the field via an interface.
func (v *SubGraphsSubGraphsSubGraph) GetService() string { return v.Service }
// GetUrl returns SubGraphsSubGraphsSubGraph.Url, and is useful for accessing the field via an interface.
func (v *SubGraphsSubGraphsSubGraph) GetUrl() *string { return v.Url }
// GetWsUrl returns SubGraphsSubGraphsSubGraph.WsUrl, and is useful for accessing the field via an interface.
func (v *SubGraphsSubGraphsSubGraph) GetWsUrl() *string { return v.WsUrl }
// GetChangedBy returns SubGraphsSubGraphsSubGraph.ChangedBy, and is useful for accessing the field via an interface.
func (v *SubGraphsSubGraphsSubGraph) GetChangedBy() string { return v.ChangedBy }
// GetChangedAt returns SubGraphsSubGraphsSubGraph.ChangedAt, and is useful for accessing the field via an interface.
func (v *SubGraphsSubGraphsSubGraph) GetChangedAt() time.Time { return v.ChangedAt }
// UpdateSubGraphResponse is returned by UpdateSubGraph on success.
type UpdateSubGraphResponse struct {
UpdateSubGraph *UpdateSubGraphUpdateSubGraph `json:"updateSubGraph"`
}
// GetUpdateSubGraph returns UpdateSubGraphResponse.UpdateSubGraph, and is useful for accessing the field via an interface.
func (v *UpdateSubGraphResponse) GetUpdateSubGraph() *UpdateSubGraphUpdateSubGraph {
return v.UpdateSubGraph
}
// UpdateSubGraphUpdateSubGraph includes the requested fields of the GraphQL type SubGraph.
type UpdateSubGraphUpdateSubGraph struct {
Service string `json:"service"`
Url *string `json:"url"`
WsUrl *string `json:"wsUrl"`
ChangedBy string `json:"changedBy"`
ChangedAt time.Time `json:"changedAt"`
}
// GetService returns UpdateSubGraphUpdateSubGraph.Service, and is useful for accessing the field via an interface.
func (v *UpdateSubGraphUpdateSubGraph) GetService() string { return v.Service }
// GetUrl returns UpdateSubGraphUpdateSubGraph.Url, and is useful for accessing the field via an interface.
func (v *UpdateSubGraphUpdateSubGraph) GetUrl() *string { return v.Url }
// GetWsUrl returns UpdateSubGraphUpdateSubGraph.WsUrl, and is useful for accessing the field via an interface.
func (v *UpdateSubGraphUpdateSubGraph) GetWsUrl() *string { return v.WsUrl }
// GetChangedBy returns UpdateSubGraphUpdateSubGraph.ChangedBy, and is useful for accessing the field via an interface.
func (v *UpdateSubGraphUpdateSubGraph) GetChangedBy() string { return v.ChangedBy }
// GetChangedAt returns UpdateSubGraphUpdateSubGraph.ChangedAt, and is useful for accessing the field via an interface.
func (v *UpdateSubGraphUpdateSubGraph) GetChangedAt() time.Time { return v.ChangedAt }
// __SubGraphsInput is used internally by genqlient
type __SubGraphsInput struct {
Ref string `json:"ref"`
}
// GetRef returns __SubGraphsInput.Ref, and is useful for accessing the field via an interface.
func (v *__SubGraphsInput) GetRef() string { return v.Ref }
// __UpdateSubGraphInput is used internally by genqlient
type __UpdateSubGraphInput struct {
Input *InputSubGraph `json:"input,omitempty"`
}
// GetInput returns __UpdateSubGraphInput.Input, and is useful for accessing the field via an interface.
func (v *__UpdateSubGraphInput) GetInput() *InputSubGraph { return v.Input }
func SubGraphs(
ctx context.Context,
client graphql.Client,
ref string,
) (*SubGraphsResponse, error) {
req := &graphql.Request{
OpName: "SubGraphs",
Query: `
query SubGraphs ($ref: String!) {
subGraphs(ref: $ref) {
service
url
wsUrl
changedBy
changedAt
}
}
`,
Variables: &__SubGraphsInput{
Ref: ref,
},
}
var err error
var data SubGraphsResponse
resp := &graphql.Response{Data: &data}
err = client.MakeRequest(
ctx,
req,
resp,
)
return &data, err
}
func UpdateSubGraph(
ctx context.Context,
client graphql.Client,
input *InputSubGraph,
) (*UpdateSubGraphResponse, error) {
req := &graphql.Request{
OpName: "UpdateSubGraph",
Query: `
mutation UpdateSubGraph ($input: InputSubGraph!) {
updateSubGraph(input: $input) {
service
url
wsUrl
changedBy
changedAt
}
}
`,
Variables: &__UpdateSubGraphInput{
Input: input,
},
}
var err error
var data UpdateSubGraphResponse
resp := &graphql.Response{Data: &data}
err = client.MakeRequest(
ctx,
req,
resp,
)
return &data, err
}
+13
View File
@@ -0,0 +1,13 @@
# Default genqlient config; for full documentation see:
# https://github.com/Khan/genqlient/blob/main/docs/genqlient.yaml
schema: ../graph/schema.graphqls
operations:
- "*.graphql"
generated: generated.go
package: ctl
use_struct_references: true
optional: pointer
#- output: pointer
bindings:
Time:
type: time.Time
+22
View File
@@ -0,0 +1,22 @@
package ctl
import (
"net/http"
)
type headerTransport struct {
tripper http.RoundTripper
apiKey string
}
func NewTransport(tripper http.RoundTripper, apiKey string) *headerTransport {
return &headerTransport{
tripper: tripper,
apiKey: apiKey,
}
}
func (t *headerTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("X-Api-Key", t.apiKey)
return t.tripper.RoundTrip(req)
}
+48
View File
@@ -0,0 +1,48 @@
package ctl
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_headerTransport_RoundTrip(t1 *testing.T) {
type fields struct {
apiKey string
}
tests := []struct {
name string
fields fields
handler func(t *testing.T) http.Handler
want *http.Response
wantErr bool
}{
{
name: "apiKey is set as header",
fields: fields{
apiKey: "abc123",
},
handler: func(t *testing.T) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
assert.Equal(t, "abc123", req.Header.Get("x-api-key"))
_, err := w.Write([]byte("OK"))
assert.NoError(t, err)
})
},
want: nil,
wantErr: false,
},
}
for _, tt := range tests {
t1.Run(tt.name, func(t1 *testing.T) {
srv := httptest.NewServer(tt.handler(t1))
defer srv.Close()
t := NewTransport(http.DefaultTransport, tt.fields.apiKey)
c := http.Client{Transport: t}
_, err := c.Get(srv.URL)
assert.NoError(t1, err)
})
}
}
+11
View File
@@ -0,0 +1,11 @@
package ctl
import "time"
type SubGraph struct {
Service string
URL *string
WSUrl *string
ChangedBy string
ChangedAt time.Time
}