Permalink
Browse files

Installed graphql, generated types and mutations, and set up a route …

…for graphiql
  • Loading branch information...
mattboldt committed Dec 27, 2018
1 parent 2dfe329 commit 0c9b56aa4b0aea178856aebd5832337763826daf
@@ -26,7 +26,9 @@ gem 'puma', '~> 3.11'
gem 'bootsnap', '>= 1.1.0', require: false

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem 'rack-cors'
gem 'rack-cors'
# Our GraphQL server-side library
gem 'graphql'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
@@ -39,6 +41,7 @@ group :development do
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'graphiql-rails'
end


@@ -55,6 +55,10 @@ GEM
ffi (1.9.25)
globalid (0.4.1)
activesupport (>= 4.2.0)
graphiql-rails (1.5.0)
railties
sprockets-rails
graphql (1.8.11)
i18n (1.3.0)
concurrent-ruby (~> 1.0)
listen (3.1.5)
@@ -79,6 +83,7 @@ GEM
mini_portile2 (~> 2.4.0)
puma (3.12.0)
rack (2.0.6)
rack-cors (1.0.2)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (5.2.2)
@@ -138,8 +143,11 @@ DEPENDENCIES
bootsnap (>= 1.1.0)
byebug
faker
graphiql-rails
graphql
listen (>= 3.0.5, < 3.2)
puma (~> 3.11)
rack-cors
rails (~> 5.2.2)
spring
spring-watcher-listen (~> 2.0.0)
@@ -0,0 +1,43 @@
class GraphqlController < ApplicationController
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 = RailsGraphqlDemoSchema.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
No changes.
@@ -0,0 +1,4 @@
class RailsGraphqlDemoSchema < GraphQL::Schema
mutation(Types::MutationType)
query(Types::QueryType)
end
No changes.
@@ -0,0 +1,4 @@
module Types
class BaseEnum < GraphQL::Schema::Enum
end
end
@@ -0,0 +1,4 @@
module Types
class BaseInputObject < GraphQL::Schema::InputObject
end
end
@@ -0,0 +1,5 @@
module Types
module BaseInterface
include GraphQL::Schema::Interface
end
end
@@ -0,0 +1,4 @@
module Types
class BaseObject < GraphQL::Schema::Object
end
end
@@ -0,0 +1,4 @@
module Types
class BaseScalar < GraphQL::Schema::Scalar
end
end
@@ -0,0 +1,4 @@
module Types
class BaseUnion < GraphQL::Schema::Union
end
end
@@ -0,0 +1,4 @@
module Types
class BookType < Types::BaseObject
end
end
@@ -0,0 +1,10 @@
module Types
class MutationType < Types::BaseObject
# TODO: remove me
field :test_field, String, null: false,
description: "An example field added by the generator"
def test_field
"Hello World"
end
end
end
@@ -0,0 +1,13 @@
module Types
class QueryType < Types::BaseObject
# Add root-level fields here.
# They will be entry points for queries on your schema.

# TODO: remove me
field :test_field, String, null: false,
description: "An example field added by the generator"
def test_field
"Hello World!"
end
end
end
@@ -0,0 +1,4 @@
module Types
class UserType < Types::BaseObject
end
end
@@ -1,3 +1,6 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
if Rails.env.development?
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "graphql#execute"
end
post "/graphql", to: "graphql#execute"
end

0 comments on commit 0c9b56a

Please sign in to comment.