Skip to content

Commit

Permalink
Add create mutation for review
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyMatskevich committed Nov 26, 2019
1 parent 8820916 commit f9a46eb
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 2 deletions.
29 changes: 29 additions & 0 deletions app/graphql/mutations/create_review.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Mutations
class CreateReview < BaseMutation
argument :body, String, required: true do
description "New products name"
end

argument :rating, Integer, required: true do
description "New products weight"
end

argument :product_id, ID, required: true do
description "New products description for preview page"
end

field :review, Types::ReviewType, null: true

def resolve(body:, rating:, product_id:)
review = Review.new(body: body, rating: rating, product_id: product_id, user: context[:current_user])

authorize! review, to: :create?

if review.save
valid(review)
else
invalid(review)
end
end
end
end
1 change: 1 addition & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ class MutationType < Types::BaseObject

field :create_product, mutation: Mutations::CreateProduct
field :create_direct_upload, mutation: Mutations::CreateDirectUpload
field :create_review, mutation: Mutations::CreateReview
end
end
5 changes: 5 additions & 0 deletions app/policies/review_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ReviewPolicy < ApplicationPolicy
def create?
true
end
end
16 changes: 14 additions & 2 deletions spec/graphql/mutations/create_product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
mutation createProduct($name: String!, $weight: Int!, $price: Int!, $previewDescription: String!, $image: String!) {
createProduct(name: $name, weight: $weight, price: $price, previewDescription: $previewDescription, image: $image) {
product {
id
name
weight
price
Expand Down Expand Up @@ -42,8 +41,21 @@
context: {current_user: user}
end

let(:expected_result) {
{
"product" => {
"name" => "Example product",
"weight" => 30,
"price" => 5,
"previewDescription" => "Some description",
},
"errors" => [],
"success" => true,
}
}

it "return the product object" do
expect(gql_response.data[mutation_type]["product"]).to include("name" => "Example product")
expect(gql_response.data[mutation_type]).to eq expected_result
end
end
end
61 changes: 61 additions & 0 deletions spec/graphql/mutations/create_review_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require "rails_helper"

RSpec.describe Mutations::CreateReview do
subject { described_class }

it { is_expected.to have_a_field(:review).of_type(Types::ReviewType) }

describe "create Review" do
let(:user) { create(:user) }
let(:product) { create(:product) }
let(:mutation_type) { "createReview" }
let(:mutation_string) {
<<-GRAPHQL
mutation createReview($body: String!, $rating: Int!, $productId: ID!) {
createReview(body: $body, rating: $rating, productId: $productId) {
review {
body
rating
product {
id
}
}
errors {
message
field
details
}
success
}
}
GRAPHQL
}

before do
mutation mutation_string,
variables: {
body: "Example review",
rating: 4,
productId: product.id,
},
context: {current_user: user}
end

let(:expected_result) {
{
"review" => {
"body" => "Example review",
"rating" => 4,
"product" => {
"id" => product.id.to_s
}
},
"errors" => [],
"success" => true
}
}
it "return the product object" do
expect(gql_response.data[mutation_type]).to eq expected_result
end
end
end
13 changes: 13 additions & 0 deletions spec/policies/review_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "rails_helper"
require "action_policy/rspec/dsl"

describe ReviewPolicy do
let(:user) { build_stubbed :user }
let(:record) { build_stubbed :review }

let(:context) { {user: user} }

describe_rule :create? do
succeed "when user with user role"
end
end

0 comments on commit f9a46eb

Please sign in to comment.