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.

130 lines
2.7 KiB

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