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.

133 lines
2.8 KiB

  1. package loader
  2. import (
  3. "context"
  4. "errors"
  5. "log"
  6. "strings"
  7. "git.aiterp.net/rpdata/api/model/character"
  8. "github.com/graph-gophers/dataloader"
  9. )
  10. // Character gets a character by key
  11. func (loader *Loader) Character(key, value string) (character.Character, error) {
  12. if !strings.HasPrefix(key, "Character.") {
  13. key = "Character." + key
  14. }
  15. if loader.loaders[key] == nil {
  16. return character.Character{}, errors.New("unsupported key")
  17. }
  18. thunk := loader.loaders[key].Load(loader.ctx, dataloader.StringKey(value))
  19. res, err := thunk()
  20. if err != nil {
  21. return character.Character{}, err
  22. }
  23. char, ok := res.(character.Character)
  24. if !ok {
  25. return character.Character{}, errors.New("incorrect type")
  26. }
  27. return char, nil
  28. }
  29. // Characters gets characters by key
  30. func (loader *Loader) Characters(key string, values ...string) ([]character.Character, error) {
  31. if !strings.HasPrefix(key, "Character.") {
  32. key = "Character." + key
  33. }
  34. if loader.loaders[key] == nil {
  35. return nil, errors.New("unsupported key")
  36. }
  37. thunk := loader.loaders[key].LoadMany(loader.ctx, dataloader.NewKeysFromStrings(values))
  38. res, errs := thunk()
  39. for _, err := range errs {
  40. if err != nil && err != ErrNotFound {
  41. return nil, err
  42. }
  43. }
  44. chars := make([]character.Character, len(res))
  45. for i := range res {
  46. char, ok := res[i].(character.Character)
  47. if !ok {
  48. return nil, errors.New("incorrect type")
  49. }
  50. chars[i] = char
  51. }
  52. return chars, nil
  53. }
  54. func characterIDBatch(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
  55. results := make([]*dataloader.Result, 0, len(keys))
  56. ids := keys.Keys()
  57. log.Println("Loading", len(ids), "characters:", strings.Join(ids, ","))
  58. characters, err := character.ListIDs(ids...)
  59. if err != nil {
  60. for range ids {
  61. results = append(results, &dataloader.Result{Error: err})
  62. }
  63. return results
  64. }
  65. for _, id := range ids {
  66. found := false
  67. for _, character := range characters {
  68. if character.ID == id {
  69. results = append(results, &dataloader.Result{Data: character})
  70. found = true
  71. break
  72. }
  73. }
  74. if !found {
  75. results = append(results, &dataloader.Result{Data: character.Character{}, Error: ErrNotFound})
  76. }
  77. }
  78. return results
  79. }
  80. func characterNickBatch(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
  81. var results []*dataloader.Result
  82. nicks := keys.Keys()
  83. characters, err := character.ListNicks(nicks...)
  84. if err != nil {
  85. for range nicks {
  86. results = append(results, &dataloader.Result{Error: err})
  87. }
  88. return results
  89. }
  90. for _, nick := range nicks {
  91. found := false
  92. for i := range characters {
  93. if characters[i].HasNick(nick) {
  94. results = append(results, &dataloader.Result{Data: characters[i]})
  95. found = true
  96. break
  97. }
  98. }
  99. if !found {
  100. results = append(results, &dataloader.Result{Data: character.Character{}, Error: err})
  101. }
  102. }
  103. return results
  104. }