-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7256f3
commit 560d9b5
Showing
8 changed files
with
215 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
api/app/graph/refinery/api/inputs/pages/page_part_input.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
60
api/app/graph/refinery/api/mutations/pages/page_mutations.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
api/spec/graph/refinery/api/mutations/pages/page_mutations_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |