GraphQL API and utilities for the rpdata project
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.

57 lines
1.3 KiB

  1. package types
  2. import (
  3. "context"
  4. "errors"
  5. "git.aiterp.net/rpdata/api/internal/loader"
  6. "git.aiterp.net/rpdata/api/models"
  7. "git.aiterp.net/rpdata/api/models/posts"
  8. )
  9. type logResolver struct{}
  10. func (r *logResolver) Channel(ctx context.Context, log *models.Log) (*models.Channel, error) {
  11. loader := loader.FromContext(ctx)
  12. if loader == nil {
  13. return nil, errors.New("no loader")
  14. }
  15. return loader.Channel("name", log.ChannelName)
  16. }
  17. func (r *logResolver) Characters(ctx context.Context, log *models.Log) ([]*models.Character, error) {
  18. loader := loader.FromContext(ctx)
  19. if loader == nil {
  20. return nil, errors.New("no loader")
  21. }
  22. characters, err := loader.Characters("id", log.CharacterIDs...)
  23. if err != nil {
  24. return nil, err
  25. }
  26. characters2 := make([]*models.Character, len(characters))
  27. for i := range characters {
  28. characters2[i] = &characters[i]
  29. }
  30. return characters2, nil
  31. }
  32. func (r *logResolver) Posts(ctx context.Context, log *models.Log, kinds []string) ([]*models.Post, error) {
  33. posts, err := posts.List(&posts.Filter{LogID: &log.ShortID, Kind: kinds, Limit: 0})
  34. if err != nil {
  35. return nil, err
  36. }
  37. posts2 := make([]*models.Post, len(posts))
  38. for i := range posts {
  39. posts2[i] = &posts[i]
  40. }
  41. return posts2, nil
  42. }
  43. // LogResolver is a resolver
  44. var LogResolver logResolver