package graph import ( "context" "fmt" "log/slog" "gitlab.com/unboundsoftware/eventsourced/eventsourced" "gitea.unbound.se/unboundsoftware/schemas/cache" "gitea.unbound.se/unboundsoftware/schemas/middleware" ) //go:generate go run github.com/99designs/gqlgen //go:generate gofumpt -w . //go:generate goimports -w -local gitea.unbound.se/unboundsoftware/schemas . // 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(ctx context.Context, event eventsourced.Event) error } type Resolver struct { EventStore eventsourced.EventStore Publisher Publisher Logger *slog.Logger Cache *cache.Cache PubSub *PubSub CosmoGenerator *CosmoGenerator Debouncer *Debouncer } func (r *Resolver) apiKeyCanAccessRef(ctx context.Context, ref string, publish bool) (string, error) { key, err := middleware.ApiKeyFromContext(ctx) if err != nil { return "", err } apiKey := r.Cache.ApiKeyByKey(key) if publish && !apiKey.Publish { return "", fmt.Errorf("provided API-key doesn't have publish privilege") } if !publish && !apiKey.Read { return "", fmt.Errorf("provided API-key doesn't have read privilege") } for _, rr := range apiKey.Refs { if rr == ref { return apiKey.Name, nil } } return "", fmt.Errorf("provided API-key doesn't have the required privilege on the requested Schema Ref") } func (r *Resolver) handler(ctx context.Context, aggregate eventsourced.Aggregate) (eventsourced.CommandHandler, error) { return eventsourced.NewHandler(ctx, aggregate, r.EventStore, eventsourced.WithEventPublisher(r.Publisher)) } func apiKeyId(orgId, name string) string { return fmt.Sprintf("%s-%s", orgId, name) }