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.

49 lines
992 B

  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/log"
  8. )
  9. // LogsArgs is args for the logs query
  10. type LogsArgs struct {
  11. Filter *log.Filter
  12. }
  13. // Logs resolves the logs query
  14. func (r *QueryResolver) Logs(ctx context.Context, args *LogsArgs) ([]*types.LogResolver, error) {
  15. var logs []log.Log
  16. filter := args.Filter
  17. if filter == nil {
  18. filter = log.NewFilter().WithLimit(100)
  19. }
  20. logs, err := log.List(filter)
  21. if err != nil {
  22. return nil, err
  23. }
  24. if len(logs) >= 100 {
  25. loader := loader.FromContext(ctx)
  26. if loader == nil {
  27. return nil, errors.New("no loader")
  28. }
  29. for _, log := range logs {
  30. loader.PrimeCharacters("id", log.CharacterIDs...)
  31. loader.PrimeChannels("name", log.Channel)
  32. }
  33. }
  34. resolvers := make([]*types.LogResolver, len(logs))
  35. for i := range logs {
  36. resolvers[i] = &types.LogResolver{L: logs[i]}
  37. }
  38. return resolvers, nil
  39. }