From 560d9b5a5fc9178dcd946533c866641c4ef9cfa2 Mon Sep 17 00:00:00 2001 From: Brice Sanchez Date: Thu, 16 Nov 2017 21:48:20 -0500 Subject: [PATCH] WIP: Add mutations for pages --- api/app/graph/refinery/api/graphql_schema.rb | 2 +- .../refinery/api/inputs/pages/page_input.rb | 36 +++++++++ .../api/inputs/pages/page_part_input.rb | 20 +++++ .../api/mutations/pages/page_mutations.rb | 60 +++++++++++++++ .../graph/refinery/api/types/mutation_type.rb | 16 ++++ .../api/fields/pages/page_field_spec.rb | 6 +- .../api/fields/pages/pages_field_spec.rb | 8 +- .../mutations/pages/page_mutations_spec.rb | 73 +++++++++++++++++++ 8 files changed, 215 insertions(+), 6 deletions(-) create mode 100644 api/app/graph/refinery/api/inputs/pages/page_input.rb create mode 100644 api/app/graph/refinery/api/inputs/pages/page_part_input.rb create mode 100644 api/app/graph/refinery/api/mutations/pages/page_mutations.rb create mode 100644 api/app/graph/refinery/api/types/mutation_type.rb create mode 100644 api/spec/graph/refinery/api/mutations/pages/page_mutations_spec.rb diff --git a/api/app/graph/refinery/api/graphql_schema.rb b/api/app/graph/refinery/api/graphql_schema.rb index 78f91112e2..5c457b4dcf 100644 --- a/api/app/graph/refinery/api/graphql_schema.rb +++ b/api/app/graph/refinery/api/graphql_schema.rb @@ -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 diff --git a/api/app/graph/refinery/api/inputs/pages/page_input.rb b/api/app/graph/refinery/api/inputs/pages/page_input.rb new file mode 100644 index 0000000000..f6d3ac6166 --- /dev/null +++ b/api/app/graph/refinery/api/inputs/pages/page_input.rb @@ -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 \ No newline at end of file diff --git a/api/app/graph/refinery/api/inputs/pages/page_part_input.rb b/api/app/graph/refinery/api/inputs/pages/page_part_input.rb new file mode 100644 index 0000000000..33f52a5cfc --- /dev/null +++ b/api/app/graph/refinery/api/inputs/pages/page_part_input.rb @@ -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 \ No newline at end of file diff --git a/api/app/graph/refinery/api/mutations/pages/page_mutations.rb b/api/app/graph/refinery/api/mutations/pages/page_mutations.rb new file mode 100644 index 0000000000..42d2e3d8f2 --- /dev/null +++ b/api/app/graph/refinery/api/mutations/pages/page_mutations.rb @@ -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 \ No newline at end of file diff --git a/api/app/graph/refinery/api/types/mutation_type.rb b/api/app/graph/refinery/api/types/mutation_type.rb new file mode 100644 index 0000000000..671e5f2a6c --- /dev/null +++ b/api/app/graph/refinery/api/types/mutation_type.rb @@ -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 \ No newline at end of file diff --git a/api/spec/graph/refinery/api/fields/pages/page_field_spec.rb b/api/spec/graph/refinery/api/fields/pages/page_field_spec.rb index 7b5b5a132a..07d811e4db 100644 --- a/api/spec/graph/refinery/api/fields/pages/page_field_spec.rb +++ b/api/spec/graph/refinery/api/fields/pages/page_field_spec.rb @@ -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 diff --git a/api/spec/graph/refinery/api/fields/pages/pages_field_spec.rb b/api/spec/graph/refinery/api/fields/pages/pages_field_spec.rb index 8d51bc0011..dfb8a8db4b 100644 --- a/api/spec/graph/refinery/api/fields/pages/pages_field_spec.rb +++ b/api/spec/graph/refinery/api/fields/pages/pages_field_spec.rb @@ -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) { { } } @@ -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 diff --git a/api/spec/graph/refinery/api/mutations/pages/page_mutations_spec.rb b/api/spec/graph/refinery/api/mutations/pages/page_mutations_spec.rb new file mode 100644 index 0000000000..78210a32b1 --- /dev/null +++ b/api/spec/graph/refinery/api/mutations/pages/page_mutations_spec.rb @@ -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