Skip to content

Commit

Permalink
WIP: Update to GraphQL 1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
bricesanchez committed Sep 5, 2018
1 parent d3634f4 commit a485588
Show file tree
Hide file tree
Showing 25 changed files with 151 additions and 106 deletions.
44 changes: 35 additions & 9 deletions api/app/controllers/refinery/api/graphql_controller.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
module Refinery
module Api
class GraphqlController < Refinery::AdminController

class GraphqlController < ApplicationController
def execute
variables = ensure_hash(params[:variables])
query = params[:query]
variables = params[:variables] || {}
operation_name = params[:operationName]

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

render json: result
rescue => e
raise e unless Rails.env.development?
handle_error_in_development e
end

private
Expand All @@ -22,6 +23,31 @@ def context
current_user: current_refinery_user
}
end

# Handle form data, JSON body, or a blank value
def ensure_hash(ambiguous_param)
case ambiguous_param
when String
if ambiguous_param.present?
ensure_hash(JSON.parse(ambiguous_param))
else
{}
end
when Hash, ActionController::Parameters
ambiguous_param
when nil
{}
else
raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
end
end

def handle_error_in_development(e)
logger.error e.message
logger.error e.backtrace.join("\n")

render json: { error: { message: e.message, backtrace: e.backtrace }, data: {} }, status: 500
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Api
module Fields
module Pages
class PageField < GraphQL::Schema::Field
name 'Page'
graphql_name 'Page'
description 'Find a page by ID'

type Types::Pages::PageType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Api
module Fields
module Pages
class PagesField < GraphQL::Schema::Field
name 'Pages'
graphql_name 'Pages'
description 'Find all pages'

type types[Types::Pages::PageType]
Expand Down
9 changes: 2 additions & 7 deletions api/app/graph/refinery/api/graphql_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
module Refinery
module Api
class GraphqlSchema < GraphQL::Schema
# mutation Types::MutationType
query Types::QueryType
mutation Types::MutationType

resolve_type -> (obj, args, ctx) {
type_name = obj.class.name
Schema.types[type_name]
}
end
end
end
end
2 changes: 1 addition & 1 deletion api/app/graph/refinery/api/inputs/pages/page_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Api
module Inputs
module Pages
class PageInput < GraphQL::Schema::InputObject
name 'PageInput'
graphql_name 'PageInput'

input_field :parent_id, types.Int
input_field :path, types.String
Expand Down
2 changes: 1 addition & 1 deletion api/app/graph/refinery/api/inputs/pages/page_part_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Api
module Inputs
module Pages
class PagePartInput < GraphQL::Schema::InputObject
name 'PagePartInput'
graphql_name 'PagePartInput'

input_field :slug, types.String
input_field :position, types.Int
Expand Down
8 changes: 8 additions & 0 deletions api/app/graph/refinery/api/mutations/base_mutation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Refinery
module Api
module Mutations
class Mutations::BaseMutation < GraphQL::Schema::RelayClassicMutation
end
end
end
end
6 changes: 3 additions & 3 deletions api/app/graph/refinery/api/mutations/pages/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ module Api
module Mutations
module Pages
class Create < GraphQL::Schema::Mutation
name 'CreatePage'
graphql_name 'CreatePage'
description 'Create a page'

input_field :page, !Inputs::Pages::PageInput
argument :page, !Types::Pages::PageType

return_field :page, Types::Pages::PageType
type Types::Pages::PageType

