package graph // This file will be automatically regenerated based on the schema, any resolver // implementations // will be copied through when generating and any unknown code will be moved to the end. // Code generated by github.com/99designs/gqlgen version v0.17.98 import ( "context" "fmt" "strings" "adam-french.co.uk/backend/graph/model" "adam-french.co.uk/backend/models" ) // CreatePost is the resolver for the createPost field. func (r *mutationResolver) CreatePost(ctx context.Context, input model.CreatePostInput) (*models.Post, error) { // @admin has already established that there is a session; the id is read // here because the post needs an author, not as a second gate. The !ok // branch is only reachable with a token that has admin but no usable id. userID, ok := UserIDFromCtx(ctx) if !ok { return nil, fmt.Errorf("unauthorized") } // The admin form submits whatever is in its fields, so an accidental click // on an empty form would otherwise publish a blank post to the feed. if strings.TrimSpace(input.Title) == "" { return nil, fmt.Errorf("a post needs a title") } post := models.Post{Title: input.Title, Content: input.Content, AuthorID: userID} if err := r.Store.DB.Create(&post).Error; err != nil { return nil, err } return &post, nil } // UpdatePost is the resolver for the updatePost field. func (r *mutationResolver) UpdatePost(ctx context.Context, id int, input model.UpdatePostInput) (*models.Post, error) { userID, ok := UserIDFromCtx(ctx) if !ok { return nil, fmt.Errorf("unauthorized") } var post models.Post if err := r.Store.DB.First(&post, id).Error; err != nil { return nil, fmt.Errorf("post not found") } // Admin alone (the @admin on the field) is not enough: a post may only be // edited by its author, so one admin cannot rewrite another's posts. This // is the kind of rule a directive cannot express, so it stays here. if post.AuthorID != userID { return nil, fmt.Errorf("you can only update your own posts") } // Both fields are non-pointer in the input, so this is a full replace — // omitting one in the mutation blanks it rather than leaving it alone. post.Title = input.Title post.Content = input.Content if err := r.Store.DB.Save(&post).Error; err != nil { return nil, err } return &post, nil } // DeletePost is the resolver for the deletePost field. func (r *mutationResolver) DeletePost(ctx context.Context, id int) (*models.Post, error) { userID, ok := UserIDFromCtx(ctx) if !ok { return nil, fmt.Errorf("unauthorized") } var post models.Post if err := r.Store.DB.First(&post, id).Error; err != nil { return nil, fmt.Errorf("post not found") } if post.AuthorID != userID { return nil, fmt.Errorf("you can only delete your own posts") } // Soft delete. The error is propagated: reporting success for a delete // that did not happen would leave the client showing the post as gone // until the next reload. if err := r.Store.DB.Delete(&post).Error; err != nil { return nil, err } return &post, nil } // Posts is the resolver for the posts field. func (r *queryResolver) Posts(ctx context.Context) ([]*models.Post, error) { // Preload issues a second query for the authors and fills in Post.Author. // Without it the field is nil and the schema's author field resolves to // null — there is no lazy loading and no error to tell you why. var posts []models.Post if err := r.Store.DB.Preload("Author").Order("created_at DESC").Find(&posts).Error; err != nil { return nil, err } return ptrs(posts), nil } // Post is the resolver for the post field. func (r *queryResolver) Post(ctx context.Context, id int) (*models.Post, error) { var post models.Post if err := r.Store.DB.Preload("Author").First(&post, id).Error; err != nil { return nil, fmt.Errorf("post not found") } return &post, nil }