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.

59 lines
1.2 KiB

  1. package queries
  2. import (
  3. "context"
  4. "git.aiterp.net/rpdata/api/graphql/resolver/types"
  5. "git.aiterp.net/rpdata/api/model/character"
  6. )
  7. // CharactersArgs is args for characters query
  8. type CharactersArgs struct {
  9. Filter *struct {
  10. IDs *[]string
  11. Nicks *[]string
  12. Names *[]string
  13. Author *string
  14. Search *string
  15. }
  16. }
  17. // Characters resolves the characters query
  18. func (r *QueryResolver) Characters(ctx context.Context, args *CharactersArgs) ([]*types.CharacterResolver, error) {
  19. var characters []character.Character
  20. var err error
  21. filter := args.Filter
  22. if filter != nil {
  23. var ids []string
  24. var nicks []string
  25. var names []string
  26. if filter.IDs != nil {
  27. ids = *filter.IDs
  28. }
  29. if filter.Nicks != nil {
  30. nicks = *filter.Nicks
  31. }
  32. if filter.Names != nil {
  33. names = *filter.Names
  34. }
  35. characters, err = character.ListFilter(ids, nicks, names, filter.Author, filter.Search)
  36. if err != nil {
  37. return nil, err
  38. }
  39. } else {
  40. characters, err = character.List()
  41. if err != nil {
  42. return nil, err
  43. }
  44. }
  45. resolvers := make([]*types.CharacterResolver, 0, len(characters))
  46. for _, character := range characters {
  47. resolvers = append(resolvers, &types.CharacterResolver{C: character})
  48. }
  49. return resolvers, nil
  50. }