Browse Source

GraphQL: Fixed stories resolver crashing if filter is nil.

1.0
Gisle Aune 6 years ago
parent
commit
20e53b4f5e
  1. 38
      graphql/resolver/queries/stories.go

38
graphql/resolver/queries/stories.go

@ -28,20 +28,29 @@ type StoriesArg struct {
// Stories resolves the stories query // Stories resolves the stories query
func (r *QueryResolver) Stories(ctx context.Context, args *StoriesArg) ([]*types.StoryResolver, error) { func (r *QueryResolver) Stories(ctx context.Context, args *StoriesArg) ([]*types.StoryResolver, error) {
var err error
filter := args.Filter filter := args.Filter
author := "" author := ""
if filter != nil && filter.Author != nil {
category := ""
tags := make([]story.Tag, 0, 8)
earliest := time.Time{}
latest := time.Time{}
unlisted := false
open := (*bool)(nil)
limit := 0
if filter != nil {
if filter.Author != nil {
author = *filter.Author author = *filter.Author
} }
category := ""
if filter != nil && filter.Category != nil {
if filter.Category != nil {
category = *filter.Category category = *filter.Category
} }
tags := make([]story.Tag, 0, 8)
if filter != nil && filter.Tags != nil {
if filter.Tags != nil {
for _, tagInput := range *filter.Tags { for _, tagInput := range *filter.Tags {
tags = append(tags, story.Tag{ tags = append(tags, story.Tag{
Kind: tagInput.Kind, Kind: tagInput.Kind,
@ -50,24 +59,25 @@ func (r *QueryResolver) Stories(ctx context.Context, args *StoriesArg) ([]*types
} }
} }
earliest := time.Time{}
err := error(nil)
if filter != nil && filter.EarliestFictionalDate != nil {
if filter.EarliestFictionalDate != nil {
earliest, err = time.Parse(time.RFC3339Nano, *filter.EarliestFictionalDate) earliest, err = time.Parse(time.RFC3339Nano, *filter.EarliestFictionalDate)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
latest := time.Time{}
if filter != nil && filter.LatestFictionalDate != nil {
if filter.LatestFictionalDate != nil {
latest, err = time.Parse(time.RFC3339Nano, *filter.LatestFictionalDate) latest, err = time.Parse(time.RFC3339Nano, *filter.LatestFictionalDate)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
unlisted := filter.Unlisted != nil && *filter.Unlisted == true
if filter.Limit != nil {
limit = int(*filter.Limit)
}
unlisted = filter.Unlisted != nil && *filter.Unlisted == true
if unlisted { if unlisted {
user := session.FromContext(ctx).User() user := session.FromContext(ctx).User()
if user == nil { if user == nil {
@ -81,11 +91,7 @@ func (r *QueryResolver) Stories(ctx context.Context, args *StoriesArg) ([]*types
author = user.ID author = user.ID
} }
open := filter.Open
limit := 0
if filter != nil && filter.Limit != nil {
limit = int(*filter.Limit)
open = filter.Open
} }
stories, err := story.List(author, category, tags, earliest, latest, unlisted, open, limit) stories, err := story.List(author, category, tags, earliest, latest, unlisted, open, limit)

Loading…
Cancel
Save