package queries import ( "context" "time" "git.aiterp.net/rpdata/api/graphql/resolver/types" "git.aiterp.net/rpdata/api/model/story" ) // StoriesArg is args for stories query type StoriesArg struct { Filter *struct { Author *string Tags *[]struct { Kind string Name string } EarliestFictionalDate *string LatestFictionalDate *string Limit *int32 } } // Stories resolves the stories query func (r *QueryResolver) Stories(ctx context.Context, args *StoriesArg) ([]*types.StoryResolver, error) { filter := args.Filter author := "" if filter != nil && filter.Author != nil { author = *filter.Author } tags := make([]story.Tag, 0, 8) if filter != nil && filter.Tags != nil { for _, tagInput := range *filter.Tags { tags = append(tags, story.Tag{ Kind: tagInput.Kind, Name: tagInput.Name, }) } } earliest := time.Time{} err := error(nil) if filter != nil && filter.EarliestFictionalDate != nil { earliest, err = time.Parse(time.RFC3339Nano, *filter.EarliestFictionalDate) if err != nil { return nil, err } } latest := time.Time{} if filter != nil && filter.LatestFictionalDate != nil { latest, err = time.Parse(time.RFC3339Nano, *filter.LatestFictionalDate) if err != nil { return nil, err } } limit := 30 if filter != nil && filter.Limit != nil { limit = int(*filter.Limit) } stories, err := story.List(author, tags, earliest, latest, limit) if err != nil { return nil, err } resolvers := make([]*types.StoryResolver, len(stories)) for i, story := range stories { resolvers[i] = &types.StoryResolver{S: story} } return resolvers, nil }