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.

43 lines
950 B

  1. package queries
  2. import (
  3. "context"
  4. "errors"
  5. "git.aiterp.net/rpdata/api/graphql/resolver/types"
  6. "git.aiterp.net/rpdata/api/model/character"
  7. )
  8. // ErrIncorrectArguments is returned by character query.
  9. var ErrIncorrectArguments = errors.New("You can only query character by ID or Nick")
  10. // CharacterArgs is args for character query
  11. type CharacterArgs struct {
  12. ID *string
  13. Nick *string
  14. }
  15. // Character resolves the character query
  16. func (r *QueryResolver) Character(ctx context.Context, args *CharacterArgs) (*types.CharacterResolver, error) {
  17. var char character.Character
  18. var err error
  19. if args.Nick != nil && args.ID != nil {
  20. return nil, ErrIncorrectArguments
  21. }
  22. switch {
  23. case args.ID != nil:
  24. char, err = character.FindID(*args.ID)
  25. case args.Nick != nil:
  26. char, err = character.FindNick(*args.Nick)
  27. default:
  28. err = ErrIncorrectArguments
  29. }
  30. if err != nil {
  31. return nil, err
  32. }
  33. return &types.CharacterResolver{C: char}, nil
  34. }