Skip to content

Commit

Permalink
WIP: Add mutations for pages
Browse files Browse the repository at this point in the history
  • Loading branch information
bricesanchez committed Nov 18, 2017
1 parent c7256f3 commit 560d9b5
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api/app/graph/refinery/api/graphql_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Refinery
module Api
GraphqlSchema = GraphQL::Schema.define do
query Types::QueryType
# mutation Types::MutationType
mutation Types::MutationType

resolve_type -> (obj, args, ctx) {
type_name = obj.class.name
Expand Down
36 changes: 36 additions & 0 deletions api/app/graph/refinery/api/inputs/pages/page_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module Refinery
module Api
module Inputs
module Pages
PageInput = GraphQL::InputObjectType.define do
name 'PageInput'

input_field :parent_id, types.Int
input_field :path, types.String
input_field :show_in_menu, types.Boolean
input_field :link_url, types.String
input_field :menu_match, types.String
input_field :deletable, types.Boolean
input_field :draft, types.Boolean
input_field :skip_to_first_child, types.Boolean
input_field :lft, types.Int
input_field :rgt, types.Int
input_field :depth, types.Int
input_field :view_template, types.String
input_field :layout_template, types.String
input_field :locale, types.String
input_field :title, types.String
input_field :custom_slug, types.String
input_field :menu_title, types.String
input_field :slug, types.String
input_field :meta_description, types.String
input_field :browser_title, types.String

input_field :parts, types[Inputs::Pages::PagePartInput]
end
end
end
end
end
20 changes: 20 additions & 0 deletions api/app/graph/refinery/api/inputs/pages/page_part_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Refinery
module Api
module Inputs
module Pages
PagePartInput = GraphQL::InputObjectType.define do
name 'PagePartInput'

input_field :slug, types.String
input_field :position, types.Int
input_field :title, types.String

input_field :locale, types.String
input_field :body, types.String
end
end
end
end
end
60 changes: 60 additions & 0 deletions api/app/graph/refinery/api/mutations/pages/page_mutations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

module Refinery
module Api
module Mutations
module Pages
module PageMutations
Create = GraphQL::Relay::Mutation.define do
name 'CreatePage'
description 'Create a page'

input_field :page, !Inputs::Pages::PageInput

return_field :page, Types::Pages::PageType

resolve -> (obj, inputs, ctx) {
inputs = inputs.to_h.deep_symbolize_keys

page = Refinery::Page.create!(inputs[:page])

{ page: page }
}
end

Update = GraphQL::Relay::Mutation.define do
name 'UpdatePage'
description 'Create a page'

input_field :id, !types.ID
input_field :page, !Inputs::Pages::PageInput

return_field :page, Types::Pages::PageType

resolve -> (obj, inputs, ctx) {
inputs = inputs.to_h.deep_symbolize_keys

Refinery::Page.update(inputs[:id], inputs[:page])

{ page: page }
}
end

Delete = GraphQL::Relay::Mutation.define do
name 'DeletePage'

input_field :id, !types.ID

return_field :page, Types::Pages::PageType

resolve -> (obj, inputs, ctx) {
page = Refinery::Page.destroy(inputs[:id])

{ page: page }
}
end
end
end
end
end
end
16 changes: 16 additions & 0 deletions api/app/graph/refinery/api/types/mutation_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Refinery
module Api
module Types
MutationType = GraphQL::ObjectType.define do
name 'Mutation'
description 'The mutation root for this schema'

field :create_page, field: Mutations::Pages::PageMutations::Create.field
field :update_page, field: Mutations::Pages::PageMutations::Update.field
field :delete_page, field: Mutations::Pages::PageMutations::Delete.field
end
end
end
end
6 changes: 4 additions & 2 deletions api/spec/graph/refinery/api/fields/pages/page_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ module Pages
QUERY
end

subject { result }

context "as a normal user" do
let(:variables) do
{'query' => '', 'id' => page.id }
end

it 'returns a page' do
result_page = result['data']['page']
expect(result_page['title']).to eq(page.title)
subject
expect(result['data']['page']['title']).to eq(page.title)
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions api/spec/graph/refinery/api/fields/pages/pages_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Fields
module Pages
describe 'PagesField' do

let!(:page) { FactoryBot.create_list(:page, 5) }
let!(:pages) { FactoryBot.create_list(:page, 5) }

let(:context) { { } }
let(:variables) { { } }
Expand All @@ -31,10 +31,12 @@ module Pages
QUERY
end

subject { result }

context "as a normal user" do
it 'returns the pages' do
pages = result['data']['pages']
expect(pages.length).to eq(5)
subject
expect(result['data']['pages'].length).to eq(5)
end
end
end
Expand Down
73 changes: 73 additions & 0 deletions api/spec/graph/refinery/api/mutations/pages/page_mutations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

require 'spec_helper'

module Refinery
module Api
module Mutations
module Pages
describe 'DeletePageMutation' do
let(:logged_in_user) { Refinery::Core::NilUser.new }

let!(:page) { FactoryBot.create(:page) }

let(:context) { {current_user: logged_in_user} }

let(:result) do
GraphqlSchema.execute(
query_string,
context: context,
variables: variables
)
end

let(:query_string) do
<<-QUERY
mutation($page: DeletePageInput!) {
delete_page(input: $page) {
page {
id
}
}
}
QUERY
end

subject { result }

context 'Correct page id' do
let(:variables) { {'page': { 'id': page.id }} }

it 'deletes the page' do
subject
expect(Refinery::Page.find_by_id(page.id)).to be(nil)
end
end

context 'Incorrect page id' do
let(:variables) { {'page': { 'id': 1000 }} }

it 'does not delete the page' do
subject
expect(Refinery::Page.find_by_id(page.id)).to_not be(nil)
end
end

context 'Current user does not exist' do
let(:variables) { {'page': { 'id': page.id }} }
let(:context) { {current_user: nil} }

it 'returns an error' do
expect(subject['errors']).
to include(include('message' => "You're not authorized to do this"))
end

it 'returns no data' do
expect(subject['data']['delete_page']).to be(nil)
end
end
end
end
end
end
end

0 comments on commit 560d9b5

Please sign in to comment.