feat: organizations and API keys
This commit is contained in:
+76
-19
@@ -15,11 +15,71 @@ import (
|
||||
"gitlab.com/unboundsoftware/schemas/domain"
|
||||
"gitlab.com/unboundsoftware/schemas/graph/generated"
|
||||
"gitlab.com/unboundsoftware/schemas/graph/model"
|
||||
"gitlab.com/unboundsoftware/schemas/middleware"
|
||||
"gitlab.com/unboundsoftware/schemas/rand"
|
||||
)
|
||||
|
||||
// AddOrganization is the resolver for the addOrganization field.
|
||||
func (r *mutationResolver) AddOrganization(ctx context.Context, name string) (*model.Organization, error) {
|
||||
sub := middleware.UserFromContext(ctx)
|
||||
org := &domain.Organization{}
|
||||
h, err := r.handler(ctx, org)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = h.Handle(ctx, &domain.AddOrganization{
|
||||
Name: name,
|
||||
Initiator: sub,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ToGqlOrganization(*org), nil
|
||||
}
|
||||
|
||||
// AddAPIKey is the resolver for the addAPIKey field.
|
||||
func (r *mutationResolver) AddAPIKey(ctx context.Context, input *model.InputAPIKey) (*model.APIKey, error) {
|
||||
sub := middleware.UserFromContext(ctx)
|
||||
org := &domain.Organization{BaseAggregate: eventsourced.BaseAggregateFromString(input.OrganizationID)}
|
||||
h, err := r.handler(ctx, org)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key := fmt.Sprintf("us_ak_%s", rand.String(16))
|
||||
_, err = h.Handle(ctx, &domain.AddAPIKey{
|
||||
Name: input.Name,
|
||||
Key: key,
|
||||
Refs: input.Refs,
|
||||
Read: input.Read,
|
||||
Publish: input.Publish,
|
||||
Initiator: sub,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &model.APIKey{
|
||||
ID: apiKeyId(input.OrganizationID, input.Name),
|
||||
Name: input.Name,
|
||||
Key: &key,
|
||||
Organization: &model.Organization{
|
||||
ID: input.OrganizationID,
|
||||
Name: org.Name,
|
||||
},
|
||||
Refs: input.Refs,
|
||||
Read: input.Read,
|
||||
Publish: input.Publish,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 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)
|
||||
orgId := middleware.OrganizationFromContext(ctx)
|
||||
key, err := middleware.ApiKeyFromContext(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
apiKey := r.Cache.ApiKeyByKey(key)
|
||||
subGraphId := r.Cache.SubGraphId(orgId, input.Ref, input.Service)
|
||||
subGraph := &domain.SubGraph{}
|
||||
if subGraphId != "" {
|
||||
subGraph.BaseAggregate = eventsourced.BaseAggregateFromString(subGraphId)
|
||||
@@ -34,7 +94,7 @@ func (r *mutationResolver) UpdateSubGraph(ctx context.Context, input model.Input
|
||||
return r.toGqlSubGraph(subGraph), nil
|
||||
}
|
||||
serviceSDLs := []string{input.Sdl}
|
||||
services, _ := r.Cache.Services(input.Ref, "")
|
||||
services, _ := r.Cache.Services(orgId, input.Ref, "")
|
||||
for _, id := range services {
|
||||
sg, err := r.fetchSubGraph(ctx, id)
|
||||
if err != nil {
|
||||
@@ -49,12 +109,13 @@ func (r *mutationResolver) UpdateSubGraph(ctx context.Context, input model.Input
|
||||
return nil, err
|
||||
}
|
||||
_, err = handler.Handle(ctx, domain.UpdateSubGraph{
|
||||
Ref: input.Ref,
|
||||
Service: input.Service,
|
||||
Url: input.URL,
|
||||
WSUrl: input.WsURL,
|
||||
Sdl: input.Sdl,
|
||||
Initiator: "Fetch name from API-key?",
|
||||
OrganizationId: orgId,
|
||||
Ref: input.Ref,
|
||||
Service: input.Service,
|
||||
Url: input.URL,
|
||||
WSUrl: input.WsURL,
|
||||
Sdl: input.Sdl,
|
||||
Initiator: apiKey.Name,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -62,25 +123,21 @@ func (r *mutationResolver) UpdateSubGraph(ctx context.Context, input model.Input
|
||||
return r.toGqlSubGraph(subGraph), nil
|
||||
}
|
||||
|
||||
// SubGraphs is the resolver for the subGraphs field.
|
||||
func (r *queryResolver) SubGraphs(ctx context.Context, ref string) ([]*model.SubGraph, error) {
|
||||
res, err := r.Supergraph(ctx, ref, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s, ok := res.(*model.SubGraphs); ok {
|
||||
return s.SubGraphs, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unexpected response")
|
||||
// Organizations is the resolver for the organizations field.
|
||||
func (r *queryResolver) Organizations(ctx context.Context) ([]*model.Organization, error) {
|
||||
sub := middleware.UserFromContext(ctx)
|
||||
orgs := r.Cache.OrganizationsByUser(sub)
|
||||
return ToGqlOrganizations(orgs), nil
|
||||
}
|
||||
|
||||
// Supergraph is the resolver for the supergraph field.
|
||||
func (r *queryResolver) Supergraph(ctx context.Context, ref string, isAfter *string) (model.Supergraph, error) {
|
||||
orgId := middleware.OrganizationFromContext(ctx)
|
||||
after := ""
|
||||
if isAfter != nil {
|
||||
after = *isAfter
|
||||
}
|
||||
services, lastUpdate := r.Cache.Services(ref, after)
|
||||
services, lastUpdate := r.Cache.Services(orgId, ref, after)
|
||||
if after == lastUpdate {
|
||||
return &model.Unchanged{
|
||||
ID: lastUpdate,
|
||||
|
||||
Reference in New Issue
Block a user