diff --git a/api/app/graph/refinery/api/fields_deprecated/pages/page_field.rb b/api/app/graph/refinery/api/fields_deprecated/pages/page_field.rb deleted file mode 100644 index f756576871..0000000000 --- a/api/app/graph/refinery/api/fields_deprecated/pages/page_field.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -module Refinery - module Api - module Fields - module Pages - class PageField < GraphQL::Schema::Field - graphql_name 'Page' - description 'Find a page by ID' - - type Types::Pages::PageType - argument :id, !types.ID - - resolve -> (obj, args, ctx) { - Refinery::Page.find_by_id(args[:id]) - } - end - end - end - end -end \ No newline at end of file diff --git a/api/app/graph/refinery/api/fields_deprecated/pages/pages_field.rb b/api/app/graph/refinery/api/fields_deprecated/pages/pages_field.rb deleted file mode 100644 index c047732b52..0000000000 --- a/api/app/graph/refinery/api/fields_deprecated/pages/pages_field.rb +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true - -module Refinery - module Api - module Fields - module Pages - class PagesField < GraphQL::Schema::Field - graphql_name 'Pages' - description 'Find all pages' - - type types[Types::Pages::PageType] - - resolve -> (obj, args, ctx) { - Refinery::Page.all - } - end - end - end - end -end \ No newline at end of file diff --git a/api/app/graph/refinery/api/graphql_schema.rb b/api/app/graph/refinery/api/graphql_schema.rb index cacc028e25..ae9e76e33d 100644 --- a/api/app/graph/refinery/api/graphql_schema.rb +++ b/api/app/graph/refinery/api/graphql_schema.rb @@ -3,7 +3,7 @@ module Refinery module Api class GraphqlSchema < GraphQL::Schema - # mutation Types::MutationType + mutation Types::MutationType query Types::QueryType end end diff --git a/api/app/graph/refinery/api/inputs/pages/page_input.rb b/api/app/graph/refinery/api/inputs/pages/page_input.rb deleted file mode 100644 index 6e47e2178a..0000000000 --- a/api/app/graph/refinery/api/inputs/pages/page_input.rb +++ /dev/null @@ -1,36 +0,0 @@ -# frozen_string_literal: true - -module Refinery - module Api - module Inputs - module Pages - class PageInput < GraphQL::Schema::InputObject - graphql_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 deleted file mode 100644 index 73e37a0921..0000000000 --- a/api/app/graph/refinery/api/inputs/pages/page_part_input.rb +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true - -module Refinery - module Api - module Inputs - module Pages - class PagePartInput < GraphQL::Schema::InputObject - graphql_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/create.rb b/api/app/graph/refinery/api/mutations/pages/create.rb index f9341d3abd..4d7d640b3d 100644 --- a/api/app/graph/refinery/api/mutations/pages/create.rb +++ b/api/app/graph/refinery/api/mutations/pages/create.rb @@ -4,21 +4,30 @@ module Refinery module Api module Mutations module Pages - class Create < GraphQL::Schema::Mutation - graphql_name 'CreatePage' - description 'Create a page' - - argument :page, !Types::Pages::PageType + class Create < Mutations::BaseMutation + null true - type Types::Pages::PageType + graphql_name 'CreatePage' + description 'Create a Page' - resolve -> (obj, inputs, ctx) { - inputs = inputs.to_h.deep_symbolize_keys + field :page, Types::Pages::PageAttributes, null: true + field :errors, [String], null: false - page = Refinery::Page.create!(inputs[:page]) + def resolve(page:) + page = Refinery::Page.create!(page) - { page: page } - } + if page.errors.empty? + { + page: page, + errors: [] + } + else + { + page: nil, + errors: page.errors.full_messages + } + end + end end end end diff --git a/api/app/graph/refinery/api/mutations/pages/delete.rb b/api/app/graph/refinery/api/mutations/pages/delete.rb index 4d80b78a30..544a6a9341 100644 --- a/api/app/graph/refinery/api/mutations/pages/delete.rb +++ b/api/app/graph/refinery/api/mutations/pages/delete.rb @@ -4,18 +4,30 @@ module Refinery module Api module Mutations module Pages - class Delete < Mutations::BaseMutation + class Delete < Mutations::BaseMutation graphql_name 'DeletePage' + description 'Delete a Page' argument :id, ID, required: true - return_field :page, Types::Pages::PageType + field :page, Types::Pages::PageType, null: false + field :errors, [String], null: false - resolve -> (id:) { + def resolve(id:) page = Refinery::Page.destroy!(id) - { page: page } - } + if page.errors.empty? + { + page: page, + errors: [] + } + else + { + page: nil, + errors: page.errors.full_messages + } + end + end end end end diff --git a/api/app/graph/refinery/api/mutations/pages/update.rb b/api/app/graph/refinery/api/mutations/pages/update.rb index f2cecbcb07..a562a7922b 100644 --- a/api/app/graph/refinery/api/mutations/pages/update.rb +++ b/api/app/graph/refinery/api/mutations/pages/update.rb @@ -4,22 +4,30 @@ module Refinery module Api module Mutations module Pages - class Update < GraphQL::Schema::Mutation + class Update < Mutations::BaseMutation graphql_name 'UpdatePage' - description 'Create a page' + description 'Update a Page' - input_field :id, !types.ID - input_field :page, !Inputs::Pages::PageInput + argument :id, ID, required: true - return_field :page, Types::Pages::PageType + field :page, Types::Pages::PageType, null: false + field :errors, [String], null: false - resolve -> (obj, inputs, ctx) { - inputs = inputs.to_h.deep_symbolize_keys + def resolve(id:, page:) + page = Refinery::Page.update(id, page) - page = Refinery::Page.update(inputs[:id], inputs[:page]) - - { page: page } - } + if page.errors.empty? + { + page: page, + errors: [] + } + else + { + page: nil, + errors: page.errors.full_messages + } + end + end end end end diff --git a/api/app/graph/refinery/api/resolvers/pages/page_resolver.rb b/api/app/graph/refinery/api/resolvers/pages/page_resolver.rb index eb2e643db8..00664bff12 100644 --- a/api/app/graph/refinery/api/resolvers/pages/page_resolver.rb +++ b/api/app/graph/refinery/api/resolvers/pages/page_resolver.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Refinery module Api module Resolvers diff --git a/api/app/graph/refinery/api/types/base_enum.rb b/api/app/graph/refinery/api/types/base_enum.rb index 0dcdc6c7d4..dc22a9603a 100644 --- a/api/app/graph/refinery/api/types/base_enum.rb +++ b/api/app/graph/refinery/api/types/base_enum.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Refinery module Api module Types diff --git a/api/app/graph/refinery/api/types/base_input_object.rb b/api/app/graph/refinery/api/types/base_input_object.rb index 3ef443f532..101f8e0f9d 100644 --- a/api/app/graph/refinery/api/types/base_input_object.rb +++ b/api/app/graph/refinery/api/types/base_input_object.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Refinery module Api module Types diff --git a/api/app/graph/refinery/api/types/base_interface.rb b/api/app/graph/refinery/api/types/base_interface.rb index 500c20d2da..d1aab507aa 100644 --- a/api/app/graph/refinery/api/types/base_interface.rb +++ b/api/app/graph/refinery/api/types/base_interface.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Refinery module Api module Types diff --git a/api/app/graph/refinery/api/types/base_object.rb b/api/app/graph/refinery/api/types/base_object.rb index 18cb6f185c..12d5d42972 100644 --- a/api/app/graph/refinery/api/types/base_object.rb +++ b/api/app/graph/refinery/api/types/base_object.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Refinery module Api module Types diff --git a/api/app/graph/refinery/api/types/base_union.rb b/api/app/graph/refinery/api/types/base_union.rb index 42fe04421d..6491713e51 100644 --- a/api/app/graph/refinery/api/types/base_union.rb +++ b/api/app/graph/refinery/api/types/base_union.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Refinery module Api module Types diff --git a/api/app/graph/refinery/api/types/pages/page_attributes.rb b/api/app/graph/refinery/api/types/pages/page_attributes.rb new file mode 100644 index 0000000000..1693304635 --- /dev/null +++ b/api/app/graph/refinery/api/types/pages/page_attributes.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module Refinery + module Api + module Types + module Pages + class PageAttributes < Types::BaseInputObject + description "Attributes for creating or updating a page" + + argument :parent_id, Integer, required: false + argument :path, String, required: false + argument :show_in_menu, Boolean, required: false + argument :link_url, String, required: false + argument :menu_match, String, required: false + argument :deletable, Boolean, required: false + argument :draft, Boolean, required: false + argument :skip_to_first_child, Boolean, required: false + argument :lft, Integer, required: false + argument :rgt, required: false + argument :depth, Integer, required: false + argument :view_template, String, required: false + argument :layout_template, String, required: false + argument :locale, String, required: false + argument :title, String, required: false + argument :custom_slug, String, required: false + argument :menu_title, String, required: false + argument :slug, String, required: false + argument :meta_description, String, required: false + argument :browser_title, String, required: false + + argument :parts, [Types::PagePart], required: false + end + end + end + end +end + + diff --git a/api/app/graph/refinery/api/types/pages/page_part_attributes.rb b/api/app/graph/refinery/api/types/pages/page_part_attributes.rb new file mode 100644 index 0000000000..10e0bc7098 --- /dev/null +++ b/api/app/graph/refinery/api/types/pages/page_part_attributes.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module Refinery + module Api + module Types + module Pages + class PagePartAttributes < Types::BaseInputObject + description "Attributes for creating or updating a page part" + + argument :slug, String, required: false + argument :position, Integer, required: false + argument :title, String, required: false + + argument :locale, String, required: false + argument :body, String, required: false + end + end + end + end +end \ No newline at end of file diff --git a/api/app/graph/refinery/api/types/query_type.rb b/api/app/graph/refinery/api/types/query_type.rb index 8e193f58e6..5014da7802 100644 --- a/api/app/graph/refinery/api/types/query_type.rb +++ b/api/app/graph/refinery/api/types/query_type.rb @@ -14,9 +14,9 @@ class QueryType < Types::BaseObject field :page, Types::Pages::PageType, null: true, resolve: Resolvers::Pages::PageResolver::ById do + argument :id, ID, required: true description "Find page by id" end - end end end diff --git a/api/config/routes.rb b/api/config/routes.rb index 700384ec35..cf0ca72261 100644 --- a/api/config/routes.rb +++ b/api/config/routes.rb @@ -1,9 +1,9 @@ Refinery::Core::Engine.routes.draw do namespace :api do post '/graphql', to: 'graphql#execute' + end - if Rails.env.development? - mount GraphiQL::Rails::Engine, at: '/graphiql', graphql_path: "/#{Refinery::Core.backend_route}/graphql" - end + if Rails.env.development? + mount GraphiQL::Rails::Engine, at: "/#{Refinery::Core.backend_route}/api/graphiql", graphql_path: '/api/graphql' end -end +end \ No newline at end of file diff --git a/api/lib/refinery/api.rb b/api/lib/refinery/api.rb index 48926d647f..2942426761 100644 --- a/api/lib/refinery/api.rb +++ b/api/lib/refinery/api.rb @@ -1,5 +1,6 @@ require 'refinerycms-core' require 'graphql' +require 'graphiql/rails' module Refinery module Api diff --git a/api/refinerycms-api.gemspec b/api/refinerycms-api.gemspec index 8402f0158e..5f8002bcc4 100644 --- a/api/refinerycms-api.gemspec +++ b/api/refinerycms-api.gemspec @@ -22,10 +22,10 @@ Gem::Specification.new do |s| # Runtime dependencies s.add_dependency 'refinerycms-core', version s.add_dependency 'graphql', '~> 1.8', '>= 1.8.5' + s.add_dependency 'graphiql-rails', '~> 1.4' # Development dependencies (usually used for testing) s.add_development_dependency 'refinerycms-testing', '~> 4.0.0' - s.add_development_dependency 'graphiql-rails', '~> 1.4' s.required_ruby_version = Refinery::Version.required_ruby_version diff --git a/api/spec/graph/refinery/api/mutations/pages/create_page_mutation_spec.rb b/api/spec/graph/refinery/api/mutations/pages/create_page_mutation_spec.rb index 53d5723233..0ace09e606 100644 --- a/api/spec/graph/refinery/api/mutations/pages/create_page_mutation_spec.rb +++ b/api/spec/graph/refinery/api/mutations/pages/create_page_mutation_spec.rb @@ -6,7 +6,7 @@ module Refinery module Api module Mutations module Pages - describe 'CreatePageMutation' do + describe 'CreatePageMutation', focus: true do let(:logged_in_user) { Refinery::Core::NilUser.new } let(:context) { {current_user: logged_in_user} } @@ -21,8 +21,8 @@ module Pages let(:query_string) do <<-QUERY -mutation($page: CreatePageInput!) { - create_page(input: $page) { +mutation { + createPage(page: $page) { page { title } @@ -47,6 +47,7 @@ module Pages it 'returns the page title of the newly created page' do subject + byebug expect(result['data']['create_page']['page']['title']).to eq('Test page') end end diff --git a/api/spec/graph/refinery/api/mutations/pages/delete_page_mutation_spec.rb b/api/spec/graph/refinery/api/mutations/pages/delete_page_mutation_spec.rb index 8d91c662c2..ea5ed22947 100644 --- a/api/spec/graph/refinery/api/mutations/pages/delete_page_mutation_spec.rb +++ b/api/spec/graph/refinery/api/mutations/pages/delete_page_mutation_spec.rb @@ -24,7 +24,7 @@ module Pages let(:query_string) do <<-QUERY mutation($page: DeletePageInput!) { - delete_page(input: $page) { + deletePage(input: $page) { page { id } @@ -39,7 +39,6 @@ module Pages 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 diff --git a/api/spec/graph/refinery/api/mutations/pages/update_page_mutation_spec.rb b/api/spec/graph/refinery/api/mutations/pages/update_page_mutation_spec.rb index cb6ea0b5b2..fb8cc2ec00 100644 --- a/api/spec/graph/refinery/api/mutations/pages/update_page_mutation_spec.rb +++ b/api/spec/graph/refinery/api/mutations/pages/update_page_mutation_spec.rb @@ -6,7 +6,7 @@ module Refinery module Api module Mutations module Pages - describe 'UpdatePageMutation' do + describe 'UpdatePageMutation', focus: true do let(:logged_in_user) { Refinery::Core::NilUser.new } let!(:page) { FactoryBot.create(:page) } @@ -24,7 +24,7 @@ module Pages let(:query_string) do <<-QUERY mutation($page: UpdatePageInput!) { - update_page(input: $page) { + updatePage(input: $page) { page { id title