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.

51 lines
1.1 KiB

  1. package resolver
  2. import (
  3. "context"
  4. "git.aiterp.net/rpdata/api/internal/session"
  5. )
  6. // LoginArgs is args
  7. type LoginArgs struct {
  8. Username string
  9. Password string
  10. }
  11. // Session resolves query.session
  12. func (r *QueryResolver) Session(ctx context.Context) (*SessionResolver, error) {
  13. return &SessionResolver{S: session.FromContext(ctx)}, nil
  14. }
  15. // Login resolves mutation.login
  16. func (r *MutationResolver) Login(ctx context.Context, args *LoginArgs) (*SessionResolver, error) {
  17. session := session.FromContext(ctx)
  18. err := session.Login(args.Username, args.Password)
  19. if err != nil {
  20. return nil, err
  21. }
  22. return &SessionResolver{S: session}, nil
  23. }
  24. // Logout resolves mutation.logout
  25. func (r *MutationResolver) Logout(ctx context.Context) (*SessionResolver, error) {
  26. session := session.FromContext(ctx)
  27. session.Logout()
  28. return &SessionResolver{S: session}, nil
  29. }
  30. // SessionResolver resolves Session
  31. type SessionResolver struct{ S *session.Session }
  32. // User resolves Session.user
  33. func (r *SessionResolver) User() *UserResolver {
  34. user := r.S.User()
  35. if user == nil {
  36. return nil
  37. }
  38. return &UserResolver{U: user}
  39. }