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.

46 lines
1.0 KiB

  1. package queries
  2. import (
  3. "context"
  4. "errors"
  5. "strings"
  6. "git.aiterp.net/rpdata/api/internal/loader"
  7. "git.aiterp.net/rpdata/api/models"
  8. "git.aiterp.net/rpdata/api/models/logs"
  9. "github.com/99designs/gqlgen/graphql"
  10. )
  11. func (r *resolver) Log(ctx context.Context, id string) (models.Log, error) {
  12. return logs.FindID(id)
  13. }
  14. func (r *resolver) Logs(ctx context.Context, filter *logs.Filter) ([]models.Log, error) {
  15. logs, err := logs.List(filter)
  16. if err != nil {
  17. return nil, err
  18. }
  19. reqCtx := graphql.GetRequestContext(ctx)
  20. maybeCharacters := strings.Contains(reqCtx.RawQuery, "characters")
  21. maybeChannels := strings.Contains(reqCtx.RawQuery, "channels")
  22. if len(logs) >= 100 && (maybeCharacters || maybeChannels) {
  23. loader := loader.FromContext(ctx)
  24. if loader == nil {
  25. return nil, errors.New("no loader")
  26. }
  27. for _, log := range logs {
  28. if maybeChannels {
  29. loader.PrimeChannels("name", log.ChannelName)
  30. }
  31. if maybeCharacters {
  32. loader.PrimeCharacters("id", log.CharacterIDs...)
  33. }
  34. }
  35. }
  36. return logs, nil
  37. }