Skip to content

Commit 560d9b5

Browse files
committed
WIP: Add mutations for pages
1 parent c7256f3 commit 560d9b5

File tree

8 files changed

+215
-6
lines changed

8 files changed

+215
-6
lines changed

api/app/graph/refinery/api/graphql_schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Refinery
44
module Api
55
GraphqlSchema = GraphQL::Schema.define do
66
query Types::QueryType
7-
# mutation Types::MutationType
7+
mutation Types::MutationType
88

99
resolve_type -> (obj, args, ctx) {
1010
type_name = obj.class.name
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
module Refinery
4+
module Api
5+
module Inputs
6+
module Pages
7+
PageInput = GraphQL::InputObjectType.define do
8+
name 'PageInput'
9+
10+
input_field :parent_id, types.Int
11+
input_field :path, types.String
12+
input_field :show_in_menu, types.Boolean
13+
input_field :link_url, types.String
14+
input_field :menu_match, types.String
15+
input_field :deletable, types.Boolean
16+
input_field :draft, types.Boolean
17+
input_field :skip_to_first_child, types.Boolean
18+
input_field :lft, types.Int
19+
input_field :rgt, types.Int
20+
input_field :depth, types.Int
21+
input_field :view_template, types.String
22+
input_field :layout_template, types.String
23+
input_field :locale, types.String
24+
input_field :title, types.String
25+
input_field :custom_slug, types.String
26+
input_field :menu_title, types.String
27+
input_field :slug, types.String
28+
input_field :meta_description, types.String
29+
input_field :browser_title, types.String
30+
31+
input_field :parts, types[Inputs::Pages::PagePartInput]
32+
end
33+
end
34+
end
35+
end
36+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module Refinery
4+
module Api
5+
module Inputs
6+
module Pages
7+
PagePartInput = GraphQL::InputObjectType.define do
8+
name 'PagePartInput'
9+
10+
input_field :slug, types.String
11+
input_field :position, types.Int
12+
input_field :title, types.String
13+
14+
input_field :locale, types.String
15+
input_field :body, types.String
16+
end
17+
end
18+
end
19+
end
20+
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# frozen_string_literal: true
2+
3+
module Refinery
4+
module Api
5+
module Mutations
6+
module Pages
7+
module PageMutations
8+
Create = GraphQL::Relay::Mutation.define do
9+
name 'CreatePage'
10+
description 'Create a page'
11+
12+
input_field :page, !Inputs::Pages::PageInput
13+
14+
return_field :page, Types::Pages::PageType
15+
16+
resolve -> (obj, inputs, ctx) {
17+
inputs = inputs.to_h.deep_symbolize_keys
18+
19+
page = Refinery::Page.create!(inputs[:page])
20+
21+
{ page: page }
22+
}
23+
end
24+
25+
Update = GraphQL::Relay::Mutation.define do
26+
name 'UpdatePage'
27+
description 'Create a page'
28+
29+
input_field :id, !types.ID
30+
input_field :page, !Inputs::Pages::PageInput
31+
32+
return_field :page, Types::Pages::PageType
33+
34+
resolve -> (obj, inputs, ctx) {
35+
inputs = inputs.to_h.deep_symbolize_keys
36+
37+
Refinery::Page.update(inputs[:id], inputs[:page])
38+
39+
{ page: page }
40+
}
41+
end
42+
43+
Delete = GraphQL::Relay::Mutation.define do
44+
name 'DeletePage'
45+
46+
input_field :id, !types.ID
47+
48+
return_field :page, Types::Pages::PageType
49+
50+
resolve -> (obj, inputs, ctx) {
51+
page = Refinery::Page.destroy(inputs[:id])
52+
53+
{ page: page }
54+
}
55+
end
56+
end
57+
end
58+
end
59+
end
60+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
module Refinery
4+
module Api
5+
module Types
6+
MutationType = GraphQL::ObjectType.define do
7+
name 'Mutation'
8+
description 'The mutation root for this schema'
9+
10+
field :create_page, field: Mutations::Pages::PageMutations::Create.field
11+
field :update_page, field: Mutations::Pages::PageMutations::Update.field
12+
field :delete_page, field: Mutations::Pages::PageMutations::Delete.field
13+
end
14+
end
15+
end
16+
end

api/spec/graph/refinery/api/fields/pages/page_field_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ module Pages
3131
QUERY
3232
end
3333

34+
subject { result }
35+
3436
context "as a normal user" do
3537
let(:variables) do
3638
{'query' => '', 'id' => page.id }
3739
end
3840

3941
it 'returns a page' do
40-
result_page = result['data']['page']
41-
expect(result_page['title']).to eq(page.title)
42+
subject
43+
expect(result['data']['page']['title']).to eq(page.title)
4244
end
4345
end
4446
end

api/spec/graph/refinery/api/fields/pages/pages_field_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Fields
88
module Pages
99
describe 'PagesField' do
1010

11-
let!(:page) { FactoryBot.create_list(:page, 5) }
11+
let!(:pages) { FactoryBot.create_list(:page, 5) }
1212

1313
let(:context) { { } }
1414
let(:variables) { { } }
@@ -31,10 +31,12 @@ module Pages
3131
QUERY
3232
end
3333

34+
subject { result }
35+
3436
context "as a normal user" do
3537
it 'returns the pages' do
36-
pages = result['data']['pages']
37-
expect(pages.length).to eq(5)
38+
subject
39+
expect(result['data']['pages'].length).to eq(5)
3840
end
3941
end
4042
end
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
module Refinery
6+
module Api
7+
module Mutations
8+
module Pages
9+
describe 'DeletePageMutation' do
10+
let(:logged_in_user) { Refinery::Core::NilUser.new }
11+
12+
let!(:page) { FactoryBot.create(:page) }
13+
14+
let(:context) { {current_user: logged_in_user} }
15+
16+
let(:result) do
17+
GraphqlSchema.execute(
18+
query_string,
19+
context: context,
20+
variables: variables
21+
)
22+
end
23+
24+
let(:query_string) do
25+
<<-QUERY
26+
mutation($page: DeletePageInput!) {
27+
delete_page(input: $page) {
28+
page {
29+
id
30+
}
31+
}
32+
}
33+
QUERY
34+
end
35+
36+
subject { result }
37+
38+
context 'Correct page id' do
39+
let(:variables) { {'page': { 'id': page.id }} }
40+
41+
it 'deletes the page' do
42+
subject
43+
expect(Refinery::Page.find_by_id(page.id)).to be(nil)
44+
end
45+
end
46+
47+
context 'Incorrect page id' do
48+
let(:variables) { {'page': { 'id': 1000 }} }
49+
50+
it 'does not delete the page' do
51+
subject
52+
expect(Refinery::Page.find_by_id(page.id)).to_not be(nil)
53+
end
54+
end
55+
56+
context 'Current user does not exist' do
57+
let(:variables) { {'page': { 'id': page.id }} }
58+
let(:context) { {current_user: nil} }
59+
60+
it 'returns an error' do
61+
expect(subject['errors']).
62+
to include(include('message' => "You're not authorized to do this"))
63+
end
64+
65+
it 'returns no data' do
66+
expect(subject['data']['delete_page']).to be(nil)
67+
end
68+
end
69+
end
70+
end
71+
end
72+
end
73+
end

0 commit comments

Comments
 (0)