package queries import ( "context" "errors" "strings" "git.aiterp.net/rpdata/api/internal/loader" "git.aiterp.net/rpdata/api/models" "git.aiterp.net/rpdata/api/models/logs" "github.com/99designs/gqlgen/graphql" ) func (r *resolver) Log(ctx context.Context, id string) (models.Log, error) { return logs.FindID(id) } func (r *resolver) Logs(ctx context.Context, filter *logs.Filter) ([]models.Log, error) { logs, err := logs.List(filter) if err != nil { return nil, err } reqCtx := graphql.GetRequestContext(ctx) maybeCharacters := strings.Contains(reqCtx.RawQuery, "characters") maybeChannels := strings.Contains(reqCtx.RawQuery, "channels") if len(logs) >= 100 && (maybeCharacters || maybeChannels) { loader := loader.FromContext(ctx) if loader == nil { return nil, errors.New("no loader") } for _, log := range logs { if maybeChannels { loader.PrimeChannels("name", log.ChannelName) } if maybeCharacters { loader.PrimeCharacters("id", log.CharacterIDs...) } } } return logs, nil }