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.

111 lines
3.3 KiB

5 years ago
5 years ago
  1. package resolvers
  2. // This file will be automatically regenerated based on the schema, any resolver implementations
  3. // will be copied through when generating and any unknown code will be moved to the end.
  4. import (
  5. "context"
  6. "git.aiterp.net/stufflog/server/graph/graphcore"
  7. "git.aiterp.net/stufflog/server/graph/graphutil"
  8. "git.aiterp.net/stufflog/server/graph/loaders"
  9. "git.aiterp.net/stufflog/server/internal/slerrors"
  10. "git.aiterp.net/stufflog/server/models"
  11. )
  12. func (r *issueResolver) Project(ctx context.Context, obj *models.Issue) (*models.Project, error) {
  13. return r.Database.Projects().Find(ctx, obj.ProjectID)
  14. }
  15. func (r *issueResolver) Owner(ctx context.Context, obj *models.Issue) (*models.User, error) {
  16. if obj.OwnerID == "" {
  17. return nil, nil
  18. }
  19. // Shortcut: if only ID is needed, resolve with empty user.
  20. if !graphutil.SelectsAnyField(ctx, "name", "active", "admin") {
  21. return &models.User{ID: obj.OwnerID}, nil
  22. }
  23. return loaders.UserLoaderFromContext(ctx).Load(obj.OwnerID)
  24. }
  25. func (r *issueResolver) Assignee(ctx context.Context, obj *models.Issue) (*models.User, error) {
  26. if obj.AssigneeID == "" {
  27. return nil, nil
  28. }
  29. // Shortcut: if only ID is needed, resolve with empty user.
  30. if !graphutil.SelectsAnyField(ctx, "name", "active", "admin") {
  31. return &models.User{ID: obj.AssigneeID}, nil
  32. }
  33. return loaders.UserLoaderFromContext(ctx).Load(obj.AssigneeID)
  34. }
  35. func (r *issueResolver) Status(ctx context.Context, obj *models.Issue) (*models.ProjectStatus, error) {
  36. // Shortcut: if description isn't needed, resolve this with issue's properties.
  37. if !graphutil.SelectsAnyField(ctx, "description") {
  38. return &models.ProjectStatus{
  39. ProjectID: obj.ProjectID,
  40. Stage: obj.StatusStage,
  41. Name: obj.StatusName,
  42. Description: "FAKE",
  43. }, nil
  44. }
  45. status, err := r.Database.ProjectStatuses().Find(ctx, obj.ProjectID, obj.StatusName)
  46. if slerrors.IsNotFound(err) {
  47. return &models.ProjectStatus{
  48. ProjectID: obj.ProjectID,
  49. Stage: obj.StatusStage,
  50. Name: obj.StatusName,
  51. Description: "(Deleted or unknown status)",
  52. }, nil
  53. } else if err != nil {
  54. return nil, err
  55. }
  56. // If the stage doesn't match, sneakily correct it for next time.
  57. if status.Stage != obj.StatusStage {
  58. updatedIssue := *obj
  59. updatedIssue.StatusStage = status.Stage
  60. _ = r.Database.Issues().Save(ctx, updatedIssue)
  61. }
  62. return status, nil
  63. }
  64. func (r *issueResolver) Tasks(ctx context.Context, obj *models.Issue, filter *models.IssueTaskFilter) ([]*models.IssueTask, error) {
  65. if filter == nil {
  66. filter = &models.IssueTaskFilter{}
  67. }
  68. filter.IssueTaskIDs = []string{obj.ID}
  69. return r.Database.IssueTasks().List(ctx, *filter)
  70. }
  71. func (r *issueResolver) Items(ctx context.Context, obj *models.Issue, filter *models.IssueItemFilter) ([]*models.IssueItem, error) {
  72. if filter == nil {
  73. filter = &models.IssueItemFilter{}
  74. }
  75. filter.IssueIDs = []string{obj.ID}
  76. return r.Database.IssueItems().List(ctx, *filter)
  77. }
  78. func (r *issueResolver) Logs(ctx context.Context, obj *models.Issue) ([]*models.Log, error) {
  79. loader := loaders.LogsByIssueLoaderFromContext(ctx)
  80. logs, err := loader.Load(obj.ID)
  81. if err != nil {
  82. return nil, err
  83. }
  84. r.Auth.FilterLogList(ctx, &logs)
  85. return logs, nil
  86. }
  87. // Issue returns graphcore.IssueResolver implementation.
  88. func (r *Resolver) Issue() graphcore.IssueResolver { return &issueResolver{r} }
  89. type issueResolver struct{ *Resolver }