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
File diff suppressed because it is too large Load Diff
+25
View File
@@ -0,0 +1,25 @@
// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.
package model
import (
"time"
)
type InputSubGraph struct {
Ref string `json:"ref"`
Service string `json:"service"`
URL *string `json:"url"`
WsURL *string `json:"wsUrl"`
Sdl string `json:"sdl"`
}
type SubGraph struct {
ID string `json:"id"`
Service string `json:"service"`
URL *string `json:"url"`
WsURL *string `json:"wsUrl"`
Sdl string `json:"sdl"`
ChangedBy string `json:"changedBy"`
ChangedAt time.Time `json:"changedAt"`
}
+30
View File
@@ -0,0 +1,30 @@
package graph
import (
"github.com/apex/log"
"gitlab.com/unboundsoftware/eventsourced/eventsourced"
"gitlab.com/unboundsoftware/schemas/cache"
)
//go:generate go run github.com/99designs/gqlgen
// This file will not be regenerated automatically.
//
// It serves as dependency injection for your app, add any dependencies you require here.
type Publisher interface {
Publish(event eventsourced.Event) error
Stop() error
}
type Resolver struct {
EventStore eventsourced.EventStore
Publisher Publisher
Logger log.Interface
Cache *cache.Cache
}
func (r *Resolver) handler(aggregate eventsourced.Aggregate) (eventsourced.CommandHandler, error) {
return eventsourced.NewHandler(aggregate, r.EventStore, eventsourced.WithEventPublisher(r.Publisher))
}
+29
View File
@@ -0,0 +1,29 @@
type Query {
subGraphs(ref: String!): [SubGraph!]! @hasApiKey
}
type Mutation {
updateSubGraph(input: InputSubGraph!): SubGraph! @hasApiKey
}
type SubGraph {
id: ID!
service: String!
url: String
wsUrl: String
sdl: String!
changedBy: String!
changedAt: Time!
}
input InputSubGraph {
ref: String!
service: String!
url: String
wsUrl: String
sdl: String!
}
scalar Time
directive @hasApiKey on FIELD_DEFINITION
+16
View File
@@ -0,0 +1,16 @@
package graph
import (
"gitlab.com/unboundsoftware/eventsourced/eventsourced"
"gitlab.com/unboundsoftware/schemas/domain"
)
func (r *Resolver) fetchSubGraph(subGraphId string) (*domain.SubGraph, error) {
subGraph := &domain.SubGraph{BaseAggregate: eventsourced.BaseAggregateFromString(subGraphId)}
_, err := r.handler(subGraph)
if err != nil {
return nil, err
}
return subGraph, nil
}
+95
View File
@@ -0,0 +1,95 @@
package graph
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
import (
"context"
"github.com/wundergraph/graphql-go-tools/pkg/federation/sdlmerge"
"gitlab.com/unboundsoftware/eventsourced/eventsourced"
"gitlab.com/unboundsoftware/schemas/domain"
"gitlab.com/unboundsoftware/schemas/graph/generated"
"gitlab.com/unboundsoftware/schemas/graph/model"
)
// UpdateSubGraph is the resolver for the updateSubGraph field.
func (r *mutationResolver) UpdateSubGraph(ctx context.Context, input model.InputSubGraph) (*model.SubGraph, error) {
subGraphId := r.Cache.SubGraphId(input.Ref, input.Service)
subGraph := &domain.SubGraph{}
if subGraphId != "" {
subGraph.BaseAggregate = eventsourced.BaseAggregateFromString(subGraphId)
}
handler, err := r.handler(subGraph)
if err != nil {
return nil, err
}
serviceSDLs := []string{input.Sdl}
for _, id := range r.Cache.Services(input.Ref) {
sg, err := r.fetchSubGraph(id)
if err != nil {
return nil, err
}
if sg.Service != input.Service {
serviceSDLs = append(serviceSDLs, sg.Sdl)
}
}
_, err = sdlmerge.MergeSDLs(serviceSDLs...)
if err != nil {
return nil, err
}
_, err = handler.Handle(domain.UpdateSubGraph{
Ref: input.Ref,
Service: input.Service,
Url: input.URL,
WSUrl: input.WsURL,
Sdl: input.Sdl,
Initiator: "Fetch name from API-key?",
})
if err != nil {
return nil, err
}
return &model.SubGraph{
ID: subGraph.ID.String(),
Service: subGraph.Service,
URL: subGraph.Url,
WsURL: subGraph.WSUrl,
Sdl: subGraph.Sdl,
ChangedBy: subGraph.ChangedBy,
ChangedAt: subGraph.ChangedAt,
}, nil
}
// SubGraphs is the resolver for the subGraphs field.
func (r *queryResolver) SubGraphs(ctx context.Context, ref string) ([]*model.SubGraph, error) {
services := r.Cache.Services(ref)
subGraphs := make([]*model.SubGraph, len(services))
for i, id := range services {
sg, err := r.fetchSubGraph(id)
if err != nil {
return nil, err
}
subGraphs[i] = &model.SubGraph{
ID: sg.ID.String(),
Service: sg.Service,
URL: sg.Url,
WsURL: sg.WSUrl,
Sdl: sg.Sdl,
ChangedBy: sg.ChangedBy,
ChangedAt: sg.ChangedAt,
}
}
return subGraphs, nil
}
// Mutation returns generated.MutationResolver implementation.
func (r *Resolver) Mutation() generated.MutationResolver { return &mutationResolver{r} }
// Query returns generated.QueryResolver implementation.
func (r *Resolver) Query() generated.QueryResolver { return &queryResolver{r} }
type (
mutationResolver struct{ *Resolver }
queryResolver struct{ *Resolver }
)
+9
View File
@@ -0,0 +1,9 @@
//go:build tools
// +build tools
package graph
import (
_ "github.com/99designs/gqlgen"
_ "github.com/99designs/gqlgen/graphql/introspection"
)