From a41520f72bb70aa35050143cf96219dc72ea9ed8 Mon Sep 17 00:00:00 2001 From: Brice Sanchez Date: Wed, 25 Jul 2018 00:08:45 -0400 Subject: [PATCH] WIP: Refactor graphql ruby with class name style --- .../refinery/api/fields/pages/page_field.rb | 2 +- .../refinery/api/fields/pages/pages_field.rb | 2 +- api/app/graph/refinery/api/graphql_schema.rb | 2 +- .../refinery/api/inputs/pages/page_input.rb | 2 +- .../api/inputs/pages/page_part_input.rb | 2 +- .../refinery/api/mutations/pages/create.rb | 26 ++++++++ .../refinery/api/mutations/pages/delete.rb | 23 +++++++ .../api/mutations/pages/page_mutations.rb | 60 ------------------- .../refinery/api/mutations/pages/update.rb | 27 +++++++++ .../refinery/api/types/base_interface.rb | 9 +++ .../graph/refinery/api/types/base_object.rb | 8 +++ .../graph/refinery/api/types/mutation_type.rb | 8 +-- .../api/types/pages/page_part_type.rb | 2 +- .../refinery/api/types/pages/page_type.rb | 2 +- .../graph/refinery/api/types/query_type.rb | 2 +- 15 files changed, 105 insertions(+), 72 deletions(-) create mode 100644 api/app/graph/refinery/api/mutations/pages/create.rb create mode 100644 api/app/graph/refinery/api/mutations/pages/delete.rb delete mode 100644 api/app/graph/refinery/api/mutations/pages/page_mutations.rb create mode 100644 api/app/graph/refinery/api/mutations/pages/update.rb create mode 100644 api/app/graph/refinery/api/types/base_interface.rb create mode 100644 api/app/graph/refinery/api/types/base_object.rb diff --git a/api/app/graph/refinery/api/fields/pages/page_field.rb b/api/app/graph/refinery/api/fields/pages/page_field.rb index 07b9992335..fc6609f303 100644 --- a/api/app/graph/refinery/api/fields/pages/page_field.rb +++ b/api/app/graph/refinery/api/fields/pages/page_field.rb @@ -4,7 +4,7 @@ module Refinery module Api module Fields module Pages - PageField = GraphQL::Field.define do + class PageField < GraphQL::Schema::Field name 'Page' description 'Find a page by ID' diff --git a/api/app/graph/refinery/api/fields/pages/pages_field.rb b/api/app/graph/refinery/api/fields/pages/pages_field.rb index cb1048503b..a3a1faafd0 100644 --- a/api/app/graph/refinery/api/fields/pages/pages_field.rb +++ b/api/app/graph/refinery/api/fields/pages/pages_field.rb @@ -4,7 +4,7 @@ module Refinery module Api module Fields module Pages - PagesField = GraphQL::Field.define do + class PagesField < GraphQL::Schema::Field name 'Pages' description 'Find all pages' diff --git a/api/app/graph/refinery/api/graphql_schema.rb b/api/app/graph/refinery/api/graphql_schema.rb index 5c457b4dcf..b2b8b79534 100644 --- a/api/app/graph/refinery/api/graphql_schema.rb +++ b/api/app/graph/refinery/api/graphql_schema.rb @@ -2,7 +2,7 @@ module Refinery module Api - GraphqlSchema = GraphQL::Schema.define do + class GraphqlSchema < GraphQL::Schema query Types::QueryType mutation Types::MutationType diff --git a/api/app/graph/refinery/api/inputs/pages/page_input.rb b/api/app/graph/refinery/api/inputs/pages/page_input.rb index f6d3ac6166..814bdcbe72 100644 --- a/api/app/graph/refinery/api/inputs/pages/page_input.rb +++ b/api/app/graph/refinery/api/inputs/pages/page_input.rb @@ -4,7 +4,7 @@ module Refinery module Api module Inputs module Pages - PageInput = GraphQL::InputObjectType.define do + class PageInput < GraphQL::Schema::InputObject name 'PageInput' input_field :parent_id, types.Int 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 index 33f52a5cfc..8ba01e534a 100644 --- a/api/app/graph/refinery/api/inputs/pages/page_part_input.rb +++ b/api/app/graph/refinery/api/inputs/pages/page_part_input.rb @@ -4,7 +4,7 @@ module Refinery module Api module Inputs module Pages - PagePartInput = GraphQL::InputObjectType.define do + class PagePartInput < GraphQL::Schema::InputObject name 'PagePartInput' input_field :slug, types.String diff --git a/api/app/graph/refinery/api/mutations/pages/create.rb b/api/app/graph/refinery/api/mutations/pages/create.rb new file mode 100644 index 0000000000..b6e87026ff --- /dev/null +++ b/api/app/graph/refinery/api/mutations/pages/create.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module Refinery + module Api + module Mutations + module Pages + class Create < GraphQL::Schema::Mutation + 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 + end + end + end +end \ No newline at end of file diff --git a/api/app/graph/refinery/api/mutations/pages/delete.rb b/api/app/graph/refinery/api/mutations/pages/delete.rb new file mode 100644 index 0000000000..7a04b657a7 --- /dev/null +++ b/api/app/graph/refinery/api/mutations/pages/delete.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module Refinery + module Api + module Mutations + module Pages + class Delete < GraphQL::Schema::Mutation + 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 \ 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 deleted file mode 100644 index 8d9565ff18..0000000000 --- a/api/app/graph/refinery/api/mutations/pages/page_mutations.rb +++ /dev/null @@ -1,60 +0,0 @@ -# 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 - - page = 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/mutations/pages/update.rb b/api/app/graph/refinery/api/mutations/pages/update.rb new file mode 100644 index 0000000000..db33db4b26 --- /dev/null +++ b/api/app/graph/refinery/api/mutations/pages/update.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module Refinery + module Api + module Mutations + module Pages + class Update < GraphQL::Schema::Mutation + 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 + + page = Refinery::Page.update(inputs[:id], inputs[:page]) + + { page: page } + } + end + end + end + end +end \ No newline at end of file diff --git a/api/app/graph/refinery/api/types/base_interface.rb b/api/app/graph/refinery/api/types/base_interface.rb new file mode 100644 index 0000000000..500c20d2da --- /dev/null +++ b/api/app/graph/refinery/api/types/base_interface.rb @@ -0,0 +1,9 @@ +module Refinery + module Api + module Types + class BaseInterface + include GraphQL::Schema::Interface + end + end + end +end \ No newline at end of file diff --git a/api/app/graph/refinery/api/types/base_object.rb b/api/app/graph/refinery/api/types/base_object.rb new file mode 100644 index 0000000000..18cb6f185c --- /dev/null +++ b/api/app/graph/refinery/api/types/base_object.rb @@ -0,0 +1,8 @@ +module Refinery + module Api + module Types + class BaseObject < GraphQL::Schema::Object + 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 index 671e5f2a6c..8ade6f2e31 100644 --- a/api/app/graph/refinery/api/types/mutation_type.rb +++ b/api/app/graph/refinery/api/types/mutation_type.rb @@ -3,13 +3,13 @@ module Refinery module Api module Types - MutationType = GraphQL::ObjectType.define do + class MutationType < Types::BaseObject 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 + field :createPage, mutation: Mutations::Pages::Create + field :updatePage, mutation: Mutations::Pages::Update + field :deletePage, mutation: Mutations::Pages::Delete end end end diff --git a/api/app/graph/refinery/api/types/pages/page_part_type.rb b/api/app/graph/refinery/api/types/pages/page_part_type.rb index fc5acc0a7f..b57a23f724 100644 --- a/api/app/graph/refinery/api/types/pages/page_part_type.rb +++ b/api/app/graph/refinery/api/types/pages/page_part_type.rb @@ -4,7 +4,7 @@ module Refinery module Api module Types module Pages - PagePartType = GraphQL::ObjectType.define do + class PagePartType < GraphQL::Schema::Object name "PagePart" description "A PagePart" diff --git a/api/app/graph/refinery/api/types/pages/page_type.rb b/api/app/graph/refinery/api/types/pages/page_type.rb index 382734d2bb..569b6119c6 100644 --- a/api/app/graph/refinery/api/types/pages/page_type.rb +++ b/api/app/graph/refinery/api/types/pages/page_type.rb @@ -4,7 +4,7 @@ module Refinery module Api module Types module Pages - PageType = GraphQL::ObjectType.define do + class PageType < GraphQL::Schema::Object name "Page" description "A Page" diff --git a/api/app/graph/refinery/api/types/query_type.rb b/api/app/graph/refinery/api/types/query_type.rb index 475ebd5d0b..13cb7fee1d 100644 --- a/api/app/graph/refinery/api/types/query_type.rb +++ b/api/app/graph/refinery/api/types/query_type.rb @@ -3,7 +3,7 @@ module Refinery module Api module Types - QueryType = GraphQL::ObjectType.define do + class QueryType < Types::BaseObject name 'Query' description 'The query root of this schema'