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.

54 lines
1.2 KiB

  1. package loaders
  2. import (
  3. "context"
  4. "git.aiterp.net/stufflog/server/database/repositories"
  5. "git.aiterp.net/stufflog/server/internal/slerrors"
  6. "git.aiterp.net/stufflog/server/models"
  7. "time"
  8. )
  9. // go run github.com/vektah/dataloaden IssueLoader string \*git.aiterp.net/stufflog/server/models.Issue
  10. var issueLoaderCtxKey = "ctx.stufflog.IssueLoader"
  11. func IssueLoaderFromContext(ctx context.Context) *IssueLoader {
  12. return ctx.Value(issueLoaderCtxKey).(*IssueLoader)
  13. }
  14. func NewIssueLoader(ctx context.Context, issueRepo repositories.IssueRepository) *IssueLoader {
  15. return &IssueLoader{
  16. fetch: func(keys []string) ([]*models.Issue, []error) {
  17. results := make([]*models.Issue, len(keys))
  18. errors := make([]error, len(keys))
  19. issues, err := issueRepo.List(ctx, models.IssueFilter{IssueIDs: keys})
  20. if err != nil {
  21. for i := range errors {
  22. errors[i] = err
  23. }
  24. return results, errors
  25. }
  26. for i, key := range keys {
  27. found := false
  28. for _, issue := range issues {
  29. if issue.ID == key {
  30. found = true
  31. results[i] = issue
  32. break
  33. }
  34. }
  35. if !found {
  36. errors[i] = slerrors.NotFound("Issue")
  37. }
  38. }
  39. return results, errors
  40. },
  41. wait: time.Millisecond / 2,
  42. maxBatch: 16,
  43. }
  44. }