stufflog graphql server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.1 KiB

  1. package loaders
  2. import (
  3. "context"
  4. "git.aiterp.net/stufflog/server/database/repositories"
  5. "git.aiterp.net/stufflog/server/models"
  6. "time"
  7. )
  8. // go run github.com/vektah/dataloaden LogsByIssueLoader string []\*git.aiterp.net/stufflog/server/models.Log
  9. var logsByIssueLoaderCtxKey = "ctx.stufflog.IssuesByLogLoader"
  10. func LogsByIssueLoaderFromContext(ctx context.Context) *LogsByIssueLoader {
  11. return ctx.Value(logsByIssueLoaderCtxKey).(*LogsByIssueLoader)
  12. }
  13. func NewLogsByIssueLoader(ctx context.Context, logsRepo repositories.LogRepository) *LogsByIssueLoader {
  14. return &LogsByIssueLoader{
  15. fetch: func(keys []string) ([][]*models.Log, []error) {
  16. results := make([][]*models.Log, len(keys))
  17. errors := make([]error, len(keys))
  18. logs, err := logsRepo.List(ctx, models.LogFilter{IssueIDs: keys})
  19. if err != nil {
  20. for i := range errors {
  21. errors[i] = err
  22. }
  23. return results, errors
  24. }
  25. for i, key := range keys {
  26. for _, log := range logs {
  27. if log.MatchesIssue(key) {
  28. results[i] = append(results[i], log)
  29. }
  30. }
  31. }
  32. return results, nil
  33. },
  34. wait: time.Millisecond / 2,
  35. maxBatch: 16,
  36. }
  37. }