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.

54 lines
1.1 KiB

  1. package queries
  2. import (
  3. "context"
  4. "errors"
  5. "git.aiterp.net/rpdata/api/graphql/loader"
  6. "git.aiterp.net/rpdata/api/graphql/resolver/types"
  7. "git.aiterp.net/rpdata/api/model/character"
  8. )
  9. // CharactersArgs is args for characters query
  10. type CharactersArgs struct {
  11. IDs *[]string
  12. Nicks *[]string
  13. Author *string
  14. }
  15. // Characters resolves the characters query
  16. func (r *QueryResolver) Characters(ctx context.Context, args *CharactersArgs) ([]*types.CharacterResolver, error) {
  17. var chars []character.Character
  18. var err error
  19. loader := loader.FromContext(ctx)
  20. if loader == nil {
  21. return nil, errors.New("no loader")
  22. }
  23. switch {
  24. case args.IDs != nil:
  25. chars, err = character.ListIDs(*args.IDs...)
  26. case args.Nicks != nil:
  27. chars, err = character.ListNicks(*args.Nicks...)
  28. case args.Author != nil:
  29. chars, err = character.ListAuthor(*args.Author)
  30. default:
  31. chars, err = character.List()
  32. }
  33. if err != nil {
  34. return nil, err
  35. }
  36. resolvers := make([]*types.CharacterResolver, 0, len(chars))
  37. for i := range chars {
  38. if chars[i].ID == "" {
  39. continue
  40. }
  41. resolvers = append(resolvers, &types.CharacterResolver{C: chars[i]})
  42. }
  43. return resolvers, nil
  44. }