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.

82 lines
2.5 KiB

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/xlerrors"
  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 xlerrors.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. // Issue returns graphcore.IssueResolver implementation.
  65. func (r *Resolver) Issue() graphcore.IssueResolver { return &issueResolver{r} }
  66. type issueResolver struct{ *Resolver }