Skip to content

Commit

Permalink
Getting Started: bundle graphql, graphiql
Browse files Browse the repository at this point in the history
  • Loading branch information
ztratify committed Nov 30, 2020
1 parent cbbaa41 commit ab7f090
Show file tree
Hide file tree
Showing 18 changed files with 137 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ gem 'jbuilder', '~> 2.7'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

# Graphql rails
gem 'graphql', '1.9.17'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
Expand All @@ -35,3 +38,6 @@ end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

# Graphiql GUI
gem 'graphiql-rails', '1.7.0', group: :development
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ GEM
ffi (1.13.1)
globalid (0.4.2)
activesupport (>= 4.2.0)
graphiql-rails (1.7.0)
railties
sprockets-rails
graphql (1.9.17)
i18n (1.8.5)
concurrent-ruby (~> 1.0)
jbuilder (2.10.1)
Expand Down Expand Up @@ -168,6 +172,8 @@ PLATFORMS
DEPENDENCIES
bootsnap (>= 1.4.2)
byebug
graphiql-rails (= 1.7.0)
graphql (= 1.9.17)
jbuilder (~> 2.7)
listen (~> 3.2)
puma (~> 4.1)
Expand Down
48 changes: 48 additions & 0 deletions app/controllers/graphql_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 = RailsHowtographqlSchema.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
Empty file added app/graphql/mutations/.keep
Empty file.
8 changes: 8 additions & 0 deletions app/graphql/mutations/base_mutation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Mutations
class BaseMutation < GraphQL::Schema::RelayClassicMutation
argument_class Types::BaseArgument
field_class Types::BaseField
input_object_class Types::BaseInputObject
object_class Types::BaseObject
end
end
4 changes: 4 additions & 0 deletions app/graphql/rails_howtographql_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class RailsHowtographqlSchema < GraphQL::Schema
mutation(Types::MutationType)
query(Types::QueryType)
end
Empty file added app/graphql/types/.keep
Empty file.
4 changes: 4 additions & 0 deletions app/graphql/types/base_argument.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Types
class BaseArgument < GraphQL::Schema::Argument
end
end
4 changes: 4 additions & 0 deletions app/graphql/types/base_enum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Types
class BaseEnum < GraphQL::Schema::Enum
end
end
5 changes: 5 additions & 0 deletions app/graphql/types/base_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Types
class BaseField < GraphQL::Schema::Field
argument_class Types::BaseArgument
end
end
5 changes: 5 additions & 0 deletions app/graphql/types/base_input_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Types
class BaseInputObject < GraphQL::Schema::InputObject
argument_class Types::BaseArgument
end
end
7 changes: 7 additions & 0 deletions app/graphql/types/base_interface.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Types
module BaseInterface
include GraphQL::Schema::Interface

field_class Types::BaseField
end
end
5 changes: 5 additions & 0 deletions app/graphql/types/base_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Types
class BaseObject < GraphQL::Schema::Object
field_class Types::BaseField
end
end
4 changes: 4 additions & 0 deletions app/graphql/types/base_scalar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Types
class BaseScalar < GraphQL::Schema::Scalar
end
end
4 changes: 4 additions & 0 deletions app/graphql/types/base_union.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Types
class BaseUnion < GraphQL::Schema::Union
end
end
10 changes: 10 additions & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Rails.application.routes.draw do
if Rails.env.development?
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"
end
post "/graphql", to: "graphql#execute"
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

0 comments on commit ab7f090

Please sign in to comment.