resolve -> (obj, inputs, ctx) {
inputs = inputs.to_h.deep_symbolize_keys
Expand Down
10 changes: 5 additions & 5 deletions api/app/graph/refinery/api/mutations/pages/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ module Refinery
module Api
module Mutations
module Pages
class Delete < GraphQL::Schema::Mutation
name 'DeletePage'
class Delete < Mutations::BaseMutation
graphql_name 'DeletePage'

input_field :id, !types.ID
argument :id, ID, required: true

return_field :page, Types::Pages::PageType

resolve -> (obj, inputs, ctx) {
page = Refinery::Page.destroy(inputs[:id])
resolve -> (id:) {
page = Refinery::Page.destroy!(id)

{ page: page }
}
Expand Down
2 changes: 1 addition & 1 deletion api/app/graph/refinery/api/mutations/pages/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Api
module Mutations
module Pages
class Update < GraphQL::Schema::Mutation
name 'UpdatePage'
graphql_name 'UpdatePage'
description 'Create a page'

input_field :id, !types.ID
Expand Down
21 changes: 21 additions & 0 deletions api/app/graph/refinery/api/resolvers/pages/page_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Refinery
module Api
module Resolvers
module Pages
class PageResolver
class All
def self.call(obj, args, ctx)
Refinery::Page.live
end
end

class ById
def self.call(obj, args, ctx)
Refinery::Page.find_by_id(args[:id])
end
end
end
end
end
end
end
26 changes: 0 additions & 26 deletions api/app/graph/refinery/api/types/active_record_interface.rb

This file was deleted.

8 changes: 8 additions & 0 deletions api/app/graph/refinery/api/types/base_enum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Refinery
module Api
module Types
class BaseEnum < GraphQL::Schema::Enum
end
end
end
end
8 changes: 8 additions & 0 deletions api/app/graph/refinery/api/types/base_input_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Refinery
module Api
module Types
class BaseInputObject < GraphQL::Schema::InputObject
end
end
end
end
8 changes: 8 additions & 0 deletions api/app/graph/refinery/api/types/base_union.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Refinery
module Api
module Types
class BaseUnion < GraphQL::Schema::Union
end
end
end
end
2 changes: 1 addition & 1 deletion api/app/graph/refinery/api/types/date_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Refinery
module Api
module Types
DateType = GraphQL::ScalarType.define do
name "Date"
graphql_name "Date"
description "Valid date format (parsable by Ruby's Date.parse)"

coerce_input -> (value, context) do
Expand Down
2 changes: 1 addition & 1 deletion api/app/graph/refinery/api/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Refinery
module Api
module Types
class MutationType < Types::BaseObject
name 'Mutation'
graphql_name 'Mutation'
description 'The mutation root for this schema'

field :createPage, mutation: Mutations::Pages::Create
Expand Down
14 changes: 6 additions & 8 deletions api/app/graph/refinery/api/types/pages/page_part_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ module Api
module Types
module Pages
class PagePartType < GraphQL::Schema::Object
name "PagePart"
graphql_name "PagePart"
description "A PagePart"

interfaces [Types::ActiveRecordInterface]
field :slug, String, null: true
field :position, Integer, null: true
field :title, String, null: true

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

field :locale, types.String
field :body, types.String
field :locale, String, null: true
field :body, String, null: true
end
end
end
Expand Down
53 changes: 22 additions & 31 deletions api/app/graph/refinery/api/types/pages/page_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,30 @@ module Api
module Types
module Pages
class PageType < GraphQL::Schema::Object
name "Page"
graphql_name "Page"
description "A Page"

interfaces [Types::ActiveRecordInterface]

field :parent_id, types.Int
field :path, types.String
field :show_in_menu, types.Boolean
field :link_url, types.String
field :menu_match, types.String
field :deletable, types.Boolean
field :draft, types.Boolean
field :skip_to_first_child, types.Boolean
field :lft, types.Int
field :rgt, types.Int
field :depth, types.Int
field :view_template, types.String
field :layout_template, types.String
field :locale, types.String
field :title, types.String
field :custom_slug, types.String
field :menu_title, types.String
field :slug, types.String
field :meta_description, types.String
field :browser_title, types.String

field :parts do
type types[Types::Pages::PagePartType]

resolve -> (obj, args, ctx) {
obj.parts.order(:position)
}
end
field :parent_id, Integer, null: true
field :path, String, null: true
field :show_in_menu, Boolean, null: true
field :link_url, String, null: true
field :menu_match, String, null: true
field :deletable, Boolean, null: true
field :draft, Boolean, null: true
field :skip_to_first_child, Boolean, null: true
field :lft, Integer, null: true
field :rgt, Integer, null: true
field :depth, Integer, null: true
field :view_template, String, null: true
field :layout_template, String, null: true
field :locale, String, null: true
field :title, String, null: true
field :custom_slug, String, null: true
field :menu_title, String, null: true
field :slug, String, null: true
field :meta_description, String, null: true
field :browser_title, String, null: true
field :parts, [PagePartType], null: true
end
end
end
Expand Down
14 changes: 11 additions & 3 deletions api/app/graph/refinery/api/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ module Refinery
module Api
module Types
class QueryType < Types::BaseObject
name 'Query'
graphql_name 'Query'
description 'The query root of this schema'

field :page, field: Fields::Pages::PageField
field :pages, field: Fields::Pages::PagesField
field :pages, [Types::Pages::PageType],
null: true, resolve: Resolvers::Pages::PageResolver::All do
description "All pages"
end

field :page, Types::Pages::PageType,
null: true, resolve: Resolvers::Pages::PageResolver::ById do
description "Find page by id"
end

end
end
end
Expand Down
4 changes: 2 additions & 2 deletions api/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Refinery::Core::Engine.routes.draw do
namespace :api do
post 'graphql' => 'graphql#execute'
post '/graphql', to: 'graphql#execute'

if Rails.env.development?
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/#{Refinery::Core.backend_route}/graphql"
mount GraphiQL::Rails::Engine, at: '/graphiql', graphql_path: "/#{Refinery::Core.backend_route}/graphql"
end
end
end
1 change: 0 additions & 1 deletion api/lib/refinery/api.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'refinerycms-core'
require 'graphql'
require 'graphiql/rails'

module Refinery
module Api
Expand Down
Loading

0 comments on commit a485588

Please sign in to comment.