Skip to content

Commit

Permalink
Merge 720da16 into 2385463
Browse files Browse the repository at this point in the history
  • Loading branch information
bricesanchez committed Jul 25, 2018
2 parents 2385463 + 720da16 commit ad7618f
Show file tree
Hide file tree
Showing 33 changed files with 793 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ env:
- DB=postgresql EXTENSION=pages
- DB=postgresql EXTENSION=images
- DB=postgresql EXTENSION=resources
- DB=postgresql EXTENSION=api
- DB=mysql EXTENSION=core
- DB=mysql EXTENSION=pages
- DB=mysql EXTENSION=images
- DB=mysql EXTENSION=resources
- DB=mysql EXTENSION=api
notifications:
email: true
irc:
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ path "./" do
gem "refinerycms-images"
gem "refinerycms-pages"
gem "refinerycms-resources"
gem "refinerycms-api"
end

gem 'spring'
Expand Down
27 changes: 27 additions & 0 deletions api/app/controllers/refinery/api/graphql_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Refinery
module Api
class GraphqlController < Refinery::AdminController

def execute
query = params[:query]
variables = params[:variables] || {}

begin
result = Refinery::Api::GraphqlSchema.execute(query, variables: variables, context: context)
rescue => error
result = { errors: [{ message: error.message }] }
end

render json: result
end

private

def context
{
current_user: current_refinery_user
}
end
end
end
end
21 changes: 21 additions & 0 deletions api/app/graph/refinery/api/fields/pages/page_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Refinery
module Api
module Fields
module Pages
class PageField < GraphQL::Schema::Field
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
20 changes: 20 additions & 0 deletions api/app/graph/refinery/api/fields/pages/pages_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Refinery
module Api
module Fields
module Pages
class PagesField < GraphQL::Schema::Field
name 'Pages'
description 'Find all pages'

type types[Types::Pages::PageType]

resolve -> (obj, args, ctx) {
Refinery::Page.all
}
end
end
end
end
end
15 changes: 15 additions & 0 deletions api/app/graph/refinery/api/graphql_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Refinery
module Api
class GraphqlSchema < GraphQL::Schema
query Types::QueryType
mutation Types::MutationType

resolve_type -> (obj, args, ctx) {
type_name = obj.class.name
Schema.types[type_name]
}
end
end
end
36 changes: 36 additions & 0 deletions api/app/graph/refinery/api/inputs/pages/page_input.rb
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
class PageInput < GraphQL::Schema::InputObject
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 api/app/graph/refinery/api/inputs/pages/page_part_input.rb
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
class PagePartInput < GraphQL::Schema::InputObject
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
26 changes: 26 additions & 0 deletions api/app/graph/refinery/api/mutations/pages/create.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions api/app/graph/refinery/api/mutations/pages/delete.rb
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions api/app/graph/refinery/api/mutations/pages/update.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions api/app/graph/refinery/api/types/active_record_interface.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Refinery
module Api
module Types
ActiveRecordInterface = GraphQL::InterfaceType.define do
name "ActiveRecord"
description "Active Record Interface"

field :id, !types.ID
field :updated_at do
type Types::DateType
resolve -> (obj, args, ctx) {
obj.updated_at
}
end
field :created_at do
type Types::DateType
resolve -> (obj, args, ctx) {
obj.created_at
}
end
end
end
end
end
9 changes: 9 additions & 0 deletions api/app/graph/refinery/api/types/base_interface.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Refinery
module Api
module Types
class BaseInterface
include GraphQL::Schema::Interface
end
end
end
end
8 changes: 8 additions & 0 deletions api/app/graph/refinery/api/types/base_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Refinery
module Api
module Types
class BaseObject < GraphQL::Schema::Object
end
end
end
end
24 changes: 24 additions & 0 deletions api/app/graph/refinery/api/types/date_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Refinery
module Api
module Types
DateType = GraphQL::ScalarType.define do
name "Date"
description "Valid date format (parsable by Ruby's Date.parse)"

coerce_input -> (value, context) do
begin Date.parse(value)
value.to_datetime
rescue ArgumentError => error
context.errors << error.message
end
end

coerce_result -> (value, context) do
value.to_datetime
end
end
end
end
end
16 changes: 16 additions & 0 deletions api/app/graph/refinery/api/types/mutation_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Refinery
module Api
module Types
class MutationType < Types::BaseObject
name 'Mutation'
description 'The mutation root for this schema'

field :createPage, mutation: Mutations::Pages::Create
field :updatePage, mutation: Mutations::Pages::Update
field :deletePage, mutation: Mutations::Pages::Delete
end
end
end
end
23 changes: 23 additions & 0 deletions api/app/graph/refinery/api/types/pages/page_part_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Refinery
module Api
module Types
module Pages
class PagePartType < GraphQL::Schema::Object
name "PagePart"
description "A PagePart"

interfaces [Types::ActiveRecordInterface]

field :slug, types.String
field :position, types.Int
field :title, types.String

field :locale, types.String
field :body, types.String
end
end
end
end
end
Loading

0 comments on commit ad7618f

Please sign in to comment.