Skip to content

Commit

Permalink
Ensure graphiql loads
Browse files Browse the repository at this point in the history
- Fix manifest per: rmosolgo/graphiql-rails#75
- Nest graphql_controller in Api module
- Clean up routes
  • Loading branch information
aaronpanch committed Mar 17, 2020
1 parent 05e6f6f commit 207a34e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 50 deletions.
2 changes: 2 additions & 0 deletions app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
//= link_tree ../images
//= link_directory ../stylesheets .css
//= link graphiql/rails/application.css
//= link graphiql/rails/application.js
50 changes: 50 additions & 0 deletions app/controllers/api/graphql_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Api
class GraphqlController < ApplicationController
# If accessing from outside this domain, nullify the session
# This allows for outside API access while preventing CSRF attacks,
# but you'll have to authenticate your user separately
# protect_from_forgery with: :null_session

def execute
variables = ensure_hash(params[:variables])
query = params[:query]
operation_name = params[:operationName]
context = {
# Query context goes here, for example:
# current_user: current_user,
}
result = IdeaBoardSchema.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

# 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
48 changes: 0 additions & 48 deletions app/controllers/graphql_controller.rb

This file was deleted.

5 changes: 3 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Rails.application.routes.draw do
if Rails.env.development?
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/api/graphql"
end
post "/graphql", to: "graphql#execute"

namespace :api do
resources :tokens, only: :create
post "/graphql", to: "graphql#execute"
end

get "*path", to: 'client#index'
Expand Down

0 comments on commit 207a34e

Please sign in to comment